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.
Here is the solution in C language.
(Paste it in VS Code or format it (can also use gpt to format).
include
void input(int arr[6][6])
{
for(int row = 0; row < 6; row++)
{
for(int col = 0; col < 6; col++)
{
scanf("%d", &arr[row][col]);
}
}
}
int highest_sum(int arr[6][6])
{
// Basically, hourglass format is like.
// These all alphabets are basically represent columns.
// a1, a2, a3
// b1
// c1, c2, c3
int result_sum = -100, temp_sum = 0;
// we have initialized result_sum with -100 not 0 because if the numbers are negative and first sum we get is also negative, so it will not get stored becuase of if condition.
for(int row=0; row<4; row++)
{
for(int cols=0; cols<4; cols++)
{
temp_sum = arr[row][cols] + arr[row][cols+1] + arr[row][cols+2] + arr[row+1][cols+1]
+ arr[row+2][cols] + arr[row+2][cols+1]
+ arr[row+2][cols+2] ;
//a1 + a2 + a3 + b1 +c1 + c2 + c3
if (temp_sum>result_sum)
{ // so that the bigger sum gets stored in the result.
result_sum = temp_sum;
}
}
}
return result_sum;
}
int main()
{
int matrix[6][6];
int highest_hourglass_sum;
input(matrix); //input function is created.
highest_hourglass_sum = highest_sum(matrix);
printf("%d",highest_hourglass_sum);
return 0;
}
int main()
{
int matrix[6][6];
int highest_hourglass_sum;
input(matrix); //input function is created.
highest_hourglass_sum = highest_sum(matrix);
printf("%d",highest_hourglass_sum);
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
2D Array - DS
You are viewing a single comment's thread. Return to all comments →
Here is the solution in C language. (Paste it in VS Code or format it (can also use gpt to format).
include
void input(int arr[6][6]) { for(int row = 0; row < 6; row++) { for(int col = 0; col < 6; col++) { scanf("%d", &arr[row][col]); } } } int highest_sum(int arr[6][6]) { // Basically, hourglass format is like. // These all alphabets are basically represent columns.
// a1, a2, a3 // b1 // c1, c2, c3 int result_sum = -100, temp_sum = 0; // we have initialized result_sum with -100 not 0 because if the numbers are negative and first sum we get is also negative, so it will not get stored becuase of if condition. for(int row=0; row<4; row++) { for(int cols=0; cols<4; cols++) {
temp_sum = arr[row][cols] + arr[row][cols+1] + arr[row][cols+2] + arr[row+1][cols+1] + arr[row+2][cols] + arr[row+2][cols+1] + arr[row+2][cols+2] ; //a1 + a2 + a3 + b1 +c1 + c2 + c3 if (temp_sum>result_sum) { // so that the bigger sum gets stored in the result. result_sum = temp_sum; } } } return result_sum; } int main() { int matrix[6][6]; int highest_hourglass_sum; input(matrix); //input function is created. highest_hourglass_sum = highest_sum(matrix); printf("%d",highest_hourglass_sum); return 0; } int main() { int matrix[6][6]; int highest_hourglass_sum; input(matrix); //input function is created. highest_hourglass_sum = highest_sum(matrix); printf("%d",highest_hourglass_sum); return 0; }