Lv.1arraymath
Rotate Array by K
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 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
| nums | k | result |
|---|---|---|
[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
-
nums.length -
nums[i] -
k
Expected complexity
- Time
- O(N)
- Space
- O(N)
Examples
| nums | k | Expected |
|---|---|---|
[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 and exceeds the time limit; place each element directly in .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1512 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 4 | verdict only |