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.
- Prepare
- Data Structures
- Arrays
- 2D Array - DS
- Discussions
2D Array - DS
2D Array - DS
Sort by
recency
|
3686 Discussions
|
Please Login in order to post a comment
this is my solution :
**My Code In Java: **
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; }
Your method of breaking down the 6x6 matrix into manageable 6x3 sections and using loops to calculate hourglass sums is a smart, structured solution—especially for fixed sizes. It's similar to how in Bowmasters, you might focus on specific strategies to collect gems efficiently within a known game mode or map. However, just like your matrix approach might not scale well for NxM matrices, a rigid strategy in Bowmasters might not adapt well across different challenges or updates. Flexibility is key in both cases for long-term success and efficiency.
My Java 8 Solution:
Here is problem solution in Python 2 and Python 3 programming https://datastructure.us/hackerrank-2d-array-ds-python-solution.html