10 · Arrays & Strings

#189 Rotate Array

Mediumsolve on LeetCode ↗

frequently reported atAppleBloombergMicrosoftSamsungAmazonGoogle

the problem

In plain words

Rotate the array to the right by k steps, in place. The last k elements move to the front.

in nums = [1,2,3,4,5,6,7], k = 3

out [5,6,7,1,2,3,4]

in nums = [-1,-100,3,99], k = 2

out [3,99,-1,-100]

  • 1 ≤ n ≤ 10⁵
  • 0 ≤ k ≤ 10⁵
  • Follow-up: O(1) extra space

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #31
      Next Permutation
      Mediumin Arrays & Strings
    2. #344
      Reverse String
      Easyin Recursion
    3. #48
      Rotate Image
      Mediumin Matrices