Here we can take some possible cases .
Case 1:
When the array containing all 1's .
Then it will take O(n) to increment the counter to n from 0.
Case 2:
When the array containing all 0's .
Then the else part only executed and it will take n*O(1)= O(n) . [ O(1) is taken for each 0's because its mentioned in question that f(m)=O(m). And for all the time f is called with counter value=0]
Case 3:
When first n-1 elements are 1's and last element is 0
Then it will take O(n-1) to increment the counter to n-1. And for executing the last input 0 with counter value "n-1" will take O(n-1) .
So total it will take only O(n).
Case 4:
When half of the array containing 1's and half of the array containing 0's.
First half takes O(n/2) to increment the counter to n/2 .
For the first 0 in the second half it will take O(n/2) . Because now the counter value is n/2.
And then the "counter=0" is executed and counter value changed to 0. So, for the remaining (n/2-1) zero's the "f" is called with counter value 0. So it will take ( n/2-1)*O(1)
So total it will take O(n) only in all the 4 cases.
Case 5:
+++++++++++++++++++++++
It will take O(n^2) when ??
++++++++++++++++++++++++
Suppose if the "counter=0" was not there in the else part.
Then as mentioned in the 4th case,
For first half of 1's it will take O(n/2) .
For the remaining n/2 0's , it will take n/2*n/2 = O(n^2). Because there's no "counter=0" statement. So for n/2 0's "f" is called with counter value n/2.
Hope this helps :)