Skip to content

Instantly share code, notes, and snippets.

@notionquest
Created March 3, 2018 08:11
Show Gist options
  • Save notionquest/cdf47fb77eeccff79c3c007c2d715624 to your computer and use it in GitHub Desktop.
Save notionquest/cdf47fb77eeccff79c3c007c2d715624 to your computer and use it in GitHub Desktop.
Rotate the integer array by n positions by left
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
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 offset = a.length - k % a.length;
if (offset > 0) {
int[] copy = a.clone();
for (int i = 0; i < a.length; ++i) {
int j = (i + k) % a.length;
a[i] = copy[j];
}
}
System.out.print(Arrays.stream(a).mapToObj(e -> String.valueOf(e)).collect(Collectors.joining(" ")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment