/
BinarySearchTwoSum.java
70 lines (59 loc) · 2.46 KB
/
BinarySearchTwoSum.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
67
68
69
70
package com.yunikov.algorithms.twosum;
import com.yunikov.algorithms.search.BinarySearch;
import com.yunikov.algorithms.search.ArraySearch;
import java.util.Arrays;
import java.util.Optional;
/**
* Variation of two-sum algorithm by sorting array and performing binary search on it.
* Running time is O(n*log(n)) for each of two steps of this algorithm.
*/
public class BinarySearchTwoSum extends TwoSumAlgorithm {
/**
* Checks if there are two numbers x,y in the sorted input array that satisfies x + y = sum.
*
* @param array sorted input array for the algorithm
* @param sum sum of two numbers to look for
* @return true if
*/
public boolean twoSumExistsSorted(final long[] array, final long sum, final boolean distinct) {
final ArraySearch search = new BinarySearch();
for (long element: array) {
final long searchElement = sum - element;
final Optional<Long> optElement = search.searchSorted(array, searchElement);
if (distinct && optElement.isPresent() && array[(int)((long)optElement.get())] != element) {
return true;
} else if (!distinct && optElement.isPresent()) {
return true;
}
}
return false;
}
/**
* See {@link #twoSumExistsSorted(long[], long, boolean)}
*/
public boolean twoSumExistsSorted(final int[] array, final int sum, final boolean distinct) {
final ArraySearch search = new BinarySearch();
for (int element: array) {
final int searchElement = sum - element;
final Optional<Integer> optElement = search.searchSorted(array, searchElement);
if (distinct && optElement.isPresent() && array[optElement.get()] != element) {
return true;
} else if (!distinct && optElement.isPresent()) {
return true;
}
}
return false;
}
@Override
protected boolean doTwoSumExists(final int[] array, final int sum, final boolean distinct) {
final int[] arrayCopy = Arrays.copyOf(array, array.length);
Arrays.sort(arrayCopy);
return twoSumExistsSorted(arrayCopy, sum, distinct);
}
@Override
protected boolean doTwoSumExists(final long[] array, final long sum, final boolean distinct) {
final long[] arrayCopy = Arrays.copyOf(array, array.length);
Arrays.sort(arrayCopy);
return twoSumExistsSorted(arrayCopy, sum, distinct);
}
}