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.
my solution for C (Sorry if it's messy, I'm fairly new to this):
(for testcases 3 & 7, maxsum is negative, so make sure it is not initilized to zero. I initilized to the first hourglass sum)
int main() {
int a[6][6], i, j, k, m, sum, maxsum;
for (i = 0; i < 6; i++){
for (j = 0; j < 6 ; j++){
scanf("%d", &a[i][j]);
}
}
maxsum = a[0][0]+a[0][1]+a[0][2]+a[1][1]+a[2][0]+a[2][1]+a[2][2]; //sum of first hourglass
for (i = 0; i < 4; i++){ // i is the top row of hourglass
for (j = 0; j < 4; j++){ //moves hourglass one to right
sum = 0; //resets sum for new hourglass
for (k = j; k < j+3; k++){ // value for column
sum += a[i][k]; //sum of first row
if (k == j+1){ // middle row of hourglass
sum += a[i+1][k]; //sum of middle row
}
m = i+2; // third row of hourglass
sum += a[m][k]; //sum of third row
}
if (sum > maxsum){ // if new sum is greater than maxsum
maxsum = sum; // sum becomes the new maxsum
}
}
}
printf("%d", maxsum);
}
(indentention got messed up @ end when copypaste, sry)
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 →
my solution for C (Sorry if it's messy, I'm fairly new to this):
(for testcases 3 & 7, maxsum is negative, so make sure it is not initilized to zero. I initilized to the first hourglass sum)
(indentention got messed up @ end when copypaste, sry)