Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rotate Array #32

Open
cheatsheet1999 opened this issue Sep 11, 2021 · 1 comment
Open

Rotate Array #32

cheatsheet1999 opened this issue Sep 11, 2021 · 1 comment

Comments

@cheatsheet1999
Copy link
Owner

Given an array, rotate the array to the right by k steps, where k is non-negative.

Screen Shot 2021-09-10 at 8 45 00 PM

/**
 * @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
Copy link

dbmohit commented Aug 29, 2022

can i work on this?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants