Skip to content

Instantly share code, notes, and snippets.

View MMintzer's full-sized avatar

Matt Mintzer MMintzer

View GitHub Profile
#Binary Search
Write a function that implements binary search.
Problem
Given a sorted array of numbers, locate the index of a specified value according to the following algorithm.First identify the middle number in the array.Next determine if the value to be found is greater than, lower than, or equal to the middle number.If it is equal, you are finished, and output the index of the found value.If not, repeat the procedure for a smaller array, formed from by taking half of the given array that the specified number falls into.
THIS IS AN EXAMPLE FOR THE INTERVIEWER. HAVE YOUR CANDIDATE COME UP WITH THEIR OWN EXAMPLE!
Example
Consider the array[1, 3, 4, 7, 12, 17, 20].We want to locate the index of 17. First compare 17 to the middle of the array, which is 7. Because 17 > 7 we then repeat the comparison using the subarray[12, 17, 20].We find that 17 matches the middle of this array, and so we output the index from the original array, which is 5. Note that we do not output the index of 17 from the smaller suba
@MMintzer
MMintzer / pairSum
Last active September 9, 2019 19:46
// Given an array of numbers sorted in ascending order (least to greatest), and a
// separate number (a "sum"), determine if any 2 numbers in the array add up to the sum.
// Return true if any 2 different numbers within the array add up to sum.
// Return false if no 2 numbers in the array add up to sum.
function pairSum(arr, target) {
}