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

Imagine you're a city planner analyzing the transportation network of a linear city! ๐Ÿ˜๏ธ

You have a city with n houses numbered from 1 to n, arranged in a straight line. Each house i is connected to house i+1 by a street, forming a linear chain. However, there's also a special shortcut street that directly connects house x to house y, potentially creating faster routes between certain houses.

Your task is to analyze the transportation efficiency: for each possible distance k (where 1 โ‰ค k โ‰ค n), count how many pairs of houses are exactly k streets apart when taking the shortest possible route.

Goal: Return an array where result[k] represents the number of house pairs that are exactly k streets apart via the shortest path.

Note: The shortcut street can be used in both directions, and houses x and y can be the same (creating a loop).

Input & Output

example_1.py โ€” Basic Linear City
$ Input: n = 3, x = 1, y = 3
โ€บ Output: [6, 0, 0]
๐Ÿ’ก Note: With houses 1-2-3 and shortcut 1โ†”3: distance 1 has pairs (1,2), (2,3), (1,3) via shortcut, (3,1) via shortcut, (2,1), (3,2) = 6 pairs. No pairs at distances 2 or 3.
example_2.py โ€” Medium City
$ Input: n = 5, x = 2, y = 4
โ€บ Output: [10, 8, 2, 0, 0]
๐Ÿ’ก Note: Houses 1-2-3-4-5 with shortcut 2โ†”4. Distance 1: adjacent pairs plus some using shortcut. Distance 2: pairs that are 2 apart via optimal path. Distance 3: only (1,4) and (4,1) via direct path.
example_3.py โ€” Same House Shortcut
$ Input: n = 4, x = 2, y = 2
โ€บ Output: [6, 4, 2, 0]
๐Ÿ’ก Note: Shortcut creates a loop at house 2, but doesn't change distances between different houses. Standard linear arrangement applies.

Visualization

Tap to expand
City Transportation Network1x3y56Express TrackLeft RegionHouses 1 to x-1May use expressExpress ZoneHouses x to yDirect connectionRight RegionHouses y+1 to nMay use expressDistance AnalysisDistance 1: Adjacent + Express shortcuts = High countDistance 2: Medium range + Some express benefitsDistance 3+: Long range, express creates alternativesMathematical formula calculates each category directly!
Understanding the Visualization
1
Map the Network
Identify the linear chain and shortcut connection
2
Analyze Regions
Divide into regions: left of shortcut, shortcut span, right of shortcut
3
Count Base Pairs
Calculate pairs at each distance without using shortcut
4
Apply Shortcut Benefits
Adjust counts based on which pairs benefit from the shortcut
Key Takeaway
๐ŸŽฏ Key Insight: Rather than computing O(nยฒ) shortest paths, we can mathematically analyze how the shortcut affects distance distribution, achieving optimal O(n) performance through regional analysis and formula-based counting.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single loop through distances, each with constant time calculations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Only space needed for the result array

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค x, y โ‰ค n
  • x and y can be equal (creating a self-loop)
  • The result array should have exactly n elements
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
27.4K Views
Medium Frequency
~25 min Avg. Time
892 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