3Sum Smaller

Given an array of integers `nums` and an integer `target`, return the number of index triplets such that the sum of the three numbers is less than `target`.

Input Format

nums = array of integers, target = integer target sum

Output Format

number of triplets with sum less than target

Constraints

Examples

Example 1:

Input:

nums = [-2,0,1,3]
target = 2

Output:

2

Explanation:

Triplets are [-2,0,1] and [-2,0,3] which sum < 2.

Example 2:

Input:

nums = [3,1,0,-2]
target = 4

Output:

3

Explanation:

Triplets are [-2,0,1], [-2,0,3], [-2,1,3] sum < 4.

Example 3:

Input:

nums = [1,1,1,1]
target = 5

Output:

4

Explanation:

All 4 combinations of 3 ones sum 3 (<5).

Loading...
3Sum Smaller - Two Pointers DSA Problem