-
Notifications
You must be signed in to change notification settings - Fork 688
/
SumOfThreeValues.java
66 lines (53 loc) · 2.2 KB
/
SumOfThreeValues.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package misc;
import java.util.Arrays;
public class SumOfThreeValues {
/*
* Given an array of integers and a value, determine if there are any three integers in the array
* that sum equal to the given value.
*
* Input: 3, 7, 1, 2, 8, 4, 5 Target sum: 20
* Output: 5 + 7 + 8 = 20
*
* Runtime Complexity:
* O(n2logn).
*
* Memory Complexity:
* Constant, O(1).
*
* First sort the input array in O(nlogn) time.
* Then iterate over each pair (a, b) in the array in a nested loop and calculate the remaining sum
* (sum - (a + b)).
* Find the remaining sum in the array using binary search.
* If found, then solution is found, with the three numbers being a,b and (sum - (a+b)).
*
* */
public static Boolean findSumOfThreeValues(int arr[],
int requiredSum) {
Arrays.sort(arr);
for (int i = 0; i < arr.length - 2; i++) {
for (int j = i + 1; j < arr.length - 1; j++) {
int remaining_sum = requiredSum - arr[i] - arr[j];
int k = Arrays.binarySearch(arr, remaining_sum);
if (k > 0 && k != i && k != j) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {-25, -10, -7, -3, 2, 4, 8, 10};
System.out.println("-8: " + findSumOfThreeValues(arr, -8));
System.out.println("-25: " + findSumOfThreeValues(arr, -25));
System.out.println("0: " + findSumOfThreeValues(arr, 0));
System.out.println("-42: " + findSumOfThreeValues(arr, -42));
System.out.println("22: " + findSumOfThreeValues(arr, 22));
System.out.println("-7: " + findSumOfThreeValues(arr, -7));
System.out.println("-3: " + findSumOfThreeValues(arr, -3));
System.out.println("2: " + findSumOfThreeValues(arr, 2));
System.out.println("4: " + findSumOfThreeValues(arr, 4));
System.out.println("8: " + findSumOfThreeValues(arr, 8));
System.out.println("7: " + findSumOfThreeValues(arr, 7));
System.out.println("1: " + findSumOfThreeValues(arr, 1));
}
}