Skip to content

Latest commit

 

History

History
37 lines (21 loc) · 1.61 KB

File metadata and controls

37 lines (21 loc) · 1.61 KB

Description Given an integer array nums, rotate the array to the right by n steps, where n is non-negative number.

Examples

Example 1:

Input: nums = [1,2,3,4,5,6,7], n = 4 Output: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-10, 4, 5, -1], n = 2 Output: [5, -1, -10, 4]

Algorithmic Steps: This problem is solved with the help of two pointers technique, which is used to reverse subarrays. The algorithmic approach can be summarized as follows:

  1. Initialize length variable to store length of an input array.

  2. Update the number of times rotation(i.e, n) to n % length incase of number of rotations greater than length of an array.

  3. Create a helper function to reverse elements of an array using swap operation with the help left and right pointers(i.e, left and right). The left pointer needs to be incremented and right pointer needs to decremented for each iteration.

  4. Invoke reversal operation for entire array of elements(i.e, from the beginning to end of an array).

  5. Invoke reversal operation for first n number of elements(i.e, from the beginning to n-1).

  6. Invoke reversal operation for remaining number of elements(i.e, from n-1 to end of an array).

  7. Return in-place rotated array as output.

Time and Space complexity:

This algorithm has a time complexity of O(n), where n is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes O(n). So the overall time complexity is O(n).

Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be O(1).