Classic problem #259MediumArrayTwo PointersBinary SearchSorting
3Sum Smaller
Real-world version: Triples Totalling Under the LimitTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Intersection of Two ArraysReal-world version: Shared Scan CodesEasy
- Earliest Finish Time for Land and Water Rides IReal-world version: Forge and Kiln BookingEasy
- Intersection of Two Arrays IIReal-world version: Shared Spare PartsEasy
- Check If N and Its Double ExistReal-world version: Twice Some Other EntryEasy