Most Visited Sector in a Circular Track - Problem

Imagine you're organizing a marathon race on a circular track! The track is divided into n numbered sectors from 1 to n, and runners move in a counter-clockwise direction through ascending sector numbers.

Given an integer n representing the number of sectors and an array rounds containing the marathon route, you need to determine which sectors were visited most frequently during the entire race.

How it works:

  • The race consists of multiple rounds
  • Round i starts at sector rounds[i-1] and ends at sector rounds[i]
  • Runners visit every sector along their path (inclusive of start and end, except we don't double-count the end of one round and start of the next)
  • The track is circular, so after sector n comes sector 1

Goal: Return an array of the most frequently visited sectors, sorted in ascending order.

Input & Output

example_1.py — Python
$ Input: n = 4, rounds = [1,3,1,2]
Output: [1,2]
💡 Note: The marathon starts at sector 1, goes to 3 (visiting 1,2,3), then to 1 (visiting 4,1), then to 2 (visiting 2). Total visits: sector 1: 3 times, sector 2: 3 times, sector 3: 1 time, sector 4: 1 time. Sectors 1 and 2 are most visited.
example_2.py — Python
$ Input: n = 2, rounds = [2,1,2,1,2,1,2,1,2]
Output: [2]
💡 Note: Multiple rounds between sectors 2 and 1. The race starts at 2 and ends at 2, so sector 2 gets one extra visit compared to sector 1.
example_3.py — Python
$ Input: n = 7, rounds = [1,3,5,7]
Output: [1,2,3,4,5,6,7]
💡 Note: Each round covers large portions of the track. When we analyze the critical path from start (1) to end (7), all sectors are visited equally due to the intermediate complete cycles.

Visualization

Tap to expand
12345Critical PathKey Insight: Only Partial Segments Create Imbalance• Complete rounds visit every sector equally• Only the path from first start to last end matters• Sectors on this path are visited one extra timeTwo Cases:start ≤ end: [start...end]start > end: [1...end] + [start...n]Time: O(n) | Space: O(k)
Understanding the Visualization
1
Complete Rounds Cancel
Any complete lap around the track visits every sector exactly once
2
Identify Critical Path
Only the partial segments at the beginning and end create imbalance
3
Handle Circular Logic
The track wraps around, so start > end means path goes through sector 1
Key Takeaway
🎯 Key Insight: Intermediate complete rounds cancel out - only the incomplete first-to-last path determines which sectors are most visited!

Time & Space Complexity

Time Complexity
⏱️
O(n × m)

Where m is the number of rounds and n is the track size. In worst case, each round could traverse the entire track

n
2n
Linear Growth
Space Complexity
O(n)

We need an array to count visits for each of the n sectors

n
2n
Linearithmic Space

Constraints

  • 2 ≤ n ≤ 104
  • 1 ≤ rounds.length ≤ 105
  • rounds[i] will be between 1 and n
  • All rounds form a valid sequence (each round starts where the previous ended)
Asked in
Amazon 12 Google 8 Microsoft 6 Apple 4
24.5K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen