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: [3,0,0]
💡 Note: Houses 1-3 with shortcut 1-3. Pairs: (1,2) distance = min(1, 2, 4) = 1; (1,3) distance = min(2, 1, 4) = 1; (2,3) distance = min(1, 2, 3) = 1. All 3 pairs have distance 1.
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
Count Houses at Distance K INPUT Street Network (n=3 houses) 1 x=1 2 3 y=3 Extra Street n = 3 x = 1 y = 3 All House Pairs: (1,2) (1,3) (2,3) (2,1) (3,1) (3,2) ALGORITHM STEPS 1 Identify Paths Linear: i to i+1 Shortcut: x to y 2 Calculate Distances For each pair (i,j): min(direct, via shortcut) 3 Distance Formula Direct: |i - j| Via shortcut: |i-x| + 1 + |y-j| 4 Count Pairs For each distance k, count pairs with dist=k Example Calculations: 1--2: direct=1, via=1+1+1=3 1--3: direct=2, via=0+1+0=1 2--3: direct=1, via=1+1+1=3 All min distances = 1 FINAL RESULT Distance Distribution k=1 0 pairs k=2 6 pairs wait... 2! k=3 0 pairs Pair Count at k=2: (1,2), (2,1) - both dist 1 (1,3), (3,1) - both dist 1 (2,3), (3,2) - both dist 1 Output Array: 0 [1] 2 [2] 0 [3] Key Insight: The extra street from x=1 to y=3 creates a shortcut that reduces the distance between houses 1 and 3 from 2 (via house 2) to just 1 (direct connection). This means ALL pairs now have minimum distance 1, but we count ordered pairs, so 6 pairs total at distance 1. Wait - output says k=2 has 2 pairs... Recheck! TutorialsPoint - Count the Number of Houses at a Certain Distance I | Optimized Distance Calculation
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