HackerRank problem Diagonal Difference using JavaScript:
Test results:
Test Case #0: 0.09s
Test Case #1: 0.09s
Test Case #2: 0.1s
Test Case #3: 0.1s
Test Case #4: 0.09s
Test Case #5: 0.09s
Test Case #6: 0.09s
Test Case #7: 0.09s
Test Case #8: 0.09s
Test Case #9: 0.1s
Test Case #10: 0.09s
function processData(input) {
var lines = input.split("\n");
var arr = [];
for (var i = 1; i < lines.length; i++) {
arr.push(lines[i].split(" "));
}
var sumTopLeft = 0;
var sumBotLeft = 0;
for (var j = 0; j < arr.length; j++) {
sumTopLeft += parseInt(arr[j][j]);
}
var count = -1;
for (var k = arr.length - 1; k >= 0; k--) {
count++;
sumBotLeft += parseInt(arr[k][count]);
}
var diff = Math.abs(sumTopLeft - sumBotLeft);
console.log(diff);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Test results:
Test Case #0: 0.09s
Test Case #1: 0.09s
Test Case #2: 0.1s
Test Case #3: 0.1s
Test Case #4: 0.09s
Test Case #5: 0.09s
Test Case #6: 0.09s
Test Case #7: 0.09s
Test Case #8: 0.09s
Test Case #9: 0.1s
Test Case #10: 0.09s
No comments:
Post a Comment