The Manual Pivot
HardAcc. 79.2%
+45 XP 25
Iterative Shifting
Rotation can be done in one big jump (efficient) or one slow step at a time (manual).
The Assignment
Your function receives an array data and a rotation count k.
- Use a loop that runs
ktimes. - Inside that loop, perform one left rotation:
- Store the first element (
index 0) in atempvariable. - Use a second loop to shift every remaining element one position to the left (
data[j] = data[j+1]). - Place the
tempvalue at the very last index of the array.
- Store the first element (
- After
krotations are complete, print the array elements on new lines.
01EXAMPLE 1
Input
data = [1, 2, 3, 4, 5], k = 2Output
3
4
5
1
2Explanation: Each element moved left twice.
Constraints
- Use nested for loops.
- Do not use .slice() or the efficient indexing trick.
ArraysLoopsAlgorithms
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
data[1,2,3,4,5]
k2
Expected Output
3 4 5 1 2
Click RUN to test your solution