Guest User

For iAtraf.co.il

a guest
Jan 24th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1.     static void Move(int[] arr, int k) {
  2.         int temp = 0; // the value of the last item
  3.         // We need to store each time the last item.
  4.         for(int i = 0; i < k; i++) { // move the items around for "k" times.
  5.             temp = arr[arr.length-1]; // storig the last item data at temp;
  6.             arr[arr.length-1] = 0; // giving the last item a value of 0 - for debugging.
  7.             for(int j = arr.length-1; j > 0; j--) { // moving the items around
  8.                 arr[j] = arr[j-1];
  9.             }
  10.             arr[0] = temp; // setting the first item with the value of the last one
  11.         }
  12.         // while the sequence is not valie(as you mentioned "45,6,6,8,7,7,12,45,45")
  13.         // we will keep going until validity " 45,45,45,6,6,8,7,7,12"
  14.         while(arr[arr.length-1] == arr[0]) {
  15.             temp = arr[arr.length-1];
  16.             arr[arr.length-1] = 0;
  17.             for(int j = arr.length-1; j > 0; j--) {
  18.                     arr[j] = arr[j-1];
  19.               }
  20.             arr[0] = temp;
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment