Funcia
Lv.1arraymath

Rotate Array by K

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

nums lists who sits in seats 0, 1, 2, … around a circular table. Each time a bell rings, everyone moves one seat to the right, and the person in the last seat moves to seat 0. Return the seating (from seat 0) after the bell rings k times.

Examples

numskresult
[1, 2, 3, 4, 5]2[4, 5, 1, 2, 3]
[7, 8, 9]0[7, 8, 9]
[10, 20]5[20, 10]

Efficiency

This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.

Function signature

solution(nums: int[], k: int) → int[]

Constraints

  • 1≤1 \le nums.length ≤200,000\le 200{,}000
  • −109≤-10^9 \le nums[i] ≤109\le 10^9
  • 0≤0 \le k ≤109\le 10^9

Expected complexity

Time
O(N)
Space
O(N)

Examples

numskExpected
[1,2,3,4,5]2[4,5,1,2,3]
[7,8,9]0[7,8,9]
[10,20]5[20,10]

Hint

Hint

Only r = k % N rotations matter. After rotating, seat i holds the person who was in seat (i - r + N) % N: the last r elements move to the front. Rotating one step at a time is O(N2)O(N^2) and exceeds the time limit; place each element directly in O(N)O(N).

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251512 msderived
Kotlin 2.4 (JVM)1400 msestimated
C# 14 (.NET 10)1400 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness4010verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.