You get an ArrayIndexOutOfBoundsException
when running the code with input as stated in your question: On your third recursive call you have l = 3
, h = 3
and thus mid = 3
. When the code enters your second if-statement (else if(input[mid]==1 && input[mid+1]==0)
) you are trying to access index number 4, but your array's index only goes from 0 to 3.
Try stepping through your code with the debugger: Put a break point at the line int zi=findFirstZero(input,0,size-1);
and step through it line by line!
However, I was not able to enter the entire sequence of numbers you gave above, since your first number (i.e. 4) tells how many numbers you can enter. When I enter 4 as the first number the code will start evaluating the sequence given after I enter the fourth 1.