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. :)

Very large negative values are not necessary. See that an hourglass is made up of 7 digits. the sum of 'those' 7 digits can be minimum when each digit is itself minimum, i.e, -9. Hence, the minimum sum will be (7)x(-9), which equals -63. So, we can initialize minimum as -64

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

Very large negative values are not necessary. See that an hourglass is made up of 7 digits. the sum of 'those' 7 digits can be minimum when each digit is itself minimum, i.e, -9. Hence, the minimum sum will be (7)x(-9), which equals -63. So, we can initialize minimum as -64.

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

Very large negative values are not necessary. See that an hourglass is made up of 7 digits. the sum of 'those' 7 digits can be minimum when each digit is itself minimum, i.e, -9. Hence, the minimum sum will be (7)x(-9), which equals -63. So, we can initialize minimum as -64

you could actually initialize it with -63 ,would still work.

Using your logic, you could use the type byte.

In Java you could use byte, since it is signed. In C# byte is an unsigned type and not appropriate.

To store the numbers you really only need 7 bits, but there's little use in packing these arrays.

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

I initialized with 1 << 16 * -1

That would very silly. Why not just use the minimum value of -63 as others have indicated.

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

Very large negative values are not necessary. See that an hourglass is made up of 7 digits. the sum of 'those' 7 digits can be minimum when each digit is itself minimum, i.e, -9. Hence, the minimum sum will be (7)x(-9), which equals -63. So, we can initialize minimum as -64.

as a[i][j]>=-9 so just initialize the min value to -9*7 as hour glass consiste of 7 elements and considering worst case for least sum all would be -9

Well if you will analyze inuts then you will get the idea. no array value can be < -9. it means [(-9 *3 *2)+-(9)] . this is your lowest value

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.

Even if some of them are zero they won't beat that 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.

That is extremely inefficient since it is not necessary to know the lowest value just to initialize your maximum value variable.

Actually it shoud be the smallest number possible where all values are equals to -9 => -9*7 = -63

You could also keep -63. Because minimum hourglass sum possible is -63.