1560. Most Visited Sector in a Circular Track LeetCode Solution
In this guide, you will get 1560. Most Visited Sector in a Circular Track LeetCode Solution with the best time and space complexity. The solution to Most Visited Sector in a Circular Track problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Most Visited Sector in a Circular Track solution in C++
- Most Visited Sector in a Circular Track solution in Java
- Most Visited Sector in a Circular Track solution in Python
- Additional Resources

Problem Statement of Most Visited Sector in a Circular Track
Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of m rounds. The ith round starts at sector rounds[i – 1] and ends at sector rounds[i]. For example, round 1 starts at sector rounds[0] and ends at sector rounds[1]
Return an array of the most visited sectors sorted in ascending order.
Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).
Example 1:
Input: n = 4, rounds = [1,3,1,2]
Output: [1,2]
Explanation: The marathon starts at sector 1. The order of the visited sectors is as follows:
1 –> 2 –> 3 (end of round 1) –> 4 –> 1 (end of round 2) –> 2 (end of round 3 and the marathon)
We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.
Example 2:
Input: n = 2, rounds = [2,1,2,1,2,1,2,1,2]
Output: [2]
Example 3:
Input: n = 7, rounds = [1,3,5,7]
Output: [1,2,3,4,5,6,7]
Constraints:
2 <= n <= 100
1 <= m <= 100
rounds.length == m + 1
1 <= rounds[i] <= n
rounds[i] != rounds[i + 1] for 0 <= i < m
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1560. Most Visited Sector in a Circular Track LeetCode Solution in C++
class Solution {
public:
vector<int> mostVisited(int n, vector<int>& rounds) {
// 1. If start <= end, [start, end] is the most visited.
//
// s --------- n
// 1 -------------- n
// 1 ------ e
//
// 2. If start > end, [1, end] and [start, n] are the most visited.
//
// s -- n
// 1 -------------- n
// 1 ------ e
const int start = rounds.front();
const int end = rounds.back();
vector<int> ans;
for (int i = 1; i <= n; ++i)
if (start <= end) {
if (start <= i && i <= end)
ans.push_back(i);
} else { // start > end
if (i >= start || i <= end)
ans.push_back(i);
}
return ans;
}
};
/* code provided by PROGIEZ */
1560. Most Visited Sector in a Circular Track LeetCode Solution in Java
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
// 1. If start <= end, [start, end] is the most visited.
//
// s --------- n
// 1 -------------- n
// 1 ------ e
//
// 2. If start > end, [1, end] and [start, n] are the most visited.
//
// s -- n
// 1 -------------- n
// 1 ------ e
final int start = rounds[0];
final int end = rounds[rounds.length - 1];
List<Integer> ans = new ArrayList<>();
for (int i = 1; i <= n; ++i)
if (start <= end) {
if (start <= i && i <= end)
ans.add(i);
} else { // start > end
if (i >= start || i <= end)
ans.add(i);
}
return ans;
}
}
// code provided by PROGIEZ
1560. Most Visited Sector in a Circular Track LeetCode Solution in Python
class Solution:
def mostVisited(self, n: int, rounds: list[int]) -> list[int]:
# 1. if start <= end, [start, end] is the most visited.
#
# s --------- n
# 1 -------------- n
# 1 ------ e
#
# 2. if start > end, [1, end] and [start, n] are the most visited.
#
# s -- n
# 1 -------------- n
# 1 ------ e
start = rounds[0]
end = rounds[-1]
if start <= end:
return range(start, end + 1)
return list(range(1, end + 1)) + list(range(start, n + 1))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.