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

Yes, that idea is good.

As a test, try this with the function written as you posted it above:

void setup() {
  noLoop();
  int[] nums = {0, 1, 1, 0, 0, 1, 1, 1, 1};
  print(maximumConsecutiveZero(nums));
}

It should produce the following output:

4

However, it produces this instead:

2

Why is that? Could you fix the problem by moving this conditional block to somewhere else?:

      if (count > maximum) { 
        maximum = count; 
      } 
1 Like