All questions
EasyArrays · Two Pointers
Two Sum with Best Pair Under Target
Written by a community member · · 1 attempt · 0% pass
0
Given an array of integers nums and an integer target, return the indices of the pair of numbers whose sum is the largest value less than or equal to target.
If there are multiple pairs with the same sum, return the pair with the smallest first index; if there is still a tie, return the pair with the smallest second index.
You may not use the same element twice. Return the indices as a list [i, j] with i < j.
Examples:
- Input:
nums = [2, 7, 11, 15],target = 9
Output:[0, 1] - Input:
nums = [1, 4, 2, 3],target = 6
Output:[1, 2]
Constraints:
2 <= len(nums) <= 100-10^9 <= nums[i], target <= 10^9- There is at least one valid pair whose sum is less than or equal to
target.
Try to achieve better than O(n^2) time.