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.
At the very least, you would have to allocate memory to store one integer. The question then becomes what you consider to be important: memory, or run-time, or something in between. If your primary focus is memory, then your solution is just to move EVERYTHING in the array one unit over, multiplied by the number of iterations. (You can mitigate this somewhat by right-shifting if the number of iterations is more than 1/2 the size of the array.) If your focus is time, then just transfer everything to another array, and back again, giving ~ O(2n). A decent in-between (which is what I went with) is to move only the number of elements out that you need to move (max 1/2 n) to another array, shift everything else, then re-insert the moved elements.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Left Rotation
You are viewing a single comment's thread. Return to all comments →
At the very least, you would have to allocate memory to store one integer. The question then becomes what you consider to be important: memory, or run-time, or something in between. If your primary focus is memory, then your solution is just to move EVERYTHING in the array one unit over, multiplied by the number of iterations. (You can mitigate this somewhat by right-shifting if the number of iterations is more than 1/2 the size of the array.) If your focus is time, then just transfer everything to another array, and back again, giving ~ O(2n). A decent in-between (which is what I went with) is to move only the number of elements out that you need to move (max 1/2 n) to another array, shift everything else, then re-insert the moved elements.