Count Pairs Whose Sum is Less than Target (LeetCode 2824): Solution and Explanation
Count index pairs i < j with nums[i] + nums[j] < target. Sorting lets one comparison settle many pairs at once.
Intuition
The count of pairs depends only on the multiset of values, so sorting does not change the answer.
In sorted order, if nums[lo] + nums[hi] < target, every value between lo and hi is at most nums[hi], so nums[lo] pairs with all hi - lo of them.
If the sum is too big, nums[hi] fails even with the smallest remaining value, so it fails with every remaining partner and can be dropped.
Approach: Sort and two pointers
- Sort nums. Set lo = 0, hi = n - 1, count = 0.
- While lo < hi: if nums[lo] + nums[hi] < target, add hi - lo to count and move lo right.
- Otherwise move hi left.
- Return count.
Step-by-step example
nums = [-1,1,2,3,1], target = 2
- Sorted: [-1,1,1,2,3].
- -1 + 3 = 2 is not below 2, so drop 3.
- -1 + 2 = 1 < 2, so -1 pairs with 1, 1 and 2: count = 3.
- 1 + 2 = 3 is not below 2, drop 2. 1 + 1 = 2 is not below 2, drop 1. lo meets hi.
- Answer 3.
Code: Python, Java, C++
1class Solution:
2 def countPairs(self, nums: List[int], target: int) -> int:
3 nums.sort()
4 lo, hi = 0, len(nums) - 1
5 count = 0
6 while lo < hi:
7 if nums[lo] + nums[hi] < target:
8 count += hi - lo
9 lo += 1
10 else:
11 hi -= 1
12 return count1class Solution {
2 public int countPairs(int[] nums, int target) {
3 Arrays.sort(nums);
4 int lo = 0, hi = nums.length - 1;
5 int count = 0;
6 while (lo < hi) {
7 if (nums[lo] + nums[hi] < target) {
8 count += hi - lo;
9 lo++;
10 } else {
11 hi--;
12 }
13 }
14 return count;
15 }
16}1class Solution {
2public:
3 int countPairs(vector<int>& nums, int target) {
4 sort(nums.begin(), nums.end());
5 int lo = 0, hi = (int)nums.size() - 1;
6 int count = 0;
7 while (lo < hi) {
8 if (nums[lo] + nums[hi] < target) {
9 count += hi - lo;
10 lo++;
11 } else {
12 hi--;
13 }
14 }
15 return count;
16 }
17};Time and space complexity
O(n log n)
Sorting dominates; the pointer scan is O(n) because each step moves one pointer.
O(1)
Only a few variables beyond the in-place sort.
Other approaches
Brute force (time O(n^2), space O(1))
Check every pair (i, j). With n at most 50 this is fast enough, but it does not scale.
Sort and binary search (time O(n log n), space O(1))
For each i, binary search the last j > i with nums[i] + nums[j] < target and add the number of indices up to it.
Common mistakes
- Using <= instead of <; a pair that sums exactly to target does not count.
- Adding 1 instead of hi - lo when the sum fits, which counts only one pair per step.
- Thinking sorting breaks the i < j rule; each unordered pair of positions is still counted exactly once.
Frequently asked questions
Why is it safe to sort when the problem talks about indices?
Every unordered pair of positions is one (i, j) with i < j, and sorting only relabels positions. The number of pairs with a small enough sum stays the same.
Why add hi - lo instead of 1?
nums[lo] + nums[hi] < target and every value at lo + 1 .. hi is at most nums[hi], so all of those sums are also below target.
Is the two-pointer version needed for n <= 50?
No, brute force passes. The two-pointer counting pattern is the point, since it carries over to larger inputs and to related problems like 3Sum Smaller.