Two Sum in a Sorted Array
Updated
Problem statement
Given an integer array nums sorted in nondecreasing order and a target, find the two distinct positions whose values sum to target.
Exactly one valid pair exists. Return [i, j] using one-based positions with i < j. Use constant extra space, apart from the returned pair.
Function
twoSumSorted(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [1,2]The first and second values are 2 and 7, so the one-based answer is [1,2].
Example 2
nums = [-4,-1,0,3,8]target = 2return = [2,4]The second and fourth values are -1 and 3. Their sum is 2.
Example 3
nums = [3,3]target = 6return = [1,2]The two positions are different even though their values are equal.
Constraints
- 2 <= nums.length <= 100000.
- Every value in nums is between -1000000 and 1000000, inclusive.
- The target is between -2000000 and 2000000, inclusive.
- Exactly one pair of distinct indices has values summing to target.
- The values are in nondecreasing order.
More London Stock Exchange interview questions
- Implement a Queue Using an ArrayPHONE SCREEN · Seen Feb 2020
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Feb 2020
- Palindrome PartitioningPHONE SCREEN · Seen Feb 2020
- Two SumPHONE SCREEN · Seen Feb 2020
- Group Anagram StringsONSITE INTERVIEW · Seen Apr 2020
- Implement Key Equality and HashingONSITE INTERVIEW · Seen Apr 2020
Similar questions at other companies
- Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
- Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Closest Vehicle and Charger in a LineSpotify · PHONE SCREEN
- Correct an In-Place Array ReversalDuolingo · PHONE SCREENDuolingo interview questions
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Remove Duplicates From Sorted Array In PlaceGet My Parking · PHONE SCREEN