Created
February 11, 2018 12:10
-
-
Save caglarorhan/0d44359da3f1761c30405e7921ee491a to your computer and use it in GitHub Desktop.
2D Array - DS - Hourglass in A Array problem from - https://www.hackerrank.com/challenges/2d-array/problem
This file contains hidden or 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 arr = []; | |
var hGMaxSum=-63; | |
for(arr_i = 0; arr_i < 6; arr_i++){ | |
arr[arr_i] = readLine().split(' '); | |
arr[arr_i] = arr[arr_i].map(Number); | |
} | |
for(var c=0; c<=3; c++){ | |
//var hGA =[]; | |
for(var hG=0; hG<=3;hG++){ | |
var sum=0; | |
sum+=parseInt(arr[c][hG]) + parseInt(arr[c][hG+1]) + parseInt(arr[c][hG+2]); | |
sum+=parseInt(arr[c+1][hG+1]); | |
sum+=parseInt(arr[c+2][hG]) + parseInt(arr[c+2][hG+1]) + parseInt(arr[c+2][hG+2]); | |
//hGA.push([sum,[parseInt(arr[c][hG]),parseInt(arr[c][hG+1]) , parseInt(arr[c][hG+2])],[parseInt(arr[c+1][hG+1])],[parseInt(arr[c+2][hG]) , parseInt(arr[c+2][hG+1]), parseInt(arr[c+2][hG+2])]]) | |
if(sum>=hGMaxSum){hGMaxSum=sum} | |
//console.log(hGA); | |
} | |
// | |
} | |
console.log(hGMaxSum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context
Given a 2D Array, :
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in to be a subset of values with indices falling in this pattern in 's graphical representation:
a b c
d
e f g
There are hourglasses in , and an hourglass sum is the sum of an hourglass' values.
Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Note: If you have already solved the Java domain's Java 2D Array challenge, you may wish to skip this challenge.
Input Format
There are lines of input, where each line contains space-separated integers describing 2D Array ; every value in will be in the inclusive range of to .
Constraints
Output Format
Print the largest (maximum) hourglass sum found in .
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
Explanation
contains the following hourglasses:
1 1 1 1 1 0 1 0 0 0 0 0
1 0 0 0
1 1 1 1 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0
1 1 0 0
0 0 2 0 2 4 2 4 4 4 4 0
1 1 1 1 1 0 1 0 0 0 0 0
0 2 4 4
0 0 0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 4 2 4 4 4 4 0
0 0 2 0
0 0 1 0 1 2 1 2 4 2 4 0
The hourglass with the maximum sum () is:
2 4 4
2
1 2 4