javascriptbinary-searcharrayofarrays

How to binary search in array of arrays


I want to implement binary search in javascript into array of arrays when output will be true or false depending if number is in array of arrays or not, but can't figure it out how to do it, could you help ?

const arr = [[3,21,37], [61,79,101,120], [133,149]];


const binarySearch = (number, array) => {

  array.forEach((innerArray) => {
    
    // binary search algorithm
    
  });

};

binarySearch(79, arr);


Solution

  • Binary search through the outer array to find the correct inner array, then binary search again through the correct inner array.

    Your check function in the outer binary search should compare your target number with the boundaries (first element, last element) of the current array and move to the left/right accordingly