Trains the technique from
LeetCode 2563Count the Number of Fair PairsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Readings are given as nums, along with bounds lower and upper.
A pair of positions i before j is in range when nums[i] + nums[j] is at least lower and at most upper.
Return how many pairs are in range.
Example 1
Sorted, the readings run 3, 9, 14, 27, 41. The pairs totalling between 20 and 40 are 3 and 27, 9 and 27, 14 and 27, and 3 and 41... of those the last totals 44, so it does not count, leaving the three that do.
Example 2
Every pair totals nothing, and there are six pairs among four readings.
Example 3
No pair totals anywhere near the range.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_fair_pairs(nums: list[int], lower: int, upper: int) -> int:public long countFairPairs(int[] nums, int lower, int upper)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.