Created
March 3, 2018 08:11
-
-
Save notionquest/cdf47fb77eeccff79c3c007c2d715624 to your computer and use it in GitHub Desktop.
Rotate the integer array by n positions by left
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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