In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge about Data structure and algorithms in C,Java,Python and other programming languages that may help you land an online job and make your programmers day successful.We use best data structure notes and best programming resources.
Here goes the article,don't forget to share with your friends 👏💘💘
Let We have a 2-D Array of size 6×6.The hourglass of array is of a particular form which we will explained later in this post.For example consider the array below:
1 5 7 8 9 9
7 8 9 9 9 0
8 4 2 8 0 1
5 7 7 2 4 2
7 3 1 9 7 0
The hour glass of an array will be of the form below:
a b c
d
e f g
That means in the matrix we have total 16 hourglass,4 horizentally and 4 vertically.
In this problem we have to find the sum of the hourglass which have the maximum sum among other hourglasses.
Here I given only the function which will calculate the maximum hourglass sum.
Check out my insta handle.
Here goes the article,don't forget to share with your friends 👏💘💘
Let We have a 2-D Array of size 6×6.The hourglass of array is of a particular form which we will explained later in this post.For example consider the array below:
1 5 7 8 9 9
7 8 9 9 9 0
8 4 2 8 0 1
5 7 7 2 4 2
7 3 1 9 7 0
The hour glass of an array will be of the form below:
a b c
d
e f g
That means in the matrix we have total 16 hourglass,4 horizentally and 4 vertically.
In this problem we have to find the sum of the hourglass which have the maximum sum among other hourglasses.
Solution :
public class Solution {
// The hourglass sum
static int Sum(int[][] a) {
int max = -1000; // As sum sum can be negative
for(int i=0;i<4;i++) // Because there are 4 hourglass horizontally
{
for(int j=0;j<4;j++) // Because there are 4 hourglass vertically
{
if((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])>max)
max=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];
}
}
return max;
}
Check out my insta handle.

0 Comments