We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The part I'm missing here is why use a loop (O(n)). Can't you take the array and find the effective rotation based on the shift amount (using the same modular arithemetic you're doing? (Which is now O(1) since the length of the array is a property)
functionrotLeft(a,d){//calculate effective rotation (d % a.length)leteffectiveRotation=d%a.length;// split a at index of effective rotation into left and rightletleftPortion=a.slice(0,effectiveRotation);letrightPortion=a.slice(effectiveRotation);// concat left to rightreturnrightPortion.concat(leftPortion)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Arrays: Left Rotation
You are viewing a single comment's thread. Return to all comments →
The part I'm missing here is why use a loop (O(n)). Can't you take the array and find the effective rotation based on the shift amount (using the same modular arithemetic you're doing? (Which is now O(1) since the length of the array is a property)