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.

Just a very silly thing, but you can also use Short values for the purpose of this problem, it doesn't have to be necessarily an Integer. Just for using less memory. :)

Given the constraint in the question (-9 <= arr[i][j] <= 9 and 0 <= i,j <= 5), I used the possible maximum negative value to initialize my max value (-9 * 6 = -54).

This is correct, but the test cases given also had nothing lower than -54, which was lucky for me, since I made the (mindless) error of thinking there were only 6 elements in an hourglass.

One of the test cases should have a max of -62 for this reason, IMO.

This is correct. Given the question the lowest possible sum is -63. The sum of an hourglass is always multiplied by 7 (2 rows of 3 + 1 column of 1). The highest negative number is -9. Therefore -9 * 7 = -63

If the sums of hourglasses are all negative , and if u have taken your max variable initilized to zero and calculate the max with refrence to 0 obviously you will be returning 0 as the answer which is wrong . So its better to initilize with Integer.MIN_VALUE to a variable to which will be storing a max value .

Or initialize it to any value less than -63, because the input values of the array ranges from -9 to +9. So a hour glass can contain a maximum of 7 times -9, which sums up to -63. Any value greater than -64 can be accepted to swap the max variable, right!

Or initialize it to any value less than -63, because the input values of the array ranges from -9 to +9. So a hour glass can contain a maximum of 7 times -9, which sums up to -63. Any value greater than -64 can be accepted to swap the max variable, right!

instead of using min value of integer, you can just initialize it to -63 as it is clearly mentioned that the values cannot be less than -9. So -9*7=-63

## 2D Array - DS

You are viewing a single comment's thread. Return to all comments →

This is helpful, thank you. I used Integer.MIN_VALUE;

Just a very silly thing, but you can also use Short values for the purpose of this problem, it doesn't have to be necessarily an Integer. Just for using less memory. :)

I think the input variables are defined as primitive type int.

But that doesn't mean you cannot change. At the end the output is a string, so it doesn't matter.

Guys we also have one more alternative, with the help of flag we can achieve it,

if (flag) {

max = sum;

flag = false;

}

if (max < sum) {

max = sum;

}

will it not give a compilation error i.e max is not initialized or something like that?

or in JS:

let max;

if (typeof max !== 'number' || sum > max) { max = sum }

I used, max = -Infinity

Using your logic, you could use the type byte.

Given the constraint in the question (-9 <= arr[i][j] <= 9 and 0 <= i,j <= 5), I used the possible maximum negative value to initialize my max value (-9 * 6 = -54).

max negative value is -63 (-9 * 7)

anything less than -54.

For python initialize with: -9223372036854775807

This is correct, but the test cases given also had nothing lower than -54, which was lucky for me, since I made the (mindless) error of thinking there were only 6 elements in an hourglass.

One of the test cases should have a max of -62 for this reason, IMO.

This is correct. Given the question the lowest possible sum is -63. The sum of an hourglass is always multiplied by 7 (2 rows of 3 + 1 column of 1). The highest negative number is -9. Therefore -9 * 7 = -63

first value always works and doesn't rely on you knowing the input before execution

It passed the testcases but why it doesn't runs when initialized to zero?

If the sums of hourglasses are all negative , and if u have taken your max variable initilized to zero and calculate the max with refrence to 0 obviously you will be returning 0 as the answer which is wrong . So its better to initilize with Integer.MIN_VALUE to a variable to which will be storing a max value .

thanks mate... helped a lot!!!

Or initialize it to any value less than -63, because the input values of the array ranges from -9 to +9. So a hour glass can contain a maximum of 7 times -9, which sums up to -63. Any value greater than -64 can be accepted to swap the max variable, right!

HotIce!Try this man!Actually, -63 will work too.

Or initialize it to any value less than -63, because the input values of the array ranges from -9 to +9. So a hour glass can contain a maximum of 7 times -9, which sums up to -63. Any value greater than -64 can be accepted to swap the max variable, right!

HotIce!Taking max value as integer.MIN_VALUE is also correct however max can also be sumArray[0] (the array of sum of hourglasses) .

Thanks ! That helped.

Zero is considered to be a positive number. If all the hour glasse's totals are negative they wont beat your zero.

instead of using min value of integer, you can just initialize it to -63 as it is clearly mentioned that the values cannot be less than -9. So -9*7=-63

I made two iterations: First time to find the lowest value and the second time to calculate the highest value starting at the lowest value.