Three Sum
Updated
Problem statement
Given an integer array nums, return all the unique triplets [nums[i], nums[j], nums[k]] such that i, j and k are three different indices and nums[i] + nums[j] + nums[k] == 0.
The answer must not contain the same triplet twice. Return each triplet in non-decreasing order, and return the list of triplets sorted in ascending order.
Function
threeSum(nums: int[]) → List<List<Integer>>Examples
Example 1
nums = [-1,0,1,2,-1,-4]return = [[-1,-1,2],[-1,0,1]]-1 + -1 + 2 = 0 and -1 + 0 + 1 = 0. The two -1 values give the same triplet [-1,0,1], so it appears once.
Example 2
nums = [0,1,1]return = []The only triplet is 0 + 1 + 1 = 2, which is not 0.
Example 3
nums = [0,0,0]return = [[0,0,0]]The only triplet adds up to 0.
Constraints
- 3 <= nums.length <= 3000
- -10^5 <= nums[i] <= 10^5
More Patreon interview questions
Similar questions at other companies
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Minimum Removals to Balance ArraySalesforce · OASalesforce interview questions
- Count Perfect PairsAdyen · OAAdyen interview questions
- Profitable Project PairsAkuna Capital · OAAkuna Capital interview questions
- Most Employees Working at OnceCitadel · OACitadel interview questions
- 3SumAT&T · OAAT&T interview questions
- Minimum Height Difference Between Distant PeaksRoblox · OARoblox interview questions
- Minimum Machines for TasksExpedia · OAExpedia interview questions
- Find Minimum Possible SizeMathWorks · OAMathWorks interview questions