We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'hourglassSum' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
function hourglassSum(arr) {
// Write your code here
function hourglassArrays(arr) {
let rows = arr.length
let cols = arr[0].length
let subArrays = []
for(let i = 0; i<rows-2; i++){
for(let j = 0; j<cols-2;j++){
let subarray = []
for(let x = 0; x<3;x++){
let row = []
for(let y=0;y<3;y++){
if(i+x < rows && j + y < cols){
row.push(arr[i+x][j+y])
}
}
if (row.length > 0) subarray.push(row)
}
subArrays.push(subarray)
}
}
return subArrays
}
let array = hourglassArrays(arr)
let sumArray = []
array.forEach(arr => {
let rows = arr.length
let cols = arr[0].length
let sum = 0
for(let i = 0; i < rows; i++){
for(let j = 0; j < cols; j++){
if(i !== 1){
sum += arr[i][j]
} else {
if(j === 1) {
sum += arr[i][j]
}
}
}
}
sumArray.push(sum)
});
return Math.max(...sumArray)
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
let arr = Array(6);
for (let i = 0; i < 6; i++) {
arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
}
const result = hourglassSum(arr);
ws.write(result + '\n');
ws.end();
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
2D Array - DS
You are viewing a single comment's thread. Return to all comments →
using Java Script
'use strict';
const fs = require('fs');
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');
});
function readLine() { return inputString[currentLine++]; }
/* * Complete the 'hourglassSum' function below. * * The function is expected to return an INTEGER. * The function accepts 2D_INTEGER_ARRAY arr as parameter. */
function hourglassSum(arr) { // Write your code here function hourglassArrays(arr) { let rows = arr.length let cols = arr[0].length let subArrays = []
}
function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
}