-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKadane.js
38 lines (33 loc) · 1.11 KB
/
Kadane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function processData(input) {
//Enter your code here
var inputArray = input.split('\n');
var testCase = Number(inputArray[0]);
var counter = 1;
for(var i = 0; i < testCase; ++i) {
var arrLength = Number(inputArray[counter]);
var array = inputArray[counter + 1].split(" ").map(Number);
var max = -Infinity,
max_cur = -Infinity,
non_conti = -Infinity,
result = max;
for( var j = 0; j < arrLength; ++j) {
max_cur = Math.max(array[j],max_cur + array[j]);
var maxi_non_conti = Math.max(array[j],non_conti + array[j]);
non_conti = maxi_non_conti > non_conti ? maxi_non_conti : non_conti;
if(max_cur > max) {
max = max_cur;
}
}
console.log(max >= max_cur ? max : max_cur , non_conti);
counter += 2;
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});