All Index Pairs Sum to K
Updated
Problem statement
Given an unsorted integer array nums and an integer target, return every pair of indices [i, j] such that:
0 <= i < j < nums.length, andnums[i] + nums[j] == target.
Repeated values and repeated occurrences are retained: distinct index pairs must all appear even when their values are equal.
Return the pairs in lexicographic order, comparing i first and j second. Return an empty matrix when no pair qualifies.
Function
allIndexPairsSumK(nums: int[], target: int) → int[][]Examples
Example 1
nums = [1,5,7,-1,5]target = 6return = [[0,1],[0,4],[2,3]]Indices 0 and 1, indices 0 and 4, and indices 2 and 3 each select values summing to 6. Both occurrences of value 5 are retained.
Example 2
nums = [3,3,3,3]target = 6return = [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]Every choice of two different indices sums to 6. The pairs are ordered by their first index and then their second index.
Example 3
nums = [-2,0,4]target = 3return = []No two values sum to 3, so the returned matrix is empty.
Constraints
0 <= nums.length <= 1000.-10^9 <= nums[i] <= 10^9.-10^9 <= target <= 10^9.- The result may contain up to
nums.length * (nums.length - 1) / 2pairs.
More JP Morgan Chase interview questions
- Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
- Maximize Binary XOR with a Set-Bit LimitPHONE SCREEN · Seen Oct 2026
- Top K Frequent Values Across Key-Partitioned ShardsPHONE SCREEN · Seen Sep 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Aug 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
Similar questions at other companies
- Versioned ArrayTrueFoundry · PHONE SCREEN
- Fair Truncation of a Message LogGoogle · PHONE SCREENGoogle interview questions
- HTTP Log QueriesDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Count Pairs of Overlapping SquaresCapital One · OACapital One interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions
- Search in a Rotated Sorted ArrayAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Log Analyzer: Busiest Service in a Rolling WindowByteDance · PHONE SCREENByteDance interview questions
- Snapshot Map with Sparse Version HistorySnorkel AI · PHONE SCREEN
JP Morgan Chase interview experiences
- JPMorgan Chase Experienced SWE Java Interview: 1 Experience
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)