-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathFindAmountOfElementsLessThan.java
82 lines (69 loc) · 2.16 KB
/
FindAmountOfElementsLessThan.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
71
72
73
74
75
76
77
78
79
80
81
82
package by.andd3dfx.search;
/**
* <pre>
* Implement function countNumbers() that accepts a sorted array of integers and counts the number of array elements
* that are less than the parameter lessThan.
*
* For example, SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4) should return 2 because there are two array
* elements less than 4.
* </pre>
*
* @see <a href="https://youtu.be/a2dvtrdi1YI">Video solution</a>
*/
public class FindAmountOfElementsLessThan {
public static int usingPrimitiveIteration(int[] items, int lessThan) {
var i = 0;
while (i < items.length && items[i] < lessThan) {
i++;
}
return i;
}
public static int usingBinarySearch(int[] items, int lessThan) {
int left = 0;
int right = items.length - 1;
if (lessThan < items[left]) {
return 0;
}
if (items[right] < lessThan) {
return items.length;
}
while (right - left > 1) {
int mid = (left + right) / 2;
if (items[mid] < lessThan) {
left = mid;
} else {
right = mid;
}
}
return right;
}
public static int usingInterpolationSearch(int[] items, int lessThan) {
int left = 0;
int right = items.length - 1;
while (right - left > 1) {
int mid = determineMid(items, lessThan, left, right);
if (items[mid] == lessThan) {
return mid;
}
if (items[mid] < lessThan) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
private static int determineMid(int[] items, int target, int left, int right) {
if (items[right] == items[left]) {
return left;
}
var mid = left + (right - left) * (target - items[left]) / (items[right] - items[left]);
if (mid < left) {
mid = left;
}
if (mid > right) {
mid = right;
}
return mid;
}
}