Show problems as
Classic problem #259MediumArrayTwo PointersBinary SearchSorting

3Sum Smaller

Real-world version: Triples Totalling Under the Limit
Tracked in this browser only
Read 3Sum Smaller on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tray holds the readings readings.

Return how many triples of positions i < j < k have readings[i] + readings[j] + readings[k] strictly below limit.

Examples

Example 1

Input
readings = [0, 0, 0], limit = 1
Output
1

The tray has one triple and it totals nothing, which is below one.

Example 2

Input
readings = [1, 1, 1], limit = 3
Output
0

The one triple totals 3, which is not strictly below 3.

Example 3

Input
readings = [-100, -100, -100], limit = -100
Output
1

The three readings total -300, comfortably below the limit.

Constraints

  • 0 <= readings.length <= 3500
  • -100 <= readings[i] <= 100
  • -100 <= limit <= 100
  • the answer is at most 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def three_sum_smaller(readings: list[int], limit: int) -> int:
Java
public int threeSumSmaller(int[] readings, int limit)