Skip to the code
cf.
+10 XP

#643 Wrap a Carousel Index

easy

An image carousel has count slides, numbered 0 to count - 1. It wraps around: moving forward from the last slide goes to slide 0, and moving back from slide 0 goes to the last slide.

Write wrapIndex(index, step, count) that returns the slide shown after moving step slides from slide index. A negative step moves backward. The result must be between 0 and count - 1.

Example 1

Input: index = 4, step = 1, count = 5
Output: 0

Slide 4 is the last of 5, so one step forward wraps to slide 0.

Example 2

Input: index = 0, step = -1, count = 5
Output: 4

One step back from slide 0 wraps to the last slide, 4.

Constraints

  • 1 <= count <= 10^6
  • 0 <= index < count
  • -10^12 <= step <= 10^12
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).