Binary SearchLearning BS on 1D Arrays

Implement Upper Bound

Problem Statement:First element > target

JavaScript Solution & Complexity Analysis

Run & test in browser
JavaScript Lab Sandbox
/*
Problem Statement -> First element > target
*/

function upperBoundApproach1(arr, target) {
  let low = 0, high = arr.length - 1, mid;
  let ans = arr.length;
  while (low <= high) {
    mid = low + Math.floor((high - low) / 2);
    if (arr[mid] > x) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return ans;
};
Console Output
Click "Run Code" above to execute and view output...