Why we require to intialize MaxhourglassSum=(-63) in the given solution of a problem?

Viewed 39

Problem :- LINK

Input Format

Each of the lines of inputs 6 contains 6 space-separated integers .

Constraints

  • -9<=a[i][j]<=9
  • 0<=i,j<6

Output Format

Print the largest (maximum) hourglass{see link} sum found in .

#include<stdio.h>
int MaxhourglassSum=(-63);

void hourglass(int a[6][6],int i,int j){
            int sum=0;
            for(int m=i;m<i+3;m++){
                for(int n=j;n<j+3;n++){
                    if((m==i+1 && n==j) || (m==i+1 && n==j+2))
                    continue;
                    else
                    sum+=a[m][n];
                }
            }
            if(sum>MaxhourglassSum)
            MaxhourglassSum =sum;
}
int main(){
    int a[6][6];
        for(int i=0;i<6;i++){
            for(int j=0;j<6;j++){
                scanf("%d",&a[i][j]);
            }
        }

        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                    hourglass(a,i,j);
            }
        }

        printf("%d",MaxhourglassSum);

    return 0;
}
1 Answers

According to the link, an "hourglass" consists of 7 numbers, and the minimum value for the numbers is -9, so the minimum possible value of the sum is 7 * (-9).

That's why MaxhourglassSum is initialized with value -63. The value will be compared with the next candidate and will be replaced by any greater value.

You could also initialize it with a lower value, e.g. -999 or INT_MIN

Related