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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
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 <= 1001 <= 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 #3015: Count the Number of Houses at a Certain Distance I
class Solution {
public int[] countOfPairs(int n, int x, int y) {
int[] ans = new int[n];
x--;
y--;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int a = j - i;
int b = Math.abs(i - x) + 1 + Math.abs(j - y);
int c = Math.abs(i - y) + 1 + Math.abs(j - x);
ans[Math.min(a, Math.min(b, c)) - 1] += 2;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3015: Count the Number of Houses at a Certain Distance I
func countOfPairs(n int, x int, y int) []int {
ans := make([]int, n)
x, y = x-1, y-1
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
a := j - i
b := abs(x-i) + abs(y-j) + 1
c := abs(x-j) + abs(y-i) + 1
ans[min(a, min(b, c))-1] += 2
}
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3015: Count the Number of Houses at a Certain Distance I
class Solution:
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
x, y = x - 1, y - 1
ans = [0] * n
for i in range(n):
for j in range(i + 1, n):
a = j - i
b = abs(i - x) + 1 + abs(j - y)
c = abs(i - y) + 1 + abs(j - x)
ans[min(a, b, c) - 1] += 2
return ans
// Accepted solution for LeetCode #3015: Count the Number of Houses at a Certain Distance I
// 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 #3015: Count the Number of Houses at a Certain Distance I
// class Solution {
// public int[] countOfPairs(int n, int x, int y) {
// int[] ans = new int[n];
// x--;
// y--;
// for (int i = 0; i < n; ++i) {
// for (int j = i + 1; j < n; ++j) {
// int a = j - i;
// int b = Math.abs(i - x) + 1 + Math.abs(j - y);
// int c = Math.abs(i - y) + 1 + Math.abs(j - x);
// ans[Math.min(a, Math.min(b, c)) - 1] += 2;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3015: Count the Number of Houses at a Certain Distance I
function countOfPairs(n: number, x: number, y: number): number[] {
const ans: number[] = Array(n).fill(0);
x--;
y--;
for (let i = 0; i < n; ++i) {
for (let j = i + 1; j < n; ++j) {
const a = j - i;
const b = Math.abs(x - i) + Math.abs(y - j) + 1;
const c = Math.abs(y - i) + Math.abs(x - j) + 1;
ans[Math.min(a, b, c) - 1] += 2;
}
}
return ans;
}
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.