package com.codegym.task.task08.task0812;
import java.io.*;
import java.util.ArrayList;
/*
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<Integer>();
ArrayList<Integer> count = new ArrayList<Integer>();
int temp = 0;
int countN = 1;
for(int i = 0; i < 10; i++) {
int tempNo = Integer.parseInt(reader.readLine());
if (i == 0) temp = tempNo;
else
if(temp == tempNo) {
countN++;
}
else
{
temp = tempNo;
count.add(countN);
countN = 1;
}
if (i == 9) count.add(countN);
numbers.add(tempNo);
}
int max = 0;
for(int i = 1; i < count.size(); i++) {
if (max < count.get(i)) max = count.get(i);
}
System.out.println(max);
}
}
The code is not going through verify
Resolved
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Cristian
23 December 2020, 18:31useful
Try this code:
ArrayList<Integer> list = new ArrayList<Integer>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
int number = 0;
int max = 1;
int current = 1;
for (int i = 0; i < 10; i++) {
number = Integer.parseInt(reader.readLine());
list.add(number);
}
reader.close();
for (int i = 0; i < 9; i++) {
if( list.get(i).equals(list.get(i+1))) {
current ++;
if (max < current) {
max = current;
}
} else {
current = 1;
}
}
if (max < current) {
max = current;
}
System.out.println(max);
+1
Guadalupe Gagnon
23 December 2020, 17:58solution
The bug is on line 36. Consider if you entered something like:
1, 1, 1, 1, 1, 1, 1, 1, 2, 2
+2
Marius Moldovan
4 January 2021, 10:54
Thank you Guadalupe! I have replaced int max = 0; with int max = count.get(0); and it works. That's what I wanted to do in first place but I forgot!
+1