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

I did the same :), 7 number and values between -9 and 9, so doing the maths. highest negative is -63 and highest positive 63. Because you need the lowest asume value, you choose -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.

## 2D Array - DS

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

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.

My answer is noobie friendly. I have set maxval = None.

Hackerrank - 2D Array - DS - Solution

set max =-63 since in constraints the value of an array lies between -9 and +9

here is problem solution in

Java Python c++andcprogramming language with practical program. https://solution.programmingoneonone.com/2020/07/hackerrank-2d-arrays-ds-solution.htmlGuys 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.

In JS, I used

let maxSum = Number.MIN_SAFE_INTEGER;

beautiifull logic

you can just set the initilize max value as the last hourglass

here is solution in

java python c++andcprogramming. https://solution.programmingoneonone.com/2020/07/hackerrank-2d-arrays-ds-solution.htmlUsing 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

I did the same :), 7 number and values between -9 and 9, so doing the maths. highest negative is -63 and highest positive 63. Because you need the lowest asume value, you choose -63.

yes intitalizing max value less than maximum negative value helped me in solving 3,5,7 testcases

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