Watch 10 video solutions for Count Pairs Whose Sum is Less than Target, a easy level problem involving Array, Two Pointers, Binary Search. This walkthrough by Engineering Digest has 6,722 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target.
Example 1:
Input: nums = [-1,1,2,3,1], target = 2 Output: 3 Explanation: There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
Example 2:
Input: nums = [-6,2,5,-2,-7,-1,3], target = -2 Output: 10 Explanation: There are 10 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target
Constraints:
1 <= nums.length == n <= 50-50 <= nums[i], target <= 50Problem Overview: Given an integer array nums and an integer target, count how many index pairs (i, j) exist such that i < j and nums[i] + nums[j] < target. The task is to efficiently count all valid pairs without double counting.
Approach 1: Brute Force Pair Check (O(n²) time, O(1) space)
The simplest method checks every possible pair in the array. Use two nested loops: the outer loop fixes index i, and the inner loop iterates from i + 1 to the end. For each pair, compute nums[i] + nums[j] and increment a counter if the sum is smaller than the target. This approach is straightforward and guarantees correctness because every pair is evaluated exactly once.
The downside is scalability. The nested iteration performs roughly n(n-1)/2 comparisons, resulting in O(n²) time complexity. Space usage stays O(1) since only a counter is stored. This method works fine for small inputs and is often the first solution developers write during interviews to establish correctness before optimizing.
Approach 2: Two-Pointer Technique on Sorted Array (O(n log n) time, O(1) space)
A more efficient solution sorts the array first and then applies the two pointers technique. After sorting using sorting, place one pointer at the start (left) and another at the end (right). Evaluate the sum nums[left] + nums[right].
If the sum is less than the target, every element between left and right paired with nums[left] will also produce a valid sum because the array is sorted. That means you can add (right - left) pairs to the count at once, then move left forward. If the sum is greater than or equal to the target, decrease right to reduce the sum.
This insight avoids checking every pair individually. Sorting takes O(n log n) time, and the two-pointer scan runs in O(n). Total time complexity becomes O(n log n) with constant O(1) extra space if sorting is done in place. This approach scales much better for large arrays.
Recommended for interviews: Start with the brute force explanation to show you understand the pair constraint and index ordering. Then transition to the sorted two-pointer solution. Interviewers typically expect the two-pointer optimization because it reduces comparisons and demonstrates familiarity with common patterns used in array and two-pointer problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Check | O(n²) | O(1) | Small input sizes or when demonstrating baseline logic in interviews |
| Two-Pointer on Sorted Array | O(n log n) | O(1) | Best general solution for unsorted arrays where pair counting is required |