Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Move(int[] arr, int k) {
- int temp = 0; // the value of the last item
- // We need to store each time the last item.
- for(int i = 0; i < k; i++) { // move the items around for "k" times.
- temp = arr[arr.length-1]; // storig the last item data at temp;
- arr[arr.length-1] = 0; // giving the last item a value of 0 - for debugging.
- for(int j = arr.length-1; j > 0; j--) { // moving the items around
- arr[j] = arr[j-1];
- }
- arr[0] = temp; // setting the first item with the value of the last one
- }
- // while the sequence is not valie(as you mentioned "45,6,6,8,7,7,12,45,45")
- // we will keep going until validity " 45,45,45,6,6,8,7,7,12"
- while(arr[arr.length-1] == arr[0]) {
- temp = arr[arr.length-1];
- arr[arr.length-1] = 0;
- for(int j = arr.length-1; j > 0; j--) {
- arr[j] = arr[j-1];
- }
- arr[0] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment