• + 2 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]);
    });