Algorithm
Problem Name: 30 days of code -
In this HackerRank in 30 Days of Code -
problem solution,Objective
Today, we are building on our knowledge of arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video.
Context
Given a 6 * 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 A, then print the maximum hourglass sum.
Example
In the array shown above, the maximum hourglass sum is 7 for the hourglass in the top left corner.
Input Format
There are 6 lines of input, where each line contains 6 space-separated integers that describe the 2D Array A.
Constraints
- -9 <= A[i][j] <= 9
- 0 <= i,j <= 5
Output Format
Print the maximum hourglass sum 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
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int arr[6][6];
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
scanf("%d", &arr[i][j]);
}
}
int max_sum = 0;
int temp_sum;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
temp_sum = 0;
// top row
temp_sum += arr[i][j];
temp_sum += arr[i][j + 1];
temp_sum += arr[i][j + 2];
//middle
temp_sum += arr[i+1][j+1];
//bottom row
temp_sum += arr[i + 2][j];
temp_sum += arr[i + 2][j + 1];
temp_sum += arr[i + 2][j + 2];
//if first hourglass, set as max
if(temp_sum > max_sum || i == 0 && j == 0)
max_sum = temp_sum;
}
}
printf("%d\n", max_sum);
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector < vector<int>> arr(6, vector<int>(6));
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
cin >> arr[i][j];
}
}
int max = -9 * 7;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (j + 2 < 6 && i + 2 < 6) {
int sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
if (sum > max) max = sum;
}
}
}
cout << max;
return 0;
}
Copy The Code &
Try With Live Editor
#3 Code Example with C# Programming
Code -
C# Programming
using System;
class Solution
{
static void Main(String[] args)
{
int[][] arr = new int[6][];
for (int i = 0; i < 6; i++)
{
var tmp = Console.ReadLine().Split(' ');
arr[i] = Array.ConvertAll(tmp, int.Parse);
}
int max = -9 * 7;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
if (j + 2 < 6 && i + 2 < 6)
{
int sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
if (sum > max) max = sum;
}
}
}
Console.WriteLine(max);
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Golang Programming
Code -
Golang Programming
package main
import "fmt"
func main() {
var a [6][6]int
var max int
for i := range a {
for j := range a[i] {
fmt.Scan(&a[i][j])
}
}
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
sumHourGlass := a[i][j] + a[i][j + 1] + a[i][j + 2] + a[i + 1][j + 1] + a[i + 2][j] + a[i + 2][j +1] + a[i + 2][j + 2]
if (i == 0 && j == 0) || sumHourGlass > max {
max = sumHourGlass
}
}
}
fmt.Println(max)
}
Copy The Code &
Try With Live Editor
#5 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int arr[][] = new int[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = in.nextInt();
}
}
int max = -9 * 7;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (j + 2 < 6 && i + 2 < 6) {
int sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
if (sum > max) max = sum;
}
}
}
System.out.println(max);
}
}
Copy The Code &
Try With Live Editor
#6 Code Example with Javascript Programming
Code -
Javascript Programming
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 = [];
for(arr_i = 0; arr_i < 6; arr_i++){
arr[arr_i] = readLine().split(' ');
arr[arr_i] = arr[arr_i].map(Number);
}
var sumarr = [];
var h = 0;
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
sumarr[h] = arr[i][j] + arr[i][j+1] + arr[i][j+2]
+ arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1]
+ arr[i+2][j+2];
h++;
}
}
sumarr.sort();
var max = -99999;
for (var x = 0; x < 16; x++){
if (sumarr[x] > max)
max = sumarr[x];
}
console.log(max);
}
Copy The Code &
Try With Live Editor
#7 Code Example with Python Programming
Code -
Python Programming
arr = []
for _ in range(6):
tmp = [int(x) for x in str(input()).split(" ")]
arr.append(tmp)
maximum = -9 * 7
for i in range(6):
for j in range(6):
if j + 2 < 6 and i + 2 < 6:
result = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]
if result > maximum:
maximum = result
print(maximum)
Copy The Code &
Try With Live Editor