package com.codegym.task.task08.task0812;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
/*
Longest sequence
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> numbers = new ArrayList<>();
ArrayList<Integer> repetitions = new ArrayList<>();
// Adding numbers to the list
for (int n = 0; n < 10; n++)
numbers.add(Integer.parseInt(reader.readLine()));
// Checking for repeated numbers
Integer sequence = 1;
for (int n = 0, nxt = 1; n < 10 && nxt < 10; n++, nxt++) {
while (!numbers.get(n).equals(numbers.get(nxt))) {
n++;
nxt++;
sequence = 1;
if (n == 8 && nxt == 9) {
repetitions.add(sequence);
break;
}
}
while (numbers.get(n).equals(numbers.get(nxt))) {
sequence++;
if (n == 8 && nxt == 9) {
repetitions.add(sequence);
break;
}
n++;
nxt++;
if (!numbers.get(n).equals(numbers.get(nxt))) {
repetitions.add(sequence);
sequence = 1;
break;
}
}
}
Collections.sort(repetitions);
System.out.println(repetitions.get(repetitions.size() - 1));
}
}

# Failed to pass the last requirement

Under discussion

Comments (1)

- Popular
- New
- Old

You must be signed in to leave a comment

John Michael Montuya

14 June 2019, 15:48

I'm thinking that we have different algorithms since your code looks rather complicated to me, but the way I did it was to declare two variables - count and finalCount wherein finalCount keeps a record of the longer number sequence by far until the array is fully traversed.

0