diff --git a/Competitive_Programming/Binary Search Problems/Binary Search Problem (Documentation).txt b/Competitive_Programming/Binary Search Problems/Binary Search Problem (Documentation).txt new file mode 100644 index 0000000000..17c5eefec8 --- /dev/null +++ b/Competitive_Programming/Binary Search Problems/Binary Search Problem (Documentation).txt @@ -0,0 +1,215 @@ +# BINARY SEARCH PROBLEMS + +Binary search is also known as half-interval search. It is a searching technique in a sorted array by repeatedly dividing search intervals in half. It is an efficient algorithm to find the element efficiently. Repeatedly check until the value is found or the interval is empty. + + + + +## Steps To Implement Binary Search +Let us assume there is an array A and an element x. +We have to search throughout the array and we need to find element x inside the array. +If not present print -1 or print the position of the array. + +### Approach: +1. Compare x with the middle element of array A. +2. If x matches with the middle element, we return the mid index. +3. Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half. +4. Else (x is smaller) recur for the left half. +#### Note: To apply binary search array must be sorted. + +#### Code in C++: + +```c +#include +using namespace std; + +int binary_search(int arr, int size, int target) +{ int start = 0; + int end = size-1; + while(start<=end) + { // Finding mid of the array + int mid = end+(start-end)/2; + + // Comparing the middle element with target + if(arr[mid] == target) + { // if the middle element is equals to target return the mid index + return mid; + } + + else if(arr[mid] > target) + { // if target is smaller than the middle element, then target can only lie in the left + end = mid - 1; + } + + else if(arr[mid] < target) + { // if target is greater than the middle element, then target can only lie in the right + start = mid + 1; + } + } + # if the target is not inside the array then return -1 + return -1; +} + + +int main() +{ //Consider a sorted Array + int arr[]={1,3,5,8,10}; + int target=8; + + // Calculating size of array + int size=sizeOf(arr)/sizeOf(arr[0]); + + // Calling of binary_search function + int index = binary_search(arr, size, target); + cout< +#include +using namespace std; + +int find_first_occur(int arr[] , int n) +{ + int start=0; + int end=n-1; + int ans =-1; + while(start<=end) + { // Finding mid of the array + int mid=(end+start)/2; + + //if middle is 0 then 1 lies in right half as array is sorted + if(arr[mid] == 0) + start=mid+1; + + // if middle is 1 then it may be possible that there is 1 in left of it + // thus, find the occurance in left half + else if(arr[mid] == 1) + { // possible position that this is the first occurance + ans=mid; + // but still finding left of it + end=mid-1; + } + } + // completion of the loop either gives us the first occurance if 1 present + // otherwise return the default value -1 which indicates that 1 is not present in array + return ans; +} + + +int main(){ + int arr[]={0,0,0,0,1,1,1}; + int n=sizeof(arr)/sizeof(arr[0]); //size of the array + int first=find_first_occur(arr, n); + if(first!=-1) + cout< +#include + +using namespace std; + + +int binary(int arr[], int size, int target, bool isleft) +{ int ans=-1; + int start= 0; + int end=size-1; + + while(start<=end) + { // finding the mid of the array + int mid=(start+end)/2; + + // if target is smaller than middle then traverse in left half + if(arr[mid]>target) + end=mid-1; + + // if target is greater than middle then traverse in right half + else if(arr[mid]