Question: > 
> **Question:** To find the maximum number of consecutive zeros in a given
>  array.
> 
> 
> **Example:**
> 
> 
> **Input:** \${1, 2, 0, 0, 2, 4, 0, 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 7, 0}\$
> 
> 
> **Output:** "Maximum number of consecutive zeros : 6"
> 
> 
> 

Here is what I've come up with:

```
public class ZeroSeriesLength {

    public static void main(String[] args) {

        int[] values = {1, 2, 0, 0, 2, 4, 0, 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 7, 0};

        int maxLength = 0;
        int tempLength = 0;

        for (int i = 0; i < values.length; i++) {

            if (values[i] == 0) {
                tempLength++;
            } else {
                tempLength = 0;
            }

            if (tempLength > maxLength) {
                maxLength = tempLength;
            }
        }

        System.out.println("Maximum number of consecutive zeros : " + maxLength);
    }
}

```

Is the logic correct? Or can it be done via a simple approach?

Answer: 
Keeping in mind of @Vogel612's nice answer (which is completly true), I have 2 more points to make.

A for-each loop is easier than a normal for loop. Don't worry about the correct order, it's the same as the order you have in the "standard" for loop.

Secondly, I would move the `if (values[i] == 0)` to a ternary statement.
You have an if-else statement that sets the same integer.

The second if statement is in my opinion great. Simple check and if true set the value.

So my solution is:

```
 public static int countSuccessiveb (int[] values, int target) {
    int maxLength = 0;
    int tempLength = 0;

    for (int value : values) {
        tempLength = (value == target) ? 1 + tempLength : 0;
        if (tempLength > maxLength) {
            maxLength = tempLength;
        }
    }
    return maxLength;
}

```
In the addendum to aforesaid :

1 . The comparation (tempLength > maxLength) should be moved to the first if block. If current is not zero, then tempLength is obviously not greater than maxLength, and therefore comparation is useless waste of CPU time.

```
       if (values[i] == 0) {
          if (++tempLength > maxLength) {
                maxLength = tempLength;
          }

       } else {
                tempLength = 0;
       }

```

2 . The comparation and assignment of maxLength could be reduced to:

```
maxLength = tempLength > maxLength?tempLength : maxLength; 

//or

maxLength = Math.max(maxLength,tempLength); 

```

3 . Inline incrementation of tempLength :

```
maxLength = Math.max(maxLength,++tempLength); 

```

So you code may look just like:

```
    public class ZeroSeriesLength {
      public static void main(String[] args) {
        int[] values = {1, 2, 0, 0, 2, 4, 0, 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 7, 0};

        int maxLength = 0;
        int tempLength = 0;

        for (int i = 0; i < values.length; i++) {

            if (values[i] == 0) {
                maxLength = Math.max(maxLength, ++tempLength);
            } else {
                tempLength = 0;
            }
        }

        System.out.println("Maximum number of consecutive zeros : " + maxLength);
    }
}

```