Last active
March 5, 2022 21:35
-
-
Save see-why/9aa070d050c4726800ea734c0487062f to your computer and use it in GitHub Desktop.
HackerRank 2d-arrays challenge solution
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
//My solution to this Hackerank challenge...https://www.hackerrank.com/challenges/30-2d-arrays/problem?isFullScreen=true | |
'use strict'; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', function(inputStdin) { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function() { | |
inputString = inputString.split('\n'); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
function main() { | |
let arr = Array(6); | |
let array_of_sums = []; | |
for (let i = 0; i < 6; i++) { | |
arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); | |
} | |
for (let i= 0; i<6 ; i++){ | |
if ( (i + 2) < 6) { | |
let length = arr[i].length; | |
if ( (i + 2) < length){ | |
let nestedArray = arr[i]; | |
let secondNestedArray = arr[i+1]; | |
let thirdNestedArray = arr[i+2]; | |
for (let k = 0; k < length; k++){ | |
if (k+2 < length) { | |
let sum = nestedArray[k] + nestedArray[k+1] + nestedArray[k+2] + secondNestedArray[k+1] + thirdNestedArray[k] + thirdNestedArray[k+1] + thirdNestedArray[k+2]; | |
array_of_sums.push(sum); | |
} | |
} | |
} | |
} | |
} | |
console.log(Math.max(...array_of_sums)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment