Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading