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 two 0-indexed integer arrays nums1 and nums2, each of size n, and an integer diff. Find the number of pairs (i, j) such that:
0 <= i < j <= n - 1 andnums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff.Return the number of pairs that satisfy the conditions.
Example 1:
Input: nums1 = [3,2,5], nums2 = [2,2,1], diff = 1 Output: 3 Explanation: There are 3 pairs that satisfy the conditions: 1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions. 2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions. 3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions. Therefore, we return 3.
Example 2:
Input: nums1 = [3,-1], nums2 = [-2,2], diff = -1 Output: 0 Explanation: Since there does not exist any pair that satisfies the conditions, we return 0.
Constraints:
n == nums1.length == nums2.length2 <= n <= 105-104 <= nums1[i], nums2[i] <= 104-104 <= diff <= 104Problem summary: You are given two 0-indexed integer arrays nums1 and nums2, each of size n, and an integer diff. Find the number of pairs (i, j) such that: 0 <= i < j <= n - 1 and nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff. Return the number of pairs that satisfy the conditions.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Segment Tree
[3,2,5] [2,2,1] 1
[3,-1] [-2,2] -1
k-diff-pairs-in-an-array)count-nice-pairs-in-an-array)count-number-of-bad-pairs)maximum-balanced-subsequence-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2426: Number of Pairs Satisfying Inequality
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public static final int lowbit(int x) {
return x & -x;
}
public void update(int x, int delta) {
while (x <= n) {
c[x] += delta;
x += lowbit(x);
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= lowbit(x);
}
return s;
}
}
class Solution {
public long numberOfPairs(int[] nums1, int[] nums2, int diff) {
BinaryIndexedTree tree = new BinaryIndexedTree(100000);
long ans = 0;
for (int i = 0; i < nums1.length; ++i) {
int v = nums1[i] - nums2[i];
ans += tree.query(v + diff + 40000);
tree.update(v + 40000, 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2426: Number of Pairs Satisfying Inequality
type BinaryIndexedTree struct {
n int
c []int
}
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
c := make([]int, n+1)
return &BinaryIndexedTree{n, c}
}
func (this *BinaryIndexedTree) lowbit(x int) int {
return x & -x
}
func (this *BinaryIndexedTree) update(x, delta int) {
for x <= this.n {
this.c[x] += delta
x += this.lowbit(x)
}
}
func (this *BinaryIndexedTree) query(x int) int {
s := 0
for x > 0 {
s += this.c[x]
x -= this.lowbit(x)
}
return s
}
func numberOfPairs(nums1 []int, nums2 []int, diff int) int64 {
tree := newBinaryIndexedTree(100000)
ans := 0
for i := range nums1 {
v := nums1[i] - nums2[i]
ans += tree.query(v + diff + 40000)
tree.update(v+40000, 1)
}
return int64(ans)
}
# Accepted solution for LeetCode #2426: Number of Pairs Satisfying Inequality
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
@staticmethod
def lowbit(x):
return x & -x
def update(self, x, delta):
while x <= self.n:
self.c[x] += delta
x += BinaryIndexedTree.lowbit(x)
def query(self, x):
s = 0
while x:
s += self.c[x]
x -= BinaryIndexedTree.lowbit(x)
return s
class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
tree = BinaryIndexedTree(10**5)
ans = 0
for a, b in zip(nums1, nums2):
v = a - b
ans += tree.query(v + diff + 40000)
tree.update(v + 40000, 1)
return ans
// Accepted solution for LeetCode #2426: Number of Pairs Satisfying Inequality
// 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 #2426: Number of Pairs Satisfying Inequality
// class BinaryIndexedTree {
// private int n;
// private int[] c;
//
// public BinaryIndexedTree(int n) {
// this.n = n;
// c = new int[n + 1];
// }
//
// public static final int lowbit(int x) {
// return x & -x;
// }
//
// public void update(int x, int delta) {
// while (x <= n) {
// c[x] += delta;
// x += lowbit(x);
// }
// }
//
// public int query(int x) {
// int s = 0;
// while (x > 0) {
// s += c[x];
// x -= lowbit(x);
// }
// return s;
// }
// }
//
// class Solution {
// public long numberOfPairs(int[] nums1, int[] nums2, int diff) {
// BinaryIndexedTree tree = new BinaryIndexedTree(100000);
// long ans = 0;
// for (int i = 0; i < nums1.length; ++i) {
// int v = nums1[i] - nums2[i];
// ans += tree.query(v + diff + 40000);
// tree.update(v + 40000, 1);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2426: Number of Pairs Satisfying Inequality
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2426: Number of Pairs Satisfying Inequality
// class BinaryIndexedTree {
// private int n;
// private int[] c;
//
// public BinaryIndexedTree(int n) {
// this.n = n;
// c = new int[n + 1];
// }
//
// public static final int lowbit(int x) {
// return x & -x;
// }
//
// public void update(int x, int delta) {
// while (x <= n) {
// c[x] += delta;
// x += lowbit(x);
// }
// }
//
// public int query(int x) {
// int s = 0;
// while (x > 0) {
// s += c[x];
// x -= lowbit(x);
// }
// return s;
// }
// }
//
// class Solution {
// public long numberOfPairs(int[] nums1, int[] nums2, int diff) {
// BinaryIndexedTree tree = new BinaryIndexedTree(100000);
// long ans = 0;
// for (int i = 0; i < nums1.length; ++i) {
// int v = nums1[i] - nums2[i];
// ans += tree.query(v + diff + 40000);
// tree.update(v + 40000, 1);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.