2
2
3
3
import java .util .Arrays ;
4
4
5
- /**
6
- * 1099. Two Sum Less Than K
7
- *
8
- * Given an array A of integers and integer K,
9
- * return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K.
10
- * If no i, j exist satisfying this equation, return -1.
11
- *
12
- * Example 1:
13
- * Input: A = [34,23,1,24,75,33,54,8], K = 60
14
- * Output: 58
15
- * Explanation:
16
- * We can use 34 and 24 to sum 58 which is less than 60.
17
- *
18
- * Example 2:
19
- * Input: A = [10,20,30], K = 15
20
- * Output: -1
21
- * Explanation:
22
- * In this case it's not possible to get a pair sum less that 15.
23
- *
24
- * Note:
25
- * 1 <= A.length <= 100
26
- * 1 <= A[i] <= 1000
27
- * 1 <= K <= 2000
28
- * */
29
5
public class _1099 {
30
6
public static class Solution1 {
31
7
/**
32
8
* Time: O(n^2)
33
9
* Space: O(1)
34
- * * /
10
+ */
35
11
public int twoSumLessThanK (int []A ,int K ) {
36
12
int maxSum =Integer .MIN_VALUE ;
37
13
for (int i =0 ;i <A .length -1 ;i ++) {
@@ -49,7 +25,7 @@ public static class Solution2 {
49
25
/**
50
26
* Time: O(nlogn)
51
27
* Space: O(1)
52
- * * /
28
+ */
53
29
public int twoSumLessThanK (int []A ,int K ) {
54
30
Arrays .sort (A );
55
31
int left =0 ;