Skip to content

Instantly share code, notes, and snippets.

@neslaram
Last active October 3, 2016 16:35
Show Gist options
  • Select an option

  • Save neslaram/f4b776c683cad4ced3393e2f68a88968 to your computer and use it in GitHub Desktop.

Select an option

Save neslaram/f4b776c683cad4ced3393e2f68a88968 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview: Arrays: Left Rotation https://www.hackerrank.com/challenges/ctci-array-left-rotation
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int[] arrayLeftRotation(int[] a, int n, int k) {
int b[]= new int[n];
for(int i=0; i<n ; i++){
b[i]=a[i];
}
for(int i=0; i<n; i++){
a[(n+i-k)%n]=b[i];
}
return a;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int[] output = new int[n];
output = arrayLeftRotation(a, n, k);
for(int i = 0; i < n; i++)
System.out.print(output[i] + " ");
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment