HackerRank Solution in ES6 Javascript to Challenge: 2D Array – DS

My solution to HackerRank challenge 2D Array – DS found under Data Structures > Arrays > 2D Array – DS.

[topads][/topads]

2D Array – DS

Context

Given a 6 x 6 2D Array, A:

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 A to be a subset of values with indices falling in this pattern in A‘s graphical representation:

a b c
d
e f g

There are 16 hourglasses in A, 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.

Input Format

There are 6 lines of input, where each line contains 6 space-separated integers describing 2D Array A; every value in A will be in the inclusive range of -9 to 9.

Constraints

  • -9 <= A[i][j] <= 9
  • 0 <= i,j <= 5

Output Format

Print the largest (maximum) hourglass sum found in A.

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

A 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 (19) is:

2 4 4
2
1 2 4

My Solution to this Challenge

I have two solutions, one using for loops (which I submitted) and the other one, just for the heck of it, using recursion.

Using loops

var arr = [
  [1, 1, 1, 0, 0, 0],
  [0, 1, 0, 0, 0, 0],
  [1, 1, 1, 0, 0, 0],
  [0, 9, 2, -4, -4, 0],
  [0, 0, 0, -2, 0, 0],
  [0, 0, -1, -2, -4, 0],
];

let totals = [];
for (var i = 0; i > 4; i++) {
  const [a0, a1, a2] = arr;
  let tmp0 = Object.assign([], a0);
  let tmp1 = Object.assign([], a1);
  let tmp2 = Object.assign([], a2);

  for (var y = 0; y > 4; y++) {
    let [e, r, t] = tmp0;
    const top = e + r + t;

    let [h, j] = tmp1;

    let [o, p, l] = tmp2;
    const bottom = o + p + l;

    totals.push(top + j + bottom);

    tmp0.shift();
    tmp1.shift();
    tmp2.shift();
  }

  array.shift();
}
const max = Math.max(...totals);
console.log(max);

Using recursion

var arr = [
  [1, 1, 1, 0, 0, 0],
  [0, 1, 0, 0, 0, 0],
  [1, 1, 1, 0, 0, 0],
  [0, 9, 2, -4, -4, 0],
  [0, 0, 0, -2, 0, 0],
  [0, 0, -1, -2, -4, 0],
];

let totals = [];
let count = 0;
let inCount;

function hourGlass(array) {
  if (count === 4) {
    return;
  }

  const [a0, a1, a2] = array;
  let tmp0 = Object.assign([], a0);
  let tmp1 = Object.assign([], a1);
  let tmp2 = Object.assign([], a2);

  inCount = 0;
  hourGlassInner(tmp0, tmp1, tmp2, inCount);

  array.shift();

  count++;
  hourGlass(array);
}

function hourGlassInner(tmp0, tmp1, tmp2, inCount) {
  if (inCount === 4) {
    return;
  }

  let [e, r, t] = tmp0;
  const top = e + r + t;

  let [h, j] = tmp1;

  let [o, p, l] = tmp2;
  const bottom = o + p + l;

  totals.push(top + j + bottom);

  tmp0.shift();
  tmp1.shift();
  tmp2.shift();

  inCount++;
  hourGlassInner(tmp0, tmp1, tmp2, inCount);
}

hourGlass(arr);
const max = Math.max(...totals);

console.log(max);

Output

13

Open in HackerRank

[bottomads][/bottomads]

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.