All problems
1159MediumArrayTwo PointersBinary SearchSorting

Triples Totalling Under the Limit

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2593Sum Smaller

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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