Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.
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 <= 1051 <= x, y <= nProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
3 1 3
5 2 4
4 1 1
walls-and-gates)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
class Solution {
public long[] countOfPairs(int n, int x, int y) {
--x;
--y;
if (x > y) {
int temp = x;
x = y;
y = temp;
}
long[] diff = new long[n];
for (int i = 0; i < n; ++i) {
diff[0] += 1 + 1;
++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)];
++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)];
--diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))];
--diff[Math.min(Math.abs(i - (n - 1)), Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))];
--diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2];
--diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2];
}
for (int i = 0; i + 1 < n; ++i) {
diff[i + 1] += diff[i];
}
return diff;
}
}
// Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
func countOfPairs(n int, x int, y int) []int64 {
if x > y {
x, y = y, x
}
A := make([]int64, n)
for i := 1; i <= n; i++ {
A[0] += 2
A[min(int64(i-1), int64(math.Abs(float64(i-y)))+int64(x))] -= 1
A[min(int64(n-i), int64(math.Abs(float64(i-x)))+1+int64(n-y))] -= 1
A[min(int64(math.Abs(float64(i-x))), int64(math.Abs(float64(y-i)))+1)] += 1
A[min(int64(math.Abs(float64(i-x)))+1, int64(math.Abs(float64(y-i))))] += 1
r := max(int64(x-i), 0) + max(int64(i-y), 0)
A[r+int64((y-x+0)/2)] -= 1
A[r+int64((y-x+1)/2)] -= 1
}
for i := 1; i < n; i++ {
A[i] += A[i-1]
}
return A
}
# Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
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
// Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
// class Solution {
// public long[] countOfPairs(int n, int x, int y) {
// --x;
// --y;
// if (x > y) {
// int temp = x;
// x = y;
// y = temp;
// }
// long[] diff = new long[n];
// for (int i = 0; i < n; ++i) {
// diff[0] += 1 + 1;
// ++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)];
// ++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)];
// --diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))];
// --diff[Math.min(Math.abs(i - (n - 1)), Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))];
// --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2];
// --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2];
// }
// for (int i = 0; i + 1 < n; ++i) {
// diff[i + 1] += diff[i];
// }
// return diff;
// }
// }
// Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3017: Count the Number of Houses at a Certain Distance II
// class Solution {
// public long[] countOfPairs(int n, int x, int y) {
// --x;
// --y;
// if (x > y) {
// int temp = x;
// x = y;
// y = temp;
// }
// long[] diff = new long[n];
// for (int i = 0; i < n; ++i) {
// diff[0] += 1 + 1;
// ++diff[Math.min(Math.abs(i - x), Math.abs(i - y) + 1)];
// ++diff[Math.min(Math.abs(i - y), Math.abs(i - x) + 1)];
// --diff[Math.min(Math.abs(i - 0), Math.abs(i - y) + 1 + Math.abs(x - 0))];
// --diff[Math.min(Math.abs(i - (n - 1)), Math.abs(i - x) + 1 + Math.abs(y - (n - 1)))];
// --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 0) / 2];
// --diff[Math.max(x - i, 0) + Math.max(i - y, 0) + ((y - x) + 1) / 2];
// }
// for (int i = 0; i + 1 < n; ++i) {
// diff[i + 1] += diff[i];
// }
// return diff;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.