Pairs with Target Sum in a Sorted Array
Updated
Problem statement
Given an integer array nums sorted in non-decreasing order and an integer target, return every pair of values [a, b] such that a + b == target and a and b come from two different positions of nums.
Write each pair with the smaller value first, so a <= b. List each pair of values only once, even if it can be formed from several positions, and order the pairs by a from smallest to largest. Return an empty list if there is no such pair.
Function
findPairs(nums: int[], target: int) → List<List<Integer>>Examples
Example 1
nums = [1,2,3,4,4,6,7]target = 8return = [[1,7],[2,6],[4,4]]1 + 7, 2 + 6 and 4 + 4 are 8. The pair [4,4] is allowed because 4 appears at two positions. 3 would need a 5, which is not in the array.
Example 2
nums = [-3,-1,0,2,2,5]target = 1return = [[-1,2]]Only -1 + 2 is 1. The value 2 appears twice, but the pair [-1,2] is listed once.
Example 3
nums = [5]target = 10return = []There is only one position, and a value cannot be paired with itself, so there is no pair.
Constraints
1 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9numsis sorted in non-decreasing order.-2 * 10^9 <= target <= 2 * 10^9
More Bank of America Online Assessment questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Maximum String BeautyOA · Seen Sep 2024
- Merge Point of Two Linked ListsOA · Seen Oct 2023
- Remove Duplicates from a ListOA · Seen Oct 2023
- Count Level-Order Traversals of a TreeOA · Seen Sep 2023
- Count Set BitsOA · Seen Sep 2023
- Count Pairs with Sum KOA · Seen Jun 2023
- Letter Pattern SumOA · Seen Sep 2022
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Meandering ArrayVerisk · OA
- Interleave Array HalvesAutodesk · OAAutodesk interview questions
- Collect Branches for a Bird NestVisa · OAVisa interview questions
- Maximum Requests in a Time WindowIBM · OAIBM interview questions
- Set IntersectionBlackRock · OABlackRock interview questions
- Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
- Filter Odds and Reverse EvensUpstart · OAUpstart interview questions
- Score Difference When Even Picks Reverse the ListMicrosoft · OAMicrosoft interview questions