Binary Search

PHOTO EMBED

Sat Apr 01 2023 19:27:55 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int size, int key)
{
   int start=0;
   int end=size-1;
   int mid=start+(start-end)/2;    //for avoiding excess memory
   while(start<=end)
   {
      if(arr[mid]==key) return mid;
      if(key>arr[mid])
      {
         start=mid+1;
      }
      else
      {
         end=mid-1;
      }
      mid=start+(start-end)/2;
   }
   return -1;
}

int main() {
	int even[6]={2,4,6,8,12,18};
	int odd[5]={3,8,11,14,17};
	
	int index=binarySearch(odd, 5, 95);
	cout<<"index of 95 is: "<<index;
	return 0;
}
content_copyCOPY

https://www.youtube.com/@CodeHelp