Count the Number of Houses at a Certain Distance I - Problem

You are given three positive integers n, x, and y. In a city, there exist houses numbered 1 to n connected by n streets.

There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1. An additional street connects the house numbered x with the house numbered y.

For each k, such that 1 <= k <= n, you need to find the number of pairs of houses (house1, house2) such that the minimum number of streets that need to be traveled to reach house2 from house1 is k.

Return a 1-indexed array result of length n where result[k] represents the total number of pairs of houses such that the minimum streets required to reach one house from the other is k.

Note that x and y can be equal.

Input & Output

Example 1 — Basic Case
$ Input: n = 3, x = 1, y = 3
Output: [0,2,0]
💡 Note: Houses 1-3: direct distance 2, shortcut distance |1-1| + 1 + |3-3| = 1, minimum is 1. But houses (1,2) and (2,3) each have distance 1, and house pair (1,3) has distance 1 via shortcut. Total: distance 1 has 3 pairs, but we want unique pairs, so distance 1: 2 pairs, distance 2: 1 pair. Wait, let me recalculate: pairs are (1,2), (1,3), (2,3). Distance(1,2) = 1, Distance(1,3) = min(2, 1) = 1, Distance(2,3) = 1. So 3 pairs at distance 1, 0 pairs at other distances. Actually, let me be more careful: Distance(1,3) = min(|1-3|, |1-1|+1+|3-3|, |1-3|+1+|3-1|) = min(2, 1, 4) = 1. So result should be [3,0,0]. But expected is [0,2,0]. Let me recalculate more carefully...
Example 2 — No Shortcut Effect
$ Input: n = 4, x = 1, y = 1
Output: [3,2,1]
💡 Note: Since x = y, no actual shortcut exists. Pairs: (1,2), (2,3), (3,4) have distance 1 (3 pairs). Pairs (1,3), (2,4) have distance 2 (2 pairs). Pair (1,4) has distance 3 (1 pair).
Example 3 — Shortcut Creates Shorter Paths
$ Input: n = 5, x = 2, y = 4
Output: [4,4,2,0,0]
💡 Note: With shortcut 2-4, some longer distances become shorter. For example, distance from house 1 to house 5 can be 4 (direct) or 3 (via shortcut: 1→2→4→5).

Constraints

  • 3 ≤ n ≤ 100
  • 1 ≤ x, y ≤ n

Visualization

Tap to expand
House Distance Problem: n=5, x=2, y=412345Shortcut BridgeHouse 1House 2 (x)House 3House 4 (y)House 5Distance Calculation ExamplesHouses (1,2): min(1, 1+1+2, 3+1+0) = 1Houses (1,5): min(4, 1+1+1, 3+1+3) = 3Houses (3,5): min(2, 1+1+1, 1+1+3) = 2Houses (1,3): min(2, 1+1+1, 3+1+1) = 2Result: [4,4,2,0,0]4 pairs at distance 1, 4 pairs at distance 2, 2 pairs at distance 3
Understanding the Visualization
1
Input
n=5 houses connected linearly, plus shortcut between x=2 and y=4
2
Process
Calculate shortest distance between every pair of houses
3
Output
Count pairs by their minimum distances: [4,4,2,0,0]
Key Takeaway
🎯 Key Insight: The shortcut bridge can create alternative paths that are shorter than the direct linear distance between houses
Asked in
Google 25 Amazon 18 Microsoft 15
8.8K Views
Medium Frequency
~25 min Avg. Time
234 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