Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs. A pair (i, j) is fair if 0 <= i < j < n and lower <= nums[i] + nums[j] <= upper. The answer can exceed 2^31, so it is returned as a 64-bit integer.
Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs. A pair (i, j) is fair if 0 <= i < j < n and lower <= nums[i] + nums[j] <= upper. The answer can exceed 2^31, so it is returned as a 64-bit integer.
Example 1:
Input: {"nums":[0,1,7,4,4,5],"lower":3,"upper":6}
Output: 6
Constraints:
1≤nums.length≤105
nums.length=n
−109≤nums[i]≤109
−109≤lower≤upper≤109
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[0,1,7,4,4,5],"lower":3,"upper":6}
Sort nums. A pair only cares about its two values, not their positions, so sorting keeps the answer and lets two pointers count whole blocks of pairs at once.