Skip to the code
cf.
+10 XP

#82 Rotate the Carousel Right

easy

An image carousel keeps its slides in slides. Pressing "next" k times moves every slide k places to the right, and slides that pass the end come back at the start.

Write rotateCarousel(slides, k) that rotates slides in place, using O(1) extra memory. The array itself is checked after the call.

Example 1

Input: slides = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]

The last two slides wrap around to the front.

Example 2

Input: slides = ["a","b","c"], k = 4
Output: ["c","a","b"]

Rotating 3 slides by 4 is the same as rotating by 1.

Constraints

  • 0 <= slides.length <= 10^5
  • 0 <= k <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).