Count the number of index pairs whose sum lies within an inclusive range.
Given an integer array nums and two integers lower and upper, count the number of fair pairs of indices (i, j) such that:
0 <= i < j < nums.lengthlower <= nums[i] + nums[j] <= upperReturn the total number of such pairs.
A pair is considered fair if its sum falls within the inclusive range [lower, upper].
numslower and upper(i, j) with i < j and lower <= nums[i] + nums[j] <= upper.1 <= nums.lengthlower <= upperExample 1
Input
nums = [0,1,7,4,4,5], lower = 3, upper = 6
Output
6
Explanation
The fair pairs are the index pairs whose sums are 3, 4, 5, or 6. There are 6 such pairs in total.
Example 2
Input
nums = [1,7,9,2,5], lower = 11, upper = 11
Output
1
Explanation
Only one pair sums to exactly 11: (2, 9).
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.