Created
February 23, 2017 14:22
-
-
Save yashikagarg13/056f41c7a45d54738fb2af61b9780b82 to your computer and use it in GitHub Desktop.
Hacker Rank - Algorithms - Diagonal Difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.hackerrank.com/challenges/diagonal-difference | |
process.stdin.resume(); | |
process.stdin.setEncoding('ascii'); | |
var input_stdin = ""; | |
var input_stdin_array = ""; | |
var input_currentline = 0; | |
process.stdin.on('data', function (data) { | |
input_stdin += data; | |
}); | |
process.stdin.on('end', function () { | |
input_stdin_array = input_stdin.split("\n"); | |
main(); | |
}); | |
function readLine() { | |
return input_stdin_array[input_currentline++]; | |
} | |
/////////////// ignore above this line //////////////////// | |
function main() { | |
var n = parseInt(readLine()); | |
var a = []; | |
for(a_i = 0; a_i < n; a_i++){ | |
a[a_i] = readLine().split(' '); | |
a[a_i] = a[a_i].map(Number); | |
} | |
var convertToFlat = arr => arr.reduce((flatArr, a) => flatArr.concat(a), []); | |
a = convertToFlat(a); | |
var diagonal1 = a.reduce((memo, num, i) => { | |
if(Math.floor(i/n) == i%n ) { | |
return memo + num; | |
} else { | |
return memo | |
} | |
}, 0) | |
var diagonal2 = a.reduce((memo, num, i) => { | |
if(((n-1) - Math.floor(i/n)) == i%n ) { | |
return memo + num; | |
} else { | |
return memo | |
} | |
}, 0) | |
console.log(Math.abs(diagonal1 - diagonal2)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume(); | |
process.stdin.setEncoding('ascii'); | |
var input_stdin = ""; | |
var input_stdin_array = ""; | |
var input_currentline = 0; | |
process.stdin.on('data', function (data) { | |
input_stdin += data; | |
}); | |
process.stdin.on('end', function () { | |
input_stdin_array = input_stdin.split("\n"); | |
main(); | |
}); | |
function readLine() { | |
return input_stdin_array[input_currentline++]; | |
} | |
/////////////// ignore above this line //////////////////// | |
function main() { | |
var n = parseInt(readLine()); | |
var a = []; | |
for(a_i = 0; a_i < n; a_i++){ | |
a[a_i] = readLine().split(' '); | |
a[a_i] = a[a_i].map(Number); | |
} | |
var convertToFlat = arr => arr.reduce((flatArr, a) => flatArr.concat(a), []); | |
a = convertToFlat(a); | |
var diagonal1 = a.filter((num, i) => Math.floor(i/n) == i%n) | |
.reduce((memo, num, i) => memo + num, 0); | |
var diagonal2 = a.filter((num, i) => ((n-1) - Math.floor(i/n) == i%n)) | |
.reduce((memo, num, i) => memo + num, 0); | |
console.log(Math.abs(diagonal1 - diagonal2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment