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.
This approach involves iterating over all possible pairs (i, j) where 0 <= i < j < n, and checking if their sum is less than the target. If so, count that pair.
This is a straightforward method but could become inefficient with larger arrays due to its O(n^2) time complexity.
This C code uses two nested loops to find all pairs (i, j) where nums[i] + nums[j] < target. The outer loop runs from 0 to n-1, and the inner loop runs from i+1 to n, where n is the length of the array. A counter keeps track of valid pairs.
Time Complexity: O(n^2) because we have two nested loops.
Space Complexity: O(1) because we only use a fixed amount of extra space.
This approach leverages sorting the array to use the two-pointer technique efficiently. By sorting, we can move pointers from both ends of the list towards the center to find valid pairs, reducing unnecessary calculations.
This C solution uses qsort to sort the array first. Then, it maintains two pointers, starting from the leftmost and rightmost ends. If the sum of elements at the pointers is less than the target, all pairs between i and j satisfy the condition, and we move the left pointer up. Else, we move the right pointer down.
Time Complexity: O(n log n) due to sorting, followed by an O(n) two-pointer scan.
Space Complexity: O(1) since sorting is in-place.
First, we sort the array nums. Then, for each j, we use binary search in the range [0, j) to find the first index i that is greater than or equal to target - nums[j]. All indices k in the range [0, i) meet the condition, so the answer increases by i.
After the traversal, we return the answer.
The time complexity is O(n times log n), and the space complexity is O(log n). Here, n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2) because we have two nested loops. |
| Two-Pointer Technique on Sorted Array | Time Complexity: O(n log n) due to sorting, followed by an O(n) two-pointer scan. |
| Sorting + Binary Search | — |
| 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 |
LeetCode 2824: Count Pairs Whose Sum is Less than Target • Engineering Digest • 6,722 views views
Watch 9 more video solutions →Practice Count Pairs Whose Sum is Less than Target with our built-in code editor and test cases.
Practice on FleetCode