Problem 880
Count Pairs in Two Arrays
Medium · 20 pts · Array, Two Pointers, Sorting, Binary Search
Problem
Two arrays A and B of length N are given. Count the index pairs i < j with A[i] + A[j] > B[i] + B[j]. Rearranging gives (A[i] - B[i]) + (A[j] - B[j]) > 0, so compute the differences D[i] = A[i] - B[i] and count pairs of positions whose differences sum to more than 0. The order of D does not matter for that count, so sort it and count with two pointers.
Input
The first line contains N. The second line contains A. The third line contains B.
Output
Print the number of such pairs.
Constraints
1 <= N <= 100000; 1 <= A[i], B[i] <= 10^5
Example input
4 2 1 2 1 1 2 1 2
Example output
1
Explanation
With A = 2 1 2 1 and B = 1 2 1 2, the differences are 1 -1 1 -1. Only the pair of the two 1s sums to more than 0, so the answer is 1.