19 / 19

How do you rotate an array by k positions? (In-place vs extra space)

Array Rotation

javascript
  1. 1

    Time complexity: O(n).

  2. 2

    Extra space: O(1) for the reversal approach.

  3. 3

    An extra-array approach is simpler but requires O(n) additional space.

  4. 4

    For very large k, k % n avoids unnecessary rotations.

  5. 5

    The direction of rotation must be defined clearly.

Difficulty: 3/10

Follow-up Questions

  • How would you rotate left by k?
  • Can you solve rotation using the cyclic replacement method?