Answer the question
In order to leave comments, you need to log in
Why is a "runtime error" issued in Yandex context tests?
Good time!
I wanted to solve problems from Yandex and ran into a strange problem.
In the problem, you need to find the longest sequence of ones in a binary vector and print its length.
It is desirable to obtain a solution that works in linear time and at the same time passes through the input array only once.
The tests are run on node.js
Input format
The first line of the input file contains a single number n, n ≤ 10000. Each of the next n lines contains exactly one number — the next element of the array.
Output Format The
output file must contain a single number, the length of the longest sequence of ones in the input array.
Sample
input string
5
1
0
1
0
1
at output
1
It seems like I solved the problem correctly, the maximum number of occurrences of 1 in the array is issued, but for some reason an error is generated. At the input, I take strings and throw them into an array, and at the output I give out a number as a string to read into
stdout.write()
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin
});
let lines = [];
rl.on('line', (line) => {
lines.push(line);
}).on('close', () => {
function num(arr){
let arrE = [0];
for(let i=0; i<arr.length; i++){
if(arr[i] === 1 ){
arrE[arrE.length-1]++;
} else{
arrE.push(0);
}
}
retrun arrE.sort().pop();
}
process.stdout.write(JSON.stringify(num(lines)));
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question