How do I find a maximum number of consecutive non-zero values in the array?

I tried to do the following, but in some situations it only return the first consecutive non-zero value but not the longest one. What might be the problem?

int maximumConsecutiveZero(int[] array) {
  int count = 0; 
  int maximum = 0; 
  for (int i=0; i<array.length; i++) { 
    if (array[i] != 0) { 
      count++;
    } else  { 
      if (count > maximum) { 
        maximum = count; 
      } 
      count = 0;
    }
  }
  return maximum;
}
1 Like