-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
159 lines (143 loc) · 4.7 KB
/
Copy pathBinarySearch.java
File metadata and controls
159 lines (143 loc) · 4.7 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
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {5, 6, 7, 8, 1, 2, 3, 4};
int key = 1;
int index = findElementInRotatedArray(arr, key);
System.out.println("Index of " + key + " is: " + index);
}
/**
* Find the start and end index of N in a sorted array
* We will run two BS one for first index and one for end index
*/
public static void firstAndLastIndex(int[] arr, int key){
int left = 0, right = arr.length-1;
int mid = (left + right) / 2;
int firstIndex = -1, lastIndex = -1;
while(left <= right){
if(arr[mid] == key){
firstIndex = mid;
right = mid - 1;
} else if(arr[mid]<key){
left = mid + 1;
} else {
right = mid - 1;
}
}
left = 0;
right = arr.length - 1;
while(left <= right){
if(arr[mid] == key){
lastIndex = mid;
left = mid + 1;
} else if(arr[mid]<key){
left = mid + 1;
} else {
right = mid - 1;
}
}
System.out.println("First index of " + key + " is: " + firstIndex);
System.out.println("Last index of " + key + " is: " + lastIndex);
}
public static int findElementInRotatedArray(int[] arr, int key) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// If the element is found at mid
if (arr[mid] == key) {
return mid;
}
// Check if the left half is sorted
if (arr[left] <= arr[mid]) {
// If the key is in the left half
if (key >= arr[left] && key < arr[mid]) {
right = mid - 1;
} else { // Otherwise, it's in the right half
left = mid + 1;
}
}
// Otherwise, the right half must be sorted
else {
// If the key is in the right half
if (key > arr[mid] && key <= arr[right]) {
left = mid + 1;
} else { // Otherwise, it's in the left half
right = mid - 1;
}
}
}
// If the element is not found
return -1;
}
/**
* Find minimum element in a sorted and rotated array
*/
public static int findMin(int[] arr) {
int left = 0, right = arr.length -1;
while(left < right){
int mid = (left + right) / 2;
if(arr[mid]<arr[right]){
right = mid;
} else {
left = mid + 1;
}
}
return arr[left];
}
/**
* Find the index of the first element in a sorted and rotated array
*
* We need to find the index of the smallest element in a sorted and rotated array
* Then Binary search in two parts from 0 to minIndex and minIndex to arr.length -1
*/
public static int binarySearch(int[] arr, int left, int right, int target){
while(left <= right){
int mid = left + (right - left) / 2;
if(arr[mid] == target){
return mid;
} else if(arr[mid] < target){
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
public static int findMinIndex(int[] arr){
int left = 0, right = arr.length - 1;
while(left < right){
int mid = (left + right) / 2;
if(arr[mid] < arr[right]){
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
public static int findIndexSortedRotated(int[] arr, int key){
int minIdx = findMinIndex(arr);
int ans = binarySearch(arr, 0, minIdx -1, key);
if(ans == -1)
ans = binarySearch(arr, minIdx -1, arr.length - 1, key);
return ans;
}
/**
* Check if the element is present in a sorted 2D array
* Start from top right corner and then start traversing
* If the element is greater than arr(i,j) then go to the next row else go to the previous column
*/
public static boolean findElementIn2DArray(int[][] arr, int target){
int i = 0, j = arr.length - 1;
while(i< arr.length && j>=0){
if(arr[i][j]==target){
return true;
} else if(arr[i][j]<target){
i++;
} else {
j--;
}
}
return false;
}
}