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
Visualization
Time & Space Complexity
Single loop through distances, each with constant time calculations
Only space needed for the result array
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