Rotate Array
cheatsheet1999 opened this issue · 1 comments
cheatsheet1999 commented
Given an array, rotate the array to the right by k steps, where k is non-negative.
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
k %= nums.length // if k is greater than nums.length then one cycle is completed that means it will remain the same and we have to remainder shifts
let reverse = function(i, j){
while(i < j){
[nums[i], nums[j]] = [nums[j], nums[i]];
i++;
j--;
}
} // suppose ----->--->
reverse(0, nums.length-1); // reverse <--<------
reverse(0, k-1) // reverse first part ---><----
reverse(k, nums.length-1)// reverse second part --->----->
};
dbmohit commented
can i work on this?