Find the City With the Smallest Number of Neighbors at a Threshold Distance
Start with d[i][j] = the road cost (0 on the diagonal, ∞ if there is no road). Then for each city k in turn, try k as a middle stop for every pair: d[i][j] = min(d[i][j], d[i][k] + d[k][j]). After all the k rounds, d holds every shortest distance. Then count, for each city, the others within the threshold, and keep the fewest. On a tie keep the greater city.
After the round for k, d[i][j] is the shortest distance from i to j whose middle cities all lie in 0..k, for every pair (i, j).
Problem
There are n cities, numbered 0 to n − 1, joined by two-way roads. Each road is [a, b, w]: it links cities a and b, and travelling it costs w.
The distance between two cities is the cost of the cheapest route between them. For each city, count the other cities whose distance is at most distanceThreshold. Return the city with the fewest such cities. If several cities tie, return the one with the greatest number.
n, edges (a list of [a, b, w]) and distanceThreshold.
The number of the city with the fewest neighbours within the threshold. A tie goes to the greater city number.
City 0 has 2 neighbours within 4 (cities 1 and 2). City 1 has 3, city 2 has 3, and city 3 has 2 (cities 1 and 2). Cities 0 and 3 tie at 2, so we return the greater number, 3.
City 0 has only one neighbour within 2 (city 1, at distance 2). Every other city has at least two.
- • 2 ≤ n ≤ 100
- • 1 ≤ edges.length ≤ n · (n − 1) / 2
- • edges[i].length == 3
- • 0 ≤ from < to < n
- • 1 ≤ weight, distanceThreshold ≤ 10^4
- • All pairs (from, to) are distinct.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
The count for each city depends on its distance to every other city. Do I need one search per city, or can I get every distance in one go?
- 2.
If I run one shortest-path search from each city, how many searches do I run, and how many roads does each one scan?
- 3.
When a route passes through city k, which two shorter routes does it join? Do those two routes need to be finished first?
- 4.
Two cities can tie on the count. Which one does the statement pick, and how do I make the loop pick it without sorting?
- 5.
Which of the three loops has to be the outermost one, and what does that loop mean?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Run Bellman-Ford from each city, count the cities within the threshold, keep the fewest | O(N² · E) | O(N) |
| Optimal | All-pairs distances in O(N³) with the middle city as the outer loop, then count per city | O(N³) | O(N²) |
Why does this belong to this pattern?
- “for each city, count the cities within the threshold”Distances between every pair are needed, not from one start. That points to all-pairs shortest paths.
- “n ≤ 100”N³ is about 10^6 steps, which is fast, so Floyd-Warshall is affordable.
- “fewest, and the greatest number on a tie”Compare with <= while scanning the cities in order, so the later city wins a tie.
The distance is the same sum of road costs as in Dijkstra. What is new is that every pair is needed, so one run from one source is not enough. Floyd-Warshall gives all the distances in O(N³), and then one counting pass per city answers the question. The middle-city loop must be outermost: after round k, d[i][j] only covers middle cities 0..k, so putting the loop inside would give wrong distances.
Don't make this mistake
This is the pattern-confusion trap: the usual 'keep the first best' habit. On example 1 the counts are [2, 3, 3, 2]. With < the first city with 2 wins, which is city 0. The statement asks for the greater number, so the answer is 3.
Take the roads 0–1 (1), 1–3 (5) and 2–3 (3). The true distance from 0 to 2 is 9 (0 → 1 → 3 → 2). With i, j, k order, when i = 0 and j = 2 is checked, d[0][3] is still ∞, because it is only set later when j = 3 runs. So d[0][2] stays ∞. Example 1 still comes out right, which is why this bug is easy to miss.
The statement says 'at most'. On example 1, d[0][2] = 4 is exactly the threshold. With <, city 0 has only 1 neighbour, so the answer becomes 0 instead of 3.
INT_MAX + INT_MAX does not fit in an int. In C++ that is undefined behaviour, and in practice the sum wraps to a negative number, which looks shorter than any real route. Halving INF keeps every sum safe.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Shortest Paths (Weighted) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.