Two Positions That Add Up to a Target
Updated
Problem statement
You are given an integer array values and an integer target. Find two different positions i and j where values[i] + values[j] == target.
- Exactly one such pair of positions exists.
- Two equal values at different positions may form the pair, but one position cannot be used twice.
Return the two positions as [i, j] with i < j. Positions start at 0.
Function
pairPositionsForTarget(values: int[], target: int) → int[]Examples
Example 1
values = [6,-1,4,9,2]target = 13return = [2,3]4 is at position 2 and 9 is at position 3, and 4 + 9 = 13. No other pair adds up to 13, so the answer is [2,3].
Example 2
values = [5,3,5]target = 10return = [0,2]The value 5 sits at positions 0 and 2, and 5 + 5 = 10. The same position cannot be used twice, but two equal values at different positions are fine. The answer is [0,2].
Example 3
values = [-4,8]target = 4return = [0,1]With two numbers there is only one pair: -4 + 8 = 4. The answer is [0,1].
Constraints
2 <= values.length <= 10^4-10^8 <= values[i] <= 10^8-2 * 10^8 <= target <= 2 * 10^8- Exactly one pair of positions
i < jhasvalues[i] + values[j] == target.
More DocuSign Phone Screen questions
- Symmetric Binary TreePHONE SCREEN · Seen Oct 2026
- Union Area of Two RectanglesPHONE SCREEN · Seen Oct 2026
- Execute Cursor-Based Text Editor OperationsPHONE SCREEN · Seen Sep 2026
- Minimize Array Sum After K Halving OperationsPHONE SCREEN · Seen Sep 2026
- Fizz and Buzz Words From 1 to nPHONE SCREEN · Seen Sep 2026
- Rain Kept Between WallsPHONE SCREEN · Seen Mar 2026
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Rebuild a Trip From Flight TicketsPHONE SCREEN · Seen Apr 2019
- Find the Words for Each PrefixPHONE SCREEN · Seen Sep 2018
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Find Unique ElementsCisco · PHONE SCREENCisco interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Two SumAmazon · OA · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Two SumBNSF Railway · PHONE SCREENBNSF Railway interview questions
- List the Key and Value of Every Map EntryNewgen Software · ONSITE INTERVIEW
- Pizza Price CalculatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions