Java iterative solution


#1

consider a case where all elements in the input array are zero, except for one which is set to ‘1’.
this case fits all the questions conditions (unique solution), but a binary search cannot solve it.

public class Solution {
public int solve(ArrayList A) {
if (A.get(0) > A.get(1)){
return A.get(0);
}
int size = A.size();
if (A.get(size-2) < A.get(size-1)){
return A.get(size-1);
}
for (int i =1 ; i < size ; i ++){
if (A.get(i-1) <= A.get(i) && A.get(i) >= A.get(i+1)){
return A.get(i);
}
}
return -1;
}
}