Count the Number of Houses at a Certain Distance I

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, 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.

Example 1
Inputn = 3, x = 1, y = 3
Output[6,0,0]
Every ordered pair of distinct houses has minimum distance 1, so the first count is 6 and all other counts are 0.
Example 2
Inputn = 5, x = 2, y = 4
Output[10,8,2,0,0]
The counts of pairs at distances 1 through 5 are 10, 8, 2, 0, and 0 respectively.

Constraints

  • 2 <= n <= 100
  • 1 <= x, y <= n

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate