Three Numbers That Add Up to a Target
Updated
Problem statement
Given an integer array nums and an integer target, return every triplet of values [a, b, c] such that a + b + c == target and the three values come from three different positions of nums.
Write each triplet in non-decreasing order, so a <= b <= c. List each triplet of values only once, even if it can be formed from several sets of positions. Order the triplets by a, then by b, from smallest to largest. Return an empty list if there is no such triplet.
Function
threeSumTarget(nums: int[], target: int) → List<List<Integer>>Examples
Example 1
nums = [4,1,-2,3,0,2]target = 5return = [[-2,3,4],[0,1,4],[0,2,3]]-2 + 3 + 4, 0 + 1 + 4 and 0 + 2 + 3 are all 5. No other set of three values adds up to 5. The triplets are ordered by their first value, then their second.
Example 2
nums = [2,2,2,2]target = 6return = [[2,2,2]]Any three of the four 2s add up to 6. They all give the same values, so [2,2,2] is listed once.
Example 3
nums = [1,1,1]target = 4return = []The only triplet adds up to 3, not 4, so the list is empty.
Constraints
3 <= nums.length <= 1000-10^5 <= nums[i] <= 10^5-3 * 10^5 <= target <= 3 * 10^5
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
- Second Largest Element in an ArrayONSITE INTERVIEW · Seen Jun 2023
Similar questions at other companies
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- 4SumAmazon · ONSITE INTERVIEW
- Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions
- Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions
- 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- 3SumMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Next and Previous Lexicographic PermutationAdobe · ONSITE INTERVIEWAdobe interview questions
- Sort ColorsInfosys · ONSITE INTERVIEWInfosys interview questions