Hello there ๐, enthusiasts.
I wanted to share with others what I had learned from my mistake, which is that it is more necessary to understand the question and the restrictions offered rather than attempting to immediately answer the problem by simply comprehending the examples given.
Question - 21:
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]
Constraints:
1 <= nums.length <= 10<sup>5</sup>
-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1
0 <= k <= 10<sup>5</sup>
First approach:
class Solution {
public void rotate(int[] nums, int k) {
int len=nums.length;
int[] res= new int[len];
int j=0;
k=k%len;
for(int i=k;i>=1;i--){
res[j]=nums[len-i];
j++;
}
for(int i=k;i<len;i++){
res[i]=nums[i-k];
}
for(int i=0;i<res.length;i++) {
nums[i]=res[i];
}
}
}
Step-by-step implementation:
The method takes two parameters: an integer array
nums
and an integerk
that represents the number of positions by which the array should be rotated.The length of the array
nums
is stored in a variablelen
.A new integer array
res
is created with the same length as the original arraynums
.An integer
j
is initialized to 0.The value of
k
is reduced to its equivalent value within the range of the length ofnums
(i.e.,k
is set tok%len
).A loop is initiated from
i=k
toi>=1
, where each iteration takes the lasti
-th element from the original arraynums
and stores it in thej
-th index of the new arrayres
. Thej
index is then incremented by 1.A second loop is initiated from
i=k
toi<len
, where each iteration takes the(i-k)
-th element from the original arraynums
and stores it in thei
-th index of the new arrayres
.A third loop is initiated to copy the contents of the new array
res
back into the original arraynums
.Within the third loop, each element of
res
is assigned to the corresponding index ofnums
.The
rotate
method finishes execution, and the rotatednums
array is returned as the result.
Time complexity: The
rotate
method consists of three loops that iterate over thenums
andres
arrays. The first loop runsk
times, the second loop runslen-k
times, and the third loop runslen
times. Therefore, the time complexity of therotate
method is O(k + len), which simplifies to O(n) wheren
is the length of the input arraynums
.Space complexity: The
rotate
method creates a new integer arrayres
that has the same length as the input arraynums
. Therefore, the space complexity of therotate
method is O(n), wheren
is the length of the input arraynums
.
Second approach:
class Solution {
public void rotate(int[] nums, int k) {
int len = nums.length;
k = k % len;
// Reverse the entire array
reverse(nums, 0, len - 1);
// Reverse the first k elements
reverse(nums, 0, k - 1);
// Reverse the remaining elements
reverse(nums, k, len - 1);
}
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
Step-by-step implementation:
The method takes two parameters: an integer array
nums
and an integerk
that represents the number of positions by which the array should be rotated.The length of the array
nums
is stored in a variablelen
.The value of
k
is reduced to its equivalent value within the range of the length ofnums
(i.e.,k
is set tok%len
).The
reverse
method is called three times with different input parameters to perform the rotation. The first call reverses the entire arraynums
from index 0 to indexlen-1
. The second call reverses the firstk
elements of the arraynums
from index 0 to indexk-1
. The third call reverses the remaining elements of the arraynums
from indexk
to indexlen-1
.The
rotate
method finishes execution, and the rotatednums
array is returned as the result.The
reverse
method takes three parameters: an integer arraynums
, and two integer indicesstart
andend
that represent the range of the array to be reversed.Within the
reverse
method, awhile
loop is used to swap the elements of the arraynums
between thestart
andend
indices. Thestart
index is initially set to the first element of the range (start = 0
), and theend
index is initially set to the last element of the range (end = nums.length - 1
).Within each iteration of the
while
loop, the elements atstart
andend
indices are swapped using a temporary variabletemp
. Thestart
index is then incremented by 1, and theend
index is decremented by 1.The
while
the loop continues until thestart
index is greater than or equal to theend
index. At this point, the range betweenstart
andend
indices have been reversed.
Time complexity: The
rotate
method calls thereverse
method three times, and each call to thereverse
method takes O(n/3) time complexity (wheren
is the length of the input arraynums
) because it reverses a subarray of size n/3. Therefore, the total time complexity of therotate
method is O(n/3 * 3), which simplifies to O(n).Space complexity: The
rotate
method uses a constant amount of extra space for temporary variabletemp
. Therefore, the space complexity of therotate
method is O(1).
Conclusion:
Both the first and the second approaches have the same time complexity i.e., O(n). Whereas the second approach is using less space i.e., O(1) comparatively with the first approach i.e., O(n).
The Second approach that uses the reverse
method is a better implementation in terms of space complexity, readability, and simplicity.
Thanks for reading...Happy Learning ๐