-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinarySearchUtils.java
More file actions
206 lines (167 loc) · 5.57 KB
/
Copy pathBinarySearchUtils.java
File metadata and controls
206 lines (167 loc) · 5.57 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package alg.binarysearch;
import alg.misc.InterestingAlgorithm;
/**
* Common binary search interview patterns.
*/
public class BinarySearchUtils {
/**
* Searches for a target in a rotated sorted array. Returns index or -1 if not found.
*/
@InterestingAlgorithm(timeComplexity = "O(log n)", spaceComplexity = "O(1)")
public static int searchRotatedArray (int [] nums, int target) {
if (nums == null || nums.length == 0)
return (-1);
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums [mid] == target)
return (mid);
if (nums [left] <= nums [mid]) {
// Left half is sorted
if (target >= nums [left] && target < nums [mid])
right = mid - 1;
else
left = mid + 1;
}
else {
// Right half is sorted
if (target > nums [mid] && target <= nums [right])
left = mid + 1;
else
right = mid - 1;
}
}
return (-1);
}
/**
* Finds the first and last position of a target in a sorted array.
* Returns {-1, -1} if target is not found.
*/
@InterestingAlgorithm(timeComplexity = "O(log n)", spaceComplexity = "O(1)")
public static int [] findFirstAndLast (int [] sorted, int target) {
int [] result = new int [] { -1, -1 };
if (sorted == null || sorted.length == 0)
return (result);
// Find first occurrence
int left = 0;
int right = sorted.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (sorted [mid] == target) {
result [0] = mid;
right = mid - 1;
}
else if (sorted [mid] < target)
left = mid + 1;
else
right = mid - 1;
}
if (result [0] == -1)
return (result);
// Find last occurrence
left = result [0];
right = sorted.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (sorted [mid] == target) {
result [1] = mid;
left = mid + 1;
}
else if (sorted [mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return (result);
}
/**
* Searches a matrix where each row is sorted and the first element of each row
* is greater than the last element of the previous row.
*/
@InterestingAlgorithm(timeComplexity = "O(log(m*n))", spaceComplexity = "O(1)")
public static boolean searchMatrix (int [][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix [0].length == 0)
return (false);
int rows = matrix.length;
int cols = matrix [0].length;
int left = 0;
int right = rows * cols - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int row = mid / cols;
int col = mid % cols;
int val = matrix [row] [col];
if (val == target)
return (true);
else if (val < target)
left = mid + 1;
else
right = mid - 1;
}
return (false);
}
/**
* Finds a peak element (strictly greater than its neighbors). Returns its index.
* For elements at the edges, only one neighbor is considered.
*/
@InterestingAlgorithm(timeComplexity = "O(log n)", spaceComplexity = "O(1)")
public static int findPeakElement (int [] nums) {
if (nums == null || nums.length == 0)
return (-1);
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums [mid] > nums [mid + 1])
right = mid;
else
left = mid + 1;
}
return (left);
}
/**
* Returns floor(sqrt(x)) using binary search.
*/
@InterestingAlgorithm(timeComplexity = "O(log x)", spaceComplexity = "O(1)")
public static int sqrtFloor (int x) {
if (x < 0)
throw new IllegalArgumentException("Cannot compute sqrt of negative number");
if (x == 0)
return (0);
long left = 1;
long right = x;
long result = 0;
while (left <= right) {
long mid = left + (right - left) / 2;
if (mid * mid == x)
return ((int) mid);
else if (mid * mid < x) {
result = mid;
left = mid + 1;
}
else {
right = mid - 1;
}
}
return ((int) result);
}
/**
* Finds the minimum element in a rotated sorted array (no duplicates).
*/
@InterestingAlgorithm(timeComplexity = "O(log n)", spaceComplexity = "O(1)")
public static int findMinRotatedSorted (int [] nums) {
if (nums == null || nums.length == 0)
throw new IllegalArgumentException("Invalid input");
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums [mid] > nums [right])
left = mid + 1;
else
right = mid;
}
return (nums [left]);
}
}