# 1885. Count Pairs in Two Arrays ###### tags: `Leetcode` `Medium` `Two Sum` Link: https://leetcode.com/problems/count-pairs-in-two-arrays/description/ ## 思路 先计算出所有位置```nums1```和```nums2```的差放进```nums1```里面 然后找到所有使得```nums1[i]+nums1[j]>0```的combination 找的方法是重点!! 先sort然后再two pointers ## Code ```java= class Solution { public long countPairs(int[] nums1, int[] nums2) { int n = nums1.length; for(int i=0; i<n; i++){ nums1[i] -= nums2[i]; } Arrays.sort(nums1); int l = 0, r = nums1.length-1; long ans = 0; while(l<r){ int sum = nums1[l]+nums1[r]; if(sum > 0){ //all the combination of nums1[l], ... nums1[r-1] & nums1[r] ans += r-l; r--; } else l++; } return ans; } } ```