Skip to content

Practice Count Pairs Whose Sum is Less than Target

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2824
LeetCode Problem

Count Pairs Whose Sum is Less than Target

Problem Statement

Count pairs of distinct positions i < j whose values satisfy nums[i] + nums[j] < target. The inequality is strict, and equal values at different positions still count as separate pairs. Practice approach: At the given size limit, check all unordered pairs in O(n^2) time and O(1) extra space. An alternative is sorting and moving two pointers: when the endpoint sum is below target, every intervening partner for the left value also qualifies. Test equality with target and negative values.

Source: Count Pairs Whose Sum is Less than Target on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [-1,1,2,3,1], target = 2
Output
3

# Example 2

Input
nums = [-6,2,5,-2,-7,-1,3], target = -2
Output
10

Constraints

1 <= nums.length == n <= 50
-50 <= nums[i], target <= 50

Related LeetCode Problems

Practice more problems similar to Count Pairs Whose Sum is Less than Target.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.