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:
3
💡 Note:
Valid pairs: (0,1): -4+1=-3, (0,2): -4+1=-3, (0,3): -4+3=-1. All have sums < 2. Total: 3 pairs.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code