Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int gcd(int a, int b) {
- while(b != 0) {
- int c = a;
- a = b;
- b = c % a;
- }
- return a;
- }
- public static void shift_array(int[] A, int n) {
- int N = A.length;
- n %= N;
- if(n < 0)
- n = N + n;
- int d = gcd(N, n);
- for(int i = 0; i < d; i++) {
- int temp = A[i];
- for(int j = i - n + N; j != i; j = (j - n + N) % N)
- A[(j + n) % N] = A[j];
- A[i + n] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement