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: 
Just a few short comments on your code:

### Separation of concerns:

You should separate functionality into different parts. Currently your main method does everything. For small applications like this, that may not be a problem, but as soon as you have larger applications, it becomes more and more unpractical.

You should thus extract your "logic" into a method:

```
private static int countSuccessiveZeros(int[] values) {
   int maxLength = 0;
   int tempLength = 0;
   //....
   // Looping here
   return maxLength;
}

```

Now if you wanted to not count zeros, but twos, you could simply modify the code to the following:

```
private static int countSuccessive(int[] values, int target) {
   int maxLength = 0;
   int tempLength = 0;
   //Your loop here, instead of 0 use target to compare
   return maxLength;
}

```

This also makes your main-method more readable:

```
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 successiveZeros = countSuccessive(values, 0);
   System.out.println("Maximum number of consecutive zeros : " + successiveZeros);
}

```

### Naming:

Very nice, a compliment. Your variable names are short, clear, descriptive and follow Java conventions (`camelCase`). Keep it up ;)
I have thought of this:

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

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

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

            if(values.length - i <= maxLength){
                break;
            }
        }

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

    return maxLength;
}

```

What I do above is: if I've just broken a chain and I already have a chain of, let's say, 5 zeroes, and my array has only 4 positions left to check, why check them? I can't get a bigger chain.

Maybe this time, with this little array of values, it doesn't matter, but in a bigger one with chains that have hundreds of target elements, it would matter.

What do you think?