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.

  1. Use a loop that runs k times.
  2. Inside that loop, perform one left rotation:
    • Store the first element (index 0) in a temp variable.
    • Use a second loop to shift every remaining element one position to the left (data[j] = data[j+1]).
    • Place the temp value at the very last index of the array.
  3. After k rotations are complete, print the array elements on new lines.

01EXAMPLE 1

Inputdata = [1, 2, 3, 4, 5], k = 2
Output3 4 5 1 2

Explanation: Each element moved left twice.

Constraints

  • Use nested for loops.
  • Do not use .slice() or the efficient indexing trick.
ArraysLoopsAlgorithms
JavaScript
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