Rotate Array
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]Example 2:
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]Solutions
The problem is asking to rotate an array to the right by k steps. This means that each element in the array should be moved k positions to the right, and the elements that go beyond the end of the array should be moved to the start of the array.
Approach – Brute Force Technique
In the brute force approach, we rotate the array k times. In each rotation, we move each element to its next position and move the last element to the start of the array.
Steps
Define the function: The function
Rotateis defined with two parameters,nums(an array of integers) andk(the number of steps to rotate).Outer loop: The outer loop runs
ktimes. Each iteration of this loop corresponds to a single step of rotation.Store the last element: Before shifting the elements, the last element of the array is stored in the variable
temp. This is because it will be overwritten during the shifting process and we need to place it at the start of the array.Shift elements to the right: The inner loop runs from the end of the array to the start, shifting each element one position to the right. This is done by setting
nums[j]tonums[j - 1].Place the last element at the start: After all elements have been shifted, there is an empty spot at the start of the array (i.e.,
nums[0]). The last element that we stored intempis placed here.Repeat: The process is repeated
ktimes to achieve the required rotation.
public class Solution
{
public void Rotate(int[] nums, int k)
{
for (int i = 1; i <= k; i++)
{
int temp = nums[nums.Length - 1];
for (int j = nums.Length - 1; j > 0; j--)
{
nums[j] = nums[j - 1];
}
if (nums.Length > 0)
{
nums[0] = temp;
}
}
}
}
Complexity
Time Complexity: O(n*k)
Auxiliary Space: O(1)
Approach – Two Pointers Technique
In the optimized approach, we use the reverse operation to achieve the rotation. We first reverse the entire array, then reverse the first k elements, and finally reverse the remaining n - k elements.
Steps
Calculate the effective number of rotations: The number of rotations
kis taken modulo the length of the arrayn. This is because rotating the arrayntimes doesn't change the array, so we only need to consider the remainder ofkdivided byn.Reverse the entire array: The
Reversefunction is called with parameters0andn - 1, which reverses the entire array.Reverse the first part of the array: The
Reversefunction is called again with parameters0andk - 1, which reverses the firstkelements of the array.Reverse the second part of the array: The
Reversefunction is called one more time with parameterskandn - 1, which reverses the remainingn - kelements of the array.
The Reverse function is a helper function that reverses the elements in the nums array between the start and end indices. It does this by swapping the elements at the start and end indices, and then incrementing start and decrementing end until start is no longer less than end.
public class Solution
{
public void Rotate(int[] nums, int k)
{
int n = nums.Length;
k = k % n;
Reverse(nums, 0, n - 1);
Reverse(nums, 0, k - 1);
Reverse(nums, k, n - 1);
}
public void Reverse(int[] nums, int start, int end)
{
while (start < end)
{
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
Complexity
Time Complexity: O(n)
Auxiliary Space: O(1)
Last updated