Posted on

Binary Search —- AGAIN!

Gotta grind the basics, so you don’t forget.


int binarySearch(vector<int> &nums, int target) {
  int low =0, high = nums.size()-1;
  while( low <= high ){
    int mid = low + (high - low) / 2;
    if( nums[mid] == target ) 
      return mid;
    else if( nums[mid] < target ) 
       low = mid + 1;
    else
       high = mid -1;
  }

  return -1;
}