Dan array. Find the longest sequence of consecutive elements in the array = 0, count their number, find out the index of the first 0 in this sequence.

Ex .: 9 10 8 0 0 3 5 6 0 0 0 1 3 the answer will be 3 and 9 (element).

What am I doing wrong?

It counts only the number of the first sequence of zeros. In addition, I decided to first deal with finding the correct sequence, so I did not consider the index yet:

public static void main(String[] args) { int s=0;//счетчик сколько нулей подряд int k=0;//количество нулей подряд int x[] = {2, 2, 0, 0, 0, 5, 1, 0, 0, 0, 0} ; k=s; {s=0; for( int i=0; i<x.length; i++) if(x[i]==0) s=s+1; else if(s>k) k=s; System.out.print(k); } } } 
  • four
    Beginner, make more efforts to solve this simple task - a_gura
  • one
    You can not be so cruel) - liliya
  • one
    This is a cruel reality. - a_gura
  • one
    @liliya Now if you sent a photo, then maybe comrades zadroty would be less cruel) - Vladimir Gordeev
  • one
    @liliya: You see! So we did everything right. - VladD

3 answers 3

@liliya , inside if (x[i] == 0) { ... You need a cycle that counts consecutive zeros (and at the same time changes i ), and then the analysis of its parameters and memorizing the maximum along with its initial index.

    In an array iteration loop, if you meet a zero, you open another cycle and you count how many times it occurs. Then, when the next number is not zero, you change the pointer of the outer loop (so as not to go through the passed zeros). When you meet the next zero, you repeat the operation with a nested zero counting cycle. If there are more zeros on this iteration than on the previous one, you overwrite their number and a pointer to the beginning of the sequence; if it is less, you go further.

        public static void main(String[] args) { int input[] = { 9, 10, 8, 0, 0, 3, 5, 6, 0, 0, 0, 1, 3 }; // StringBuilder понадобится для сбора из исходного массива // элементов, чтобы затем превратить их в String StringBuilder stringInput = new StringBuilder(); // а этот чтобы собрать наибольшую последовательность нолей StringBuilder zeroes = new StringBuilder(); //сюда будем сохранять текущую длину последовательности нолей int length = 0; //а сюда наибольшую длину int maxLength = 0; for (int i = 0; i < input.length; i++) { //двигаемся по массиву, собиреам элементы в StringBuilder stringInput = stringInput.append(input[i]); //когда встечаем 0, считаем последовательность нолей if (input[i] == 0) { length++; } //когда встречаем не 0, сбрасываем значение счетчика нолей if (input[i] != 0) { length = 0; } //сравниваем и сохраняем наибольшеее из значений счетчика нолей maxLength = Math.max(maxLength, length); } //собираем наибольшую последовательность нолей в StringBuilder for (int i = 0; i < maxLength; i++) { zeroes = zeroes.append("0"); } //превращаем StringBuilder'ы в String'и String numbers = stringInput.toString(); String zeroComb = zeroes.toString(); //выводим рамер наибольшей последовательности, //используем метод indexOf, чтобы вывести индекс наибольшей System.out.println(maxLength + " " + numbers.indexOf(zeroComb)); 

      }

      • one
        This is probably a very good code. But a bad answer. In your own words, please explain what you wrote and why. - Sergey Nudnov
      • @SergeyNudnov is perhaps a very good sample commentary (I did not like this sarcasm) - Stranger in the Q