How to Rotate Array to Left or Right in Java? Example - LeetCode Solution
Hello guys, rotating an array in Java is a common coding problems which are often used to teach beginners coding as well used during interviews to check candidate's programming and data structure skills. This problem may look easy but its not that easy, especially if you are not coding regular. In order to rotate an array of n elements to the right by kth index, you need to rearrange the item in such a way that the array will start from k + 1the element. For example, with n = 7 and k = 3, the array [1, 2, 3, 4 ,5, 6, 7] is rotated to [5, 6, 7, 1, 2, 3, 4]. See, it looks like you pick the the 4th element and rotated the array in right direction. The problem becomes even more interesting when interviewer ask you to rotate the array by left or right and in place. Could you do it in-place with O(1) extra space?