0

Since I'm practicing basic array problem in java. I had a problem about rotate n elements by k unit to left/right.

I know how to deal with small elements array, such as I have an array int[]arr={1,2,3}; I can just switch the position of the elements like this:

return new int[]{arr[1],arr[2],arr[0]};

After this if I got 100 or more elements in an array this way does not work at all. So I saw someone use reverse method to deal with it.

public void rotateProblem(int[]arr,int k){ //k means rotate k units to right
   k%=arr.length;
   k=2;
   reverse(arr, 0, arr.length - 1);
   reverse(arr, 0, k - 1); 
   reverse(arr, k, arr.length - 1);
}

But I don't get it at all how does this method reverse the array, and why do I need use k%=arr.length; Can anyone explain to me the reverse method?

2
  • Post the reverse method. It's not a Java built in. Commented Apr 13, 2017 at 2:55
  • The k%=arr.length; doesn't do anything as you have k=2; however, I expect this is to handle large values of k. Commented Apr 13, 2017 at 3:20

2 Answers 2

0

I assume, this problem is posted from leetcode

To rotate each element of array to the right by k steps
For example, for the array [1,2,3,4,5,6,7] and k = 3, is rotated to [5,6,7,1,2,3,4].

Note: You might have large k (greater than the number of elements in array).
Thus, with k = array.length the rotated array will be equal to the original array.
Similarly, k = array.length + 1 is equivalent to k = 1.

Therefore, we do k = k % arr.length in the first case to prevent unnecessary multiple rotations.


reverse(arr, 0, arr.length - 1); will result the array in [7,6,5,4,3,2,1]
reverse(arr, 0, k - 1); will result the array in [5,6,7,4,3,2,1]
As you see you have achieved the first section in array.
reverse(arr, k, arr.length - 1); will help us achieve the last part of the array. [5,6,7,1,2,3,4]

Overall, resultant (rotated) array will be [5,6,7,1,2,3,4].


There are multiple ways to achieve it. Say, len = arr.length

reverseArray(nums, len - k, len - 1); // [1,2,3,4,7,6,5]
reverseArray(nums, 0, len - k - 1);   // [4,3,2,1,7,6,5]
reverseArray(nums, 0, len - 1);       // [5,6,7,1,2,3,4]

is another way.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot but I still have a question about reverse() method, do you why they write the (arr,0,arr.length-1) into this method?
We need to rotate to the right. Consider it as taking the mirror image of the array. The elements on right automatically tend towards the left and vice versa.
0

But I don't get it at all how does this method reverse the array?

This is not a library implementation. You will have to write it by yourself. This might help.

why do I need use k%=arr.length;?

If you have an array of 100 elements and you need to rotate it by 550 positions, the resulting array will be similar to one you get after rotating by 50 elements. You will get the same array by rotating it by 100 positions, or 200 positions or, in general, k*100 positions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.