You are given a list of integers numbers and an integer target.
Your task is to choose two different elements from the list such that their sum is as large as possible while still being strictly less than target.
This is a variation of the classic two sum problem.
In the classic version, the goal is usually to find two values whose sum is exactly equal to the target.
In this version, an exact target sum is not required. Instead, you must find the closest possible pair sum that is still less than target.
Return the two chosen numbers as a comma separated string.
If no valid pair exists, return "-1".
Method Signature
String maximumPairLessThanK(List<Integer> numbers, int target)
numbers is the list of positive integers.
target is the integer limit.
- You must choose exactly two different indices
i and j.
- The chosen indices must satisfy
0 ≤ i < j < numbers.size().
- The pair sum must satisfy
numbers.get(i) + numbers.get(j) < target.
- Among all valid pairs, choose the pair with the maximum possible sum.
- Return the chosen numbers as
"firstNumber,secondNumber".
- If no valid pair exists, return
"-1".
Input Format
The method receives:
numbers: a List<Integer> containing the numbers.
target: an integer representing the strict upper limit for the pair sum.
Output Format
Return a string containing the two chosen numbers separated by a comma.
The output format must be:
"firstNumber,secondNumber"
Here, firstNumber must come from the smaller index and secondNumber must come from the larger index.
If no valid pair exists, return "-1".
Conflict Resolution
If multiple pairs have the same maximum valid sum, choose the pair using numbers at lower indices.
More formally:
- For every valid pair, let the pair be represented by indices
(i, j).
- The pair must satisfy
0 ≤ i < j < numbers.size().
- First choose the pair with the largest value of
numbers.get(i) + numbers.get(j) such that the sum is strictly less than target.
- If two or more pairs have the same best sum, choose the pair with the smaller
i.
- If there is still a tie, choose the pair with the smaller
j.
Constraints
1 ≤ numbers.size() ≤ 100
1 ≤ numbers.get(i) ≤ 1000
1 ≤ target ≤ 2000
0 ≤ i < j < numbers.size()
- The same element cannot be used twice.
- The input list will not be null.
Examples
Example 1
maximumPairLessThanK(numbers = List.of(34, 23, 1, 24, 75, 33, 54, 8), target = 60)
Returns "34,24".
Explanation:
The pair 34 and 24 gives sum 58, which is strictly less than 60.
No other valid pair has a larger sum less than 60.
Therefore, the returned pair is "34,24".
Example 2
maximumPairLessThanK(numbers = List.of(10, 20, 30), target = 15)
Returns "-1".
Explanation:
Every possible pair sum is at least 30, which is not less than 15.
Therefore, no valid pair exists.
Example 3
maximumPairLessThanK(numbers = List.of(5, 12, 18, 20), target = 25)
Returns "5,18".
Explanation:
The pair 5 and 18 gives sum 23.
The pair 5 and 20 gives sum 25, but it is not valid because the sum must be strictly less than target.
Therefore, the best valid pair is "5,18".
Example 4
maximumPairLessThanK(numbers = List.of(1, 2, 3, 4), target = 7)
Returns "2,4".
Explanation:
The pair 2 and 4 gives sum 6, which is the largest valid pair sum less than 7.
Example 5
maximumPairLessThanK(numbers = List.of(4, 6, 5, 5), target = 11)
Returns "4,6".
The pairs at indices (0, 1) and (2, 3) both have sum 10, which is the largest valid sum strictly less than 11.
Since index 0 is lower than index 2, the pair at indices (0, 1) is chosen.
Therefore, the answer is "4,6".
Example 6
maximumPairLessThanK(numbers = List.of(3, 7, 4, 6), target = 11)
Returns "3,7".
Explanation:
The pair at indices (0, 1) gives sum 10.
The pair at indices (2, 3) also gives sum 10.
Both pairs have the same best valid sum.
Since index 0 is lower than index 2, the pair "3,7" is returned.