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

It's worth noting that the description clearly states that the input will be -9 <= R <= 9.
This means that the smallest possible value will be -9*7 (as there are 7 elements in an hourglass). So one can just initialize the max value to -63 :)

because if you initialise to zero, if the max hourglass sum is negative then it will not overwrite the zero value. best to either initialise it with the value of the first hourglass (slightly more elegent in my opinion) or a large negative value. 63 is significant because it is not possible to get a sum less than that under the constraints given.

hello sir!
i want to know that if we want to do this programming in c so what programe is used from the starting because my basic in c and c++ is not clear and also i m not familiar with its operation so kindly send me a complete programe based on it in C. thanku

because if you initialise to zero, if the max hourglass sum is negative then it will not overwrite the zero value. since 0 will be greater to any negative number. What I do is save the first hourglass sum as teh finalSum and update it when/if the next ones are bigger.

Because of the -9<= A[i][j] <=9, so the extrame situation is that all the 7 elem are -9, the sum is -63, and any value that smaller than -63 could be set to the initial value.

you know max_value = -63 would do just fine.
because the lowest maximum number that can be achieved with given constraints is -63.
The given constraints for element in array is [-9,9]
so the max an hour glass with all -9s would give (-9)*(7(no_of elements in hour glass)) = -63

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!

The problem specifies that arr[i][j] values are only between -9 and 9, so the minSum for any hourglass can be -63, specifing the initial max_value = -64 will ensure that any sum will be greater

Actually, since the constraint is -9, and you are adding 7 values, the minimum would be -9 x 7 which is -63. That could save a fraction of memory instead of working with a large negative number.

Since the hour glass can only be a max of 7 digits and the smallest digit is -9, as long as your max value is initialized to less than -63 the results should be correct.

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 →

you can also set any large negative value also. Then no need to caluculate first hourglass separately.

for e.g , max_value = -99999;

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.

It's worth noting that the description clearly states that the input will be -9 <= R <= 9. This means that the smallest possible value will be -9*7 (as there are 7 elements in an hourglass). So one can just initialize the max value to -63 :)

Yes. In case the constraints are not there, use Integer.MIN_VALUE.

yes it worked. thanks

This is were I messed up, I initialized max value to -9. Thanks, didn't relize were I messed up at until reading your comment.

I just did this:

you can initilize to arrays first value, that is at a[0]

This is wrong - because you sum 7 numbers, which all can be -9. a[0] can be -9, but the minimal value can be -63

Simply set it to null and use type specific comparison to assign first sum.

why do we need to set max to -63 or to any such value ?

because if you initialise to zero, if the max hourglass sum is negative then it will not overwrite the zero value. best to either initialise it with the value of the first hourglass (slightly more elegent in my opinion) or a large negative value. 63 is significant because it is not possible to get a sum less than that under the constraints given.

hello sir! i want to know that if we want to do this programming in c so what programe is used from the starting because my basic in c and c++ is not clear and also i m not familiar with its operation so kindly send me a complete programe based on it in C. thanku

why should we initialize max value to large negative value?

because if you initialise to zero, if the max hourglass sum is negative then it will not overwrite the zero value. since 0 will be greater to any negative number. What I do is save the first hourglass sum as teh finalSum and update it when/if the next ones are bigger.

what is the hour glass

Because of the -9<= A[i][j] <=9, so the extrame situation is that all the 7 elem are -9, the sum is -63, and any value that smaller than -63 could be set to the initial value.

thanks for the help..

The least no. possible is -9 * 7 = -63. We can also initialize it to that. :)

Thanks

actually -63 is enough.Since 7 slots having a minimum value of -9 in each would turn out to a maximum negative of -7*9=-63

I used Number.NEGATIVE_INFINITY with Javascript

you can set it to integer.MIN_VALUE

You can use -63 as max value because it is stated in condition that a[i][j] is between -9 and + 9

you know max_value = -63 would do just fine. because the lowest maximum number that can be achieved with given constraints is -63. The given constraints for element in array is [-9,9] so the max an hour glass with all -9s would give (-9)*(7(no_of elements in hour glass)) = -63

may i know what is the reason behind this? why we have to initiliza ma_value = -9999 or -63

since the hourglass with smallest sum is -63 when all values in hourglass are -9

-63 would be enough

bro you can just set max value to -9 * 7 = -63 cause it can be the most small value of hourglass acording to question

even -63 can work fine

No need to set max value that low. It will not possibly be any lower than 7*9 so you can simply initialize it to 63.

perhaps -9*7 is enough, though

you could just use one less than the minimum possible value of an hourglass, which is -64.

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!since the least sum is -63 (when all elements are -9), you can initialize it to be -63

Or you can use

which gives you the largest negative value.

Worked like a charm. Thanks. I didnt know we can compare floats and integers.

I set max = nil and then checked if max was nil before setting max to the total with an or conditional

e.g., max = total if max == nil || total > max

short mayor=-63;

If you are writing in Java, you can actually use max_value= Integer.MIN_VALUE; to get the lowest signed interger value in case -99999 isn't enough.

The problem specifies that arr[i][j] values are only between -9 and 9, so the minSum for any hourglass can be -63, specifing the initial max_value = -64 will ensure that any sum will be greater

Actually, since the constraint is -9, and you are adding 7 values, the minimum would be -9 x 7 which is -63. That could save a fraction of memory instead of working with a large negative number.

This was helpful, thanks a ton.

## include

using namespace std; int main() {int a[6][6],sum,maxsum=0; for(int i=0;i<6;i++) {for(int j=0;j<6;j++) {cin>>a[i][j];

} cout<<""<

} if(sum>maxsum) { maxsum=sum; }

} cout<

}

what is wrong with this code?

Great solution if you don't care about scalability or the assumption that the dataset changes over time. Terrible solution if you do.

or you could initially allow to set first hour glass value as max value like if row & column is 0 & 0 respectively.....

Since the hour glass can only be a max of 7 digits and the smallest digit is -9, as long as your max value is initialized to less than -63 the results should be correct.

since each value is between -9 to 9 so -9*7 = -63 should suffice

Min value according to the instructions is actually -9 so -10 would be fine :)

maxValue should be initialized with -63. As every value ranges from -9 to 9.

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.

thanks

more like initiate to -64. The max value it can get down to is -9*7=-63