Skip to content
← All posts

Distance Between Bus Stops: Circular Array Minimum Path

5 min read
leetcodeproblemeasyarrays

You are given a circular bus route with n stops numbered 0 to n - 1. You have an array distance where distance[i] is the distance between stop i and stop (i + 1) % n. Given a start stop and a destination stop, return the shortest distance between them traveling in either direction around the circle.

This is LeetCode 1184: Distance Between Bus Stops, and it is a clean introduction to working with circular arrays. The trick is realizing you do not need to simulate both directions separately.

12340123STARTDESTclockwise = 1+2 = 3counter = 4+3 = 7
distance = [1,2,3,4], start = 0, destination = 2. Clockwise path (green) is shorter: 3 vs 7.

Why this problem matters

Circular arrays show up everywhere: rotating queues, ring buffers, clock arithmetic, modular indexing. This problem strips away the complexity and focuses on the core idea: there are exactly two paths around a circle, and their distances add up to the total circumference. Once you see that relationship, you never need to walk both paths. You compute one and derive the other.

This same "total minus one direction" pattern appears in more advanced circular problems. Understanding it here makes problems like "Maximum Sum Circular Subarray" (LeetCode 918) and "Circular Array Loop" (LeetCode 457) much more approachable.

The key insight

Between any two points on a circle, there are exactly two paths. If you sum all the distances around the entire loop to get the total, and then compute the clockwise distance from start to destination, the counterclockwise distance is simply total - clockwise. The answer is whichever is smaller.

The algorithm:

  1. If start is greater than destination, swap them so you always walk forward from the smaller index to the larger one. This makes the clockwise sum a simple slice.
  2. Compute the clockwise distance by summing distance[start] through distance[destination - 1].
  3. Compute the total distance by summing the entire array.
  4. Return min(clockwise, total - clockwise).

The solution

def distance_between_bus_stops(distance: list[int], start: int, destination: int) -> int:
    if start > destination:
        start, destination = destination, start

    clockwise = sum(distance[start:destination])
    total = sum(distance)

    return min(clockwise, total - clockwise)

Let's walk through what each piece does.

The swap at the top normalizes the direction. Since the route is a circle, the distance from stop 3 to stop 1 clockwise is the same as the distance from stop 1 to stop 3 counterclockwise. By always making start the smaller index, you can sum a contiguous slice of the array for the clockwise direction without worrying about wrapping.

The sum(distance[start:destination]) call adds up the distances along the direct path from start to destination going forward through the array. This gives you the clockwise distance.

The sum(distance) call gives the total distance around the full loop. Since the two directions must add up to the total, subtracting the clockwise distance gives you the counterclockwise distance for free.

Finally, min(clockwise, total - clockwise) picks the shorter of the two paths. That is the answer.

The swap trick is optional but makes the code cleaner. Without it, you would need to handle the case where start is greater than destination by wrapping the sum around the end of the array. Swapping avoids that entirely.

Visual walkthrough

Let's trace through distance = [1, 2, 3, 4], start = 0, destination = 2 step by step.

Step 1: Compute the clockwise distance from stop 0 to stop 2

011122233304clockwise = 1 + 2 = 3

Sum distance[0] + distance[1] = 1 + 2 = 3. This is the clockwise total.

Step 2: Compute the total distance around the full loop

011122233304total = 1 + 2 + 3 + 4 = 10

Sum all distances: 1 + 2 + 3 + 4 = 10.

Step 3: Counterclockwise distance = total - clockwise

011122233304counterclockwise = 10 - 3 = 7

counterclockwise = 10 - 3 = 7. The other direction uses stops 2 → 3 → 0.

Step 4: Return the minimum of both directions

min(clockwise, counterclockwise)min(3, 7) = 3

min(3, 7) = 3. The clockwise path is shorter.

The clockwise path sums indices 0 and 1: 1 + 2 = 3. The full loop totals 10. The counterclockwise path is 10 - 3 = 7. The minimum is 3.

Complexity analysis

ApproachTimeSpace
Sum and compareO(n)O(1)

Time is O(n) where n is the number of stops. You iterate through the distance array twice at most: once for the clockwise sum and once for the total. In Python, sum(distance) and sum(distance[start:destination]) each take linear time.

Space is O(1) if you ignore the slice allocation. You store only two integers: clockwise and total. The slice distance[start:destination] creates a new list in Python, so technically it is O(n) space. If you want true O(1) space, you can compute the clockwise sum with a loop instead of a slice.

The building blocks

1. Circular complement: total minus one direction

The pattern of computing one direction around a circle and deriving the other from the total:

clockwise = sum(distance[start:destination])
counterclockwise = total - clockwise

This avoids walking the array in both directions. Any time you have a circular structure where two complementary paths must sum to a known total, you can use this trick. It shows up in circular subarray problems, ring buffer calculations, and modular arithmetic.

2. Index normalization with a swap

The pattern of swapping two indices so you can always iterate forward:

if start > destination:
    start, destination = destination, start

This eliminates the need for wrap-around logic. Instead of handling the case where you need to sum from index 5 to index 2 by going 5, 6, 7, 0, 1, 2, you swap so the slice is always contiguous. You see this same normalization trick in interval problems and range queries on circular structures.

Edge cases

Before submitting, think through these scenarios:

  • Start equals destination: the distance is 0. The clockwise sum of an empty slice is 0, and min(0, total - 0) is 0. Works correctly.
  • Start and destination are adjacent: the clockwise distance is distance[start]. The counterclockwise distance is the sum of all other elements.
  • All distances are equal: both directions give the same result if the stops are equidistant, or the shorter side wins otherwise.
  • Two stops only: distance = [a, b]. The answer is min(a, b).
  • Start greater than destination: the swap handles this. Without the swap, you would need to wrap around the array.

From understanding to recall

You have seen how Distance Between Bus Stops boils down to one sum and one subtraction. The logic is minimal and the code is short. But the underlying pattern of working with circular arrays appears in dozens of harder problems.

The details that matter are small. Do you swap start and destination or iterate backward? Do you subtract from the total or compute both sums independently? Under interview pressure, these decisions need to be automatic.

Spaced repetition is how you get there. You practice the circular complement pattern, the index normalization, and the min comparison from memory at increasing intervals. After a few rounds, you see "circular route" in a problem description and the template writes itself.

Related posts

  • Circular Array Loop - Detecting cycles in circular arrays using the same modular indexing concepts
  • Rotate Array - Another circular array problem where understanding wrap-around indexing is the key insight
  • Maximum Sum Circular Subarray - The advanced version of circular complement, where you find the max subarray by computing total minus the min subarray

CodeBricks breaks Distance Between Bus Stops into its circular complement and index normalization building blocks, then drills them independently with spaced repetition. You type each piece from scratch until the pattern is automatic. When a circular array problem shows up in your interview, you do not think about it. You just write it.