You are viewing a single comment's thread. Return to all comments →
The "Runtime Error" for test case #4 occurs because k can be much bigger than n and in such cases the code tries to access a negative index.
For test case 4: k = 100000, n = 515.
The solution is to apply an extra modulo n on k before substracting:
queries.forEach(m => { // Modulo to stay inside the boundaries of the array console.log(arr[(n + m - (k % n)) % n]); });
Seems like cookies are disabled on this browser, please enable them to open this website
Circular Array Rotation
You are viewing a single comment's thread. Return to all comments →
The "Runtime Error" for test case #4 occurs because k can be much bigger than n and in such cases the code tries to access a negative index.
For test case 4: k = 100000, n = 515.
The solution is to apply an extra modulo n on k before substracting: