If mid is empty , search in both sides of mid and find the closest non - empty string , and set mid accordingly .
let left = mid - 1 ; let right = mid + 1 ; while ( true ) { if ( left < right && right > last ) return - 1 ; if ( right <= last && arr [ right ] != " " ) { mid = right ; break ; } if ( left >= right && ! arr [ left ] == " " ) { mid = left ; break ; } right ++ ; left -- ; } }