0

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);

4
  • This may help: khanacademy.org/computing/computer-science/algorithms/… Commented Aug 20, 2022 at 10:09
  • 1
    Is there any particular reason to do this in a nested array structure? Commented Aug 20, 2022 at 10:10
  • 2
    If you only want a true/false as answer then why not .flat()en arr before doing the search. Commented Aug 20, 2022 at 10:13
  • Also, you need a return value, and forEach always returns undefined. Commented Aug 20, 2022 at 10:26

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.