Posts

Showing posts from January, 2020

Algo: Find the last index of a number in an array of integers

Sol: function getBinarySort(array, elmToSearch){ const binaryIndex = getBinarySortHelper(array, elmToSearch); // console.log('111 binaryIndex for:'+ elmToSearch + " is:"+binaryIndex); if (binaryIndex == -1 || binaryIndex == array.length -1 ) { return binaryIndex; } else { for ( let i = binaryIndex+ 1 ; i<= array.length- 1 ;i++) { // console.log('Inside for loop'); // console.warn('yeah') if (array[i] !== elmToSearch) { console.log( 'IS NT EQUAL' ) return i- 1 ; } /* else { console.log('IS EQUAL') } */ } } } function getBinarySortHelper(array, elmToSearch) { let start = 0 , end = array.length - 1 , mid = Math.floor((start + end) / 2 ); while ( true ){ if (elmToSearch < array[start] || elmToSearch > array[e...