Consecutive units, why one solution works?

On Yandex.An interview is a simple task "B. sequential units".
Link https://contest.yandex.ru/contest/8458/problems/B/

made two decisions in Java 8
The first passes all the tests on Yandex.
public class Application
{
 public static void main(String[] args) throws Exception {
 BufferedReader r = new BufferedReader(new FileReader("input.txt"));

 int size = Integer.valueOf(r.readLine());
 int[] sequence = new int[size + 1];
 for (int i = 1; i <= size; i++) {
 sequence[i] = Integer.valueOf(r.readLine());
}

System.out.println(longestSequence(sequence));
}

 public static int longestSequence(int[] vector)
{
 int maxLength = 0;
 int currentLength = 0;

 for (int i = 0; i <= vector.length - 1; i++)
{
 if (vector[i] == 1)
{
currentLength++;
 maxLength = (currentLength > maxLength) ? currentLength : maxLength;
}
else
{

 currentLength = 0;
}
}

 return maxLength;
}
}


the second solution is shorter.
But only the first few tests successful. The wrong answer verdict.
public class Application
{
 public static void main(String[] args) throws Exception {
 BufferedReader r = new BufferedReader(new FileReader("input.txt"));

 int maxLength = 0;
 int currentLength = 0;
 int value;
 String line;

 while ((line = r.readLine()) != null)
{
 try {
 value = Integer.valueOf(line);
 if (value == 1) {
currentLength++;
 maxLength = (currentLength > maxLength) ? currentLength : maxLength;
 } else {
 currentLength = 0;
}
}
 catch (Exception e)
{

}
}

System.out.println(maxLength);
}
}


In my tests everything is OK in both solutions.

Yes, in the second solution, I lowered the first number n (the number of elements of a vector).
In what situation is starting to affect the result? Plz tell me
April 19th 20 at 12:08
1 answer
April 19th 20 at 12:10
I will try to suggest the following:

1. In the second option, due to not taking n to be an error, if the input will be an array {1, 0}, where 1 is n. Accordingly, the result should be 0, but in the second case the answer is 1.

2. There is a condition to limit the execution time and maybe the second option does not fit at this time, because reading from a file in a loop relative to the first option will be more expensive at the time.

Find more questions by tags AlgorithmsYandexJava