Find the M-th element of array after K left rotations
Filed under Accolite
Free read 2 of 3Confirm 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
Confirm whether left rotation means shifting elements to the left (elements wrap around to the end).
Simulate K left rotations by shifting elements one by one, resulting in O(K * N) time.
Use modulo arithmetic. A left rotation by K is equivalent to shifting the index by K. The new index in the original array for any position is (M + K) % length.
K can be larger than the array length, so always compute effective K as K % length.
Solution
O(1) time, O(1) space
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.