You are given a string array named strs
with length n
, when each string can have the value "good"
or "bad"
. It is also known that exists index i
so that:
0<=i<=n-1
, strs[0]=strs[1]=...=strs[i-1]="good"
, strs[i]=strs[i+1]=...=strs[n-1]="bad"
.
Pay attention that if i=0
, it means that strs
has only strings with the value "bad"
.
Write an algorithm to find index i
.
Desired run time: O(logn)
My attempt:
I'm sure you need to use binary search here, but for some reason I have a problem with the check of the middle element.
I thought of checking if the middle element has a value of "good"
and the middle+1 element has a value of "bad"
, but this can give out of bounce error.
Any idea how to solve it?