โ† Accolite questions
CodingEasy

Find the M-th element of array after K left rotations

Filed under Accolite

Free read 2 of 3

Confirm whether left rotation means shifting elements to the left (elements wrap around to the end).

The question

Given an array of integers and an integer K, determine the value at index M after performing K cyclic left rotations on the array.

Constraints

  • 1 <= array.length <= 10^5
  • 0 <= K <= 10^9
  • 0 <= M < array.length

Examples

Input
arr = [1, 2, 3, 4, 5], K = 2, M = 1
Output
4
Why
After 2 left rotations, arr becomes [3, 4, 5, 1, 2]. The element at index 1 is 4.

Approach

  1. Confirm whether left rotation means shifting elements to the left (elements wrap around to the end).

Solution

O(1) time, O(1) space

Python
def findElement(arr, k, m):
    n = len(arr)
    effective_k = k % n
    new_index = (m + effective_k) % n
    return arr[new_index]

Tips

  • What they are testing: Test when K is greater than the array length, when K is zero, and edge indices for M.
  • Common mistake: Performing actual array rotations, which leads to Time Limit Exceeded when K is very large.

Related Accolite questions

Collected from public candidate posts and coding forums. Updated Nov 2026.