Skip to content

Commit

Permalink
Rotate array k times
Browse files Browse the repository at this point in the history
  • Loading branch information
AnghelLeonard committed Feb 27, 2020
1 parent 2ff1ab1 commit ce41d1a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Chapter10/RotateArrayKTimes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>coding.challenge</groupId>
<artifactId>RotateArrayKTimes</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<name>RotateArrayKTimes</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package coding.challenge;

public final class Arrays {

private Arrays() {
throw new AssertionError("Cannot be instantiated");
}

public static void rightRotate(int[] m, int k) {

int[] cm = m.clone();
int len = m.length;

for (int i = 0; i < len; i++) {
m[(i + k) % len] = cm[i];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package coding.challenge;

public class Main {

public static void main(String[] args) {

int A[] = {5, 5, 2, 3, 1, -2, 33, -1};
int k = 5;

System.out.println("Before: " + java.util.Arrays.toString(A));

Arrays.rightRotate(A, k);

System.out.println("After: " + java.util.Arrays.toString(A));
}
}

0 comments on commit ce41d1a

Please sign in to comment.