C++20 sections · 1024 units
Open in Course

Xenia - The Pattern

Circular arithmetic

Here's what matters: when moving clockwise on a ring, you can only go forward. If you're at house 55 and need to reach house 22, you can't go backward. You must continue clockwise through houses 6,7,1,26, 7, 1, 2.

I'll show you the formula. If you're at position current and need to reach target: if target >= current, the distance is target - current. Otherwise, you wrap around: n - current + target.

This pattern handles circular structures in code. You'll see it in problems about clocks, calendars, and any scenario where numbers wrap around after reaching a maximum.