Count Pairs in Two Arrays

Medium

Description

Given two arrays nums1 and nums2 of equal length, count pairs (i, j) where i < j and nums1[i] + nums1[j] > nums2[i] + nums2[j].

Examples

Input:nums1 = [2,1,2,1], nums2 = [1,2,1,2]
Output:1
Explanation:

Only pair (0,2).

Input:nums1 = [5,3,8], nums2 = [2,7,1]
Output:2
Explanation:

Checking all pairs (i,j) where i < j and nums1[i]+nums1[j] > nums2[i]+nums2[j]: (0,1) 5+3=8 vs 2+7=9 false; (0,2) 5+8=13 vs 2+1=3 true; (1,2) 3+8=11 vs 7+1=8 true. Two valid pairs.

Input:nums1 = [1,1,1], nums2 = [2,2,2]
Output:0
Explanation:

All elements in nums1 are 1 and all elements in nums2 are 2. For any pair (i,j), this gives nums1[i] + nums1[j] = 1+1 = 2 and nums2[i] + nums2[j] = 2+2 = 4. Since 2 > 4 is always false, no pairs satisfy the condition.

Constraints

  • 1 ≤ nums1.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!