259. 3Sum Smaller
3Sum Smaller (LeetCode 259) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the Converging (Sorted Array Target Sum) subpattern.
How to solve 3Sum Smaller
3Sum Smaller is a Converging Two Pointers problem, part of Two Pointers. Learn converging two pointers for sorted arrays. Solve two sum, three sum, and container problems efficiently.
Work towards the solution in these steps:
- Consider how sorting the array might simplify the problem and enable efficient searching.
- After sorting, think about fixing one number and using a two-pointer approach to find pairs that satisfy the condition with it.
- Initialize two pointers at the ends of the subarray after the fixed element, and analyze how their sum relates to the target.
The reference solution runs in O(n^2) time and O(1) space.
The 3Sum Smaller solution and editorial walks through the full approach with reference code.
Problem statement
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.
Examples
Example 1
Input: {"nums": [-2, 0, 1, 3], "target": 2}
Output: 2
Explanation: There are two triplets with sum less than 2: (-2, 0, 1) and (-2, 0, 3).
Example 2
Input: {"nums": [0, 0, 0, 0], "target": 1}
Output: 4
Explanation: All four index triplets have a sum of 0, which is less than the target 1.
Topics and companies
Topics: Array, Two Pointers, Binary Search, Sorting.
Reported in interviews at IBM, Citadel.
How to practise 3Sum Smaller on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n^2) time and O(1) space.
Open 3Sum Smaller in the code editor, or read the 3Sum Smaller editorial for a worked solution with its approach and complexity analysis.
Where 3Sum Smaller sits in the DSA pattern sheet
- Two Pointers pattern guide
- Converging (Sorted Array Target Sum) subpattern
- The full DSA patterns sheet
Problems related to 3Sum Smaller
Other problems that use the same Converging (Sorted Array Target Sum) and Two Pointers techniques:
- Boats to Save People — Medium, same subpattern
- Container With Most Water — Medium, same subpattern
- 4Sum — Medium, same subpattern
- Squares of a Sorted Array — Easy, same subpattern
- 3Sum Closest — Medium, same subpattern
- 3Sum — Medium, same subpattern
3Sum Smaller FAQ
What is the LeetCode number of 3Sum Smaller?
3Sum Smaller is LeetCode problem 259, rated Medium.
What pattern is used to solve 3Sum Smaller?
3Sum Smaller is a Converging Two Pointers problem. It belongs to the Two Pointers pattern, in the Converging (Sorted Array Target Sum) subpattern.
What is the time complexity of 3Sum Smaller?
The reference solution runs in O(n^2) time and O(1) space.
Which companies ask 3Sum Smaller?
3Sum Smaller has been reported in interviews at IBM, Citadel.