Binary search is a searching algorithm that is used on a sorted array by repeatedly dividing the search interval by half.
The search space is reduced by comparing the target element with the middle element of the search space.
Complexity Analysis
Best Case : O(1)
Average Case: O(log N)
Worst Case: O(log N)
Implementing Lower Bound
N = 5, arr[] = {3,5,8,15,19}, x = 9
Result: 3
Explanation: Index 3 is the smallest index such that arr[3] >= x.
public class LowerBound {
public static void main(String[] args){
int[] arr = {5, 7, 9, 10, 15};
System.out.println(Solution(arr, 11));
}
public static int Solution(int[] arr, int target){
int n = arr.length;
int low = 0, high = n - 1;
int ans = 0;
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] >= target){
ans = arr[mid];
high = mid - 1;
}else{
low = mid + 1;
}
}
return ans;
}
}
Implementing Upper Bound
N = 4, arr[] = {1,2,2,3}, x = 2
Result: 3
Explanation: Index 3 is the smallest index such that arr[3] > x.
public class UpperBound {
public static void main(String[] args){
int[] arr = {2, 4, 6, 8, 10, 12};
System.out.println(Solution(arr,8));
}
public static int Solution(int[] arr, int target){
int n = arr.length;
int low = 0, high = n - 1;
int ans = 0;
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] > target){
ans = arr[mid];
high = mid - 1;
} else{
low = mid + 1;
}
}
return ans;
}
}
Last Occurrence Of A Number In Sorted Array
Input: N = 7, target=13, array[] = {3,4,13,13,13,20,40}
Output: 4
Explanation: As the target value is 13 ,
it appears for the first time at index number 2.
public class LastOccurence{
public static void main(String[] args){
int[] arr = {3,4,13,13,13,20,40};
System.out.println(Solution(arr,
}
public static int Solution(int[] arr, int key){
int n = arr.length;
int low = 0, high = n - 1;
int result = 0;
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] == key){
result = mid;
low = mid + 1;
}else if(key < arr[mid]){
high = mid - 1;
}else{
low = mid + 1;
}
}
return result;
}
}
Count Occurrences In A Sorted Array
N = 7, X = 3 , array[] = {2, 2 , 3 , 3 , 3 , 3 , 4}
Output: 4
Explanation: 3 is occurring 4 times in
the given array so it is our answer.
public class CountOccurences {
public static void main(String[] args) {
int[] arr = {1, 3, 3, 3, 3, 4};
System.out.println(Solution(arr,3));
}
public static int FirstOccurence(int[] arr, int key ){
int n = arr.length;
int low = 0, high = n - 1;
int ans = 0;
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] == key){
ans = mid;
high = mid - 1;
}else if(key < arr[mid]){
high = mid - 1;
}else{
low = mid + 1;
}
}
return ans;
}
public static int LastOccurence(int[] arr, int key){
int n = arr.length;
int low = 0, high = n - 1;
int ans = 0;
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] == key){
ans = mid;
low = mid + 1;
} else if (arr[mid] < key) {
low = mid + 1;
}else{
high = mid - 1;
}
}
return ans;
}
public static int Solution(int[] arr, int key){
int ans1 = FirstOccurence(arr, key);
int ans2 = LastOccurence(arr, key);
return (ans2-ans1) + 1;
}
}
Search In Rotated Sorted Array
arr = [4,5,6,7,0,1,2,3], k = 0
Result: 4
Explanation: Here, the target is 0.
We can see that 0 is present in the given rotated sorted array.
public class RotatedArray{
public static void main(String[] args){
int[] arr = {7, 8, 9, 1, 2, 3, 4, 5, 6};
System.out.println(Solution(arr, 1));
}
public static int Solution(int[] arr, int target){
int n = arr.length;
int low = 0, high = n - 1;
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] == target){
return mid;
}
if(arr[low] <= arr[mid]){
if(arr[low] <= target && target <= arr[mid]){
high = mid - 1;
}else{
low = mid + 1;
}
}else{
if(arr[mid] <= target && target <= arr[high]){
low = mid + 1;
}else{
high = mid - 1;
}
}
}
return -1;
}
}
Minimum In Rotated Sorted Array
arr = [4,5,6,7,0,1,2,3]
Result: 0
Explanation: Here, the element 0 is the minimum element in the array.
public class Minimal{
public static void main(String[] args) {
int[] arr = {4, 5, 6, 7, 0, 1, 2, 3};
System.out.println(OptimalSolution(arr));
}
public static int OptimalSolution(int[] arr){
int n = arr.length;
int low = 0, high = n - 1;
int ans = Integer.MAX_VALUE;
while(low <= high){
int mid = (low + high)/2;
if(arr[low] <= arr[high]){
ans = Math.min(ans, arr[low]);
break;
}
if(arr[low] <= arr[mid]){
ans = Math.min(ans, arr[low]);
low = mid + 1;
}else{
ans = Math.min(ans, arr[mid]);
high = mid - 1;
}
}
return ans;
}
}
How Many Times Array Is Rotated
arr = [4,5,6,7,0,1,2,3]
Result: 4
Explanation: The original array should be [0,1,2,3,4,5,6,7]. So, we can notice that the array has been rotated 4 times.
public class HowManyTimesRotated {
public static void main(String[] args) {
int[] arr = {4, 5, 6, 7, 0, 1, 2, 3};
System.out.println(OptimalSolution(arr));
}
public static int OptimalSolution(int[] arr){
int n = arr.length;
int ans = Integer.MAX_VALUE;
int index = -1;
int low = 0, high = n - 1;
while(low <= high){
int mid = (low + high)/2;
if(arr[low] <= arr[high]){
if(arr[low] < ans) {
ans = arr[low];
index = low;
}
break;
}
if(arr[low] <= arr[mid]){
if(arr[low] < ans){
ans = arr[low];
index = low;
}
low = mid + 1;
}else{
if(arr[mid] < ans){
ans = arr[mid];
index = mid;
}
high = mid - 1;
}
}
return index;
}
}
Peak Element
arr[] = {1,2,3,4,5,6,7,8,5,1}
Result: 7
Explanation: In this example, there is only 1 peak that is at index 7.
package BinarySearch;
public class PeakElement {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 5, 1};
System.out.println(OptimalSolution(arr));
}
public static int OptimalSolution(int[] arr){
int n = arr.length;
int low = 1, high = n - 2;
if(n == 1){
return 0;
}
if(arr[0] > arr[1]){
return 0;
}
if(arr[n - 1] > arr[n - 2]){
return n - 1;
}
while(low <= high){
int mid = (low + high)/2;
if(arr[mid] >= arr[mid + 1] && arr[mid] >= arr[mid - 1]){
return mid;
}
if(arr[mid] > arr[mid - 1]){
low = mid + 1;
}else {
high = mid - 1;
}
}
return -1;
}
}