Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 1.46 KB

_1885. Count Pairs in Two Arrays.md

File metadata and controls

53 lines (39 loc) · 1.46 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 08, 2024

Last updated : July 04, 2024


Related Topics : Array, Two Pointers, Binary Search, Sorting

Acceptance Rate : 60.1 %


```
    Idea:
    nums1[i] + nums1[j] > nums2[i] + nums2[j]
    ==> nums1[i] - nums2[i] + nums1[j] - nums2[j] > 0
    so we can find the difference array first
    ==> newNums[i] + newNums[j] > 0
    then find how many pairs of values are positive. This would be max n^2 + n?
```

Solutions

Python

class Solution:
    def countPairs(self, nums1: List[int], nums2: List[int]) -> int:
        newNums = sorted([nums1[x] - nums2[x] for x in range(len(nums1))])

        left, right = 0, len(newNums) - 1
        counter = 0
        while (left < right) :
            if (newNums[left] + newNums[right] > 0) :
                counter += right - left
                right -= 1
            else :
                left += 1
        return counter