Count Pairs Whose Sum is Less than Target - Problem
Given a 0-indexed integer array 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.
You need to count all valid pairs where the sum of two elements is strictly less than the target value.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [-4,1,1,3], target = 2
›
Output:
6
💡 Note:
Valid pairs: (-4,1), (-4,1), (-4,3), (1,1), (1,3), (1,3). All have sums < 2.
Example 2 — No Valid Pairs
$
Input:
nums = [-1,1,2,3,1], target = 2
›
Output:
3
💡 Note:
Valid pairs: (-1,1), (-1,1), (-1,2). Other sums are ≥ 2.
Example 3 — All Pairs Valid
$
Input:
nums = [-5,-3,-2,-1], target = 0
›
Output:
6
💡 Note:
All negative numbers, so all 6 pairs have negative sums < 0.
Constraints
- 2 ≤ nums.length ≤ 50
- -50 ≤ nums[i] ≤ 50
- -50 ≤ target ≤ 50
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array nums and target value
2
Process
Find all pairs (i,j) where i<j and nums[i]+nums[j]<target
3
Output
Count of valid pairs
Key Takeaway
🎯 Key Insight: Sorting allows us to use two pointers and count multiple valid pairs in one step
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code