Number of Pairs Satisfying Inequality

You are given two 0-indexed integer arrays nums1 and nums2, each of size n, and an integer diff. Find the number of pairs (i, j) such that:

  • 0 <= i < j <= n - 1 and
  • nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff.

Return the number of pairs that satisfy the conditions.

Example 1
Inputnums1 = [3,2,5], nums2 = [2,2,1], diff = 1
Output3
There are 3 pairs that satisfy the conditions: (0, 1), (0, 2), and (1, 2).
Example 2
Inputnums1 = [3,-1], nums2 = [-2,2], diff = -1
Output0
Since there does not exist any pair that satisfies the conditions, we return 0.

Constraints

  • n == nums1.length == nums2.length
  • 2 <= n <= 10^5
  • -10^4 <= nums1[i], nums2[i] <= 10^4
  • -10^4 <= diff <= 10^4

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate