Count the Number of Houses at a Certain Distance II Problem
Description
LeetCode Problem 3017.
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 (house_1, house_2) such that the minimum number of streets that need to be traveled to reach house_2 from house_1 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:

Input: n = 3, x = 1, y = 3
Output: [6,0,0]
Explanation: Let's look at each pair of houses:
- For the pair (1, 2), we can go from house 1 to house 2 directly.
- For the pair (2, 1), we can go from house 2 to house 1 directly.
- For the pair (1, 3), we can go from house 1 to house 3 directly.
- For the pair (3, 1), we can go from house 3 to house 1 directly.
- For the pair (2, 3), we can go from house 2 to house 3 directly.
- For the pair (3, 2), we can go from house 3 to house 2 directly.
Example 2:

Input: n = 5, x = 2, y = 4
Output: [10,8,2,0,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
- For k == 3, the pairs are (1, 5), and (5, 1).
- For k == 4 and k == 5, there are no pairs.
Example 3:

Input: n = 4, x = 1, y = 1
Output: [6,4,2,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
- For k == 3, the pairs are (1, 4), and (4, 1).
- For k == 4, there are no pairs.
Constraints:
2 <= n <= 10^51 <= x, y <= n
Solution
Approach: Path, Cycle and Tails in Closed Form
With n up to 10^5, checking every pair is too slow, so the answer must be built from counts.
The town is a path plus one extra road between x and y.
- If
|x - y| <= 1, the extra road shortens nothing, and the town is just a path: there are2 * (n - d)ordered pairs at distanced. - Otherwise the extra road creates a cycle of length
|x - y| + 1, with a tail of houses hanging off each side. Add up three groups of pairs: the pairs on the path that results from replacing the cycle by a single road, the extra pairs inside the cycle (where distances wrap around, so each distance up to half the cycle length occurs a fixed number of times, with the even-length case needing care at the largest distance), and the pairs between each tail house and the cycle houses, whose counts also form a ramp-up, plateau and ramp-down.
Each group fills a range of the answer array with a pattern of simple arithmetic progressions, so every group is added in time proportional to its range.
Complexity: O(n) time and O(n) space for the answer.
class Solution:
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
if abs(x - y) <= 1:
return [2 * x for x in reversed(range(n))]
cycle_len = abs(x - y) + 1
n2 = n - cycle_len + 2
res = [2 * x for x in reversed(range(n2))]
while len(res) < n:
res.append(0)
res2 = [cycle_len * 2] * (cycle_len >> 1)
if not cycle_len & 1:
res2[-1] = cycle_len
res2[0] -= 2
for i in range(len(res2)):
res[i] += res2[i]
if x > y:
x, y = y, x
tail1 = x - 1
tail2 = n - y
for tail in (tail1, tail2):
if not tail:
continue
i_mx = tail + (cycle_len >> 1)
val_mx = 4 * min((cycle_len - 3) >> 1, tail)
i_mx2 = i_mx - (1 - (cycle_len & 1))
res3 = [val_mx] * i_mx
res3[0] = 0
res3[1] = 0
if not cycle_len & 1:
res3[-1] = 0
for i, j in enumerate(range(4, val_mx, 4)):
res3[i + 2] = j
res3[i_mx2 - i - 1] = j
for i in range(1, tail + 1):
res3[i] += 2
if not cycle_len & 1:
mn = cycle_len >> 1
for i in range(mn, mn + tail):
res3[i] += 2
for i in range(len(res3)):
res[i] += res3[i]
return res