Advertisement
iSach

Untitled

Mar 20th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. public static int gcd(int a, int b) {
  2. while(b != 0) {
  3. int c = a;
  4. a = b;
  5. b = c % a;
  6. }
  7. return a;
  8. }
  9.  
  10. public static void shift_array(int[] A, int n) {
  11. int N = A.length;
  12. n %= N;
  13. if(n < 0)
  14. n = N + n;
  15. int d = gcd(N, n);
  16. for(int i = 0; i < d; i++) {
  17. int temp = A[i];
  18. for(int j = i - n + N; j != i; j = (j - n + N) % N)
  19. A[(j + n) % N] = A[j];
  20. A[i + n] = temp;
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement