Topics
Given two arrays and , both of size , find the number of “good pairs”. A pair is considered “good” if and .
Idea
Using basic arithmetic, transform the condition to:
Compute a new array diff where diff[i] = A[i] - B[i]. The problem now reduces to finding pairs such that diff[i] + diff[j] > 0 and .
We can solve above using two pointers at opposite ends technique (sort diff first). Use two pointers, l and r, starting from the beginning and end of the sorted diff array, respectively. If diff[l] + diff[r] > 0, then all pairs (l, r), (l+1, r), ..., (r-1, r) are good pairs. Increment the count by r - l and decrement r (since we accounted for all pairs that end at r. This is akin to contribution technique). If diff[l] + diff[r] <= 0, then increment l to find a larger value that might satisfy the condition.
Time Complexity: for diff array, sorting and 2 pointers respectively.
Space Complexity: for diff array
Alternative approach
Binary Search (Less Efficient): For each element diff[i], you could use binary search to find the number of elements diff[j] such that diff[j] > -diff[i] and .
ll pairs_with_sum_larger_than_0(const vector<int> &arr) {
int n = arr.size();
ll count = 0;
for (int i = 0; i < n; ++i) {
// Find the number of elements greater than -arr[i] in the subarray
// arr[i+1...n-1] Use upper_bound to find the first element > -arr[i]
auto it = upper_bound(arr.begin() + i + 1, arr.end(), -arr[i]);
count += distance(it, arr.end());
}
return count;
}Code
ll pairs_with_sum_larger_than_0(const vector<int> &arr) {
int n = arr.size();
int l = 0;
int r = n - 1;
int count = 0;
while (l < r) {
// arr[l] + arr[r] <= 0 (reorganized to avoid overflow)
if (arr[l] <= -arr[r]) {
++l;
} else {
count += r - l;
--r;
}
}
return count;
}
void solve() {
int n, x;
cin >> n;
vector<int> diff(n);
for (int i = 0; i < n; ++i)
cin >> diff[i];
for (int i = 0; i < n; ++i) {
cin >> x;
diff[i] -= x;
}
sort(all(diff));
cout << pairs_with_sum_larger_than_0(diff) << "\n";
}