HackerRank Algorithm
2D Array-Hourglass Problem
Hello guys,
in this article, we’re trying to solve the hourglass problem in hackerrank.
First of all, as you can see an array given to us which has 6 other arrays. And the main array looks like a 6x6 matrix. And at the description of the problem says the main array will always be a 6x6 array. And then that matrix has some hourglass shapes. The picture is down below.
So when we sum elements that are on the hourglass we retrieved a number that will be put in another array. And we need to find the max number in the final 4x4 matrix.
So let’s try to resolve this.
We are creating a for loop which runs 4 times. It’s running 4 times because the array size will always be 6. And we can create an hourglass from top to down just 4 times. Inside of for loop, we have one more for loop. and that’s reason same too. We can create an hourglass 4 times from left to right. And if you want you can consider creating one more algorithm for summing elements on hourglass but I just want to type it. And right now the empty array has all the numbers that it needs.
The empty array is not empty anymore. :) Therefore we can find the maximum number easily like above.