Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Sorting
Two Pointers
Amazon
Count The Number Of Fair Pairs
Count the number of index pairs whose sum lies within an inclusive range.
Acceptance 0%
Problem Statement
Problem
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] <= upper
Return the total number of such pairs.
A pair is considered fair if its sum falls within the inclusive range [lower, upper].
Notes
- Each index may be used in at most one pair occurrence, but the same value can appear multiple times at different indices.
- Count pairs by index, not by value.
- The answer may be large, so use an integer type that can hold the full count.
Input Format
- An integer array
nums - Two integers
lowerandupper
Output Format
- Return one integer: the number of pairs
(i, j)withi < jandlower <= nums[i] + nums[j] <= upper.
Constraints
1 <= nums.length- Pair counting is based on indices, not distinct values
lower <= upper- Values may be negative, zero, or positive
- The result may exceed 32-bit integer range
Examples
Sample cases returned by the problem API.
Example 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
Unlock deeper context for this problem
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.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.