2. Rotate Array
Different solutions to array rotation.
Given an array, rotate the array to the right by k
steps, where k
is non-negative.
Example 1:
Example 2:
Solution I
Solution:- Create a new array with rotated elements by finding the number of elements to be rotated.
This solution uses O(n) Time complexity and O(n) Space Complexity.
This solution can be optimized more by using O(n) time complexity and O(1) space complexity. Can be done by using in-place operations.
Solution II
This solution uses O(n) Time complexity and O(1) Space Complexity. This solution is based on Juggling Algorithm.
Last updated