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
|
3706 Discussions
|
Please Login in order to post a comment
Easy to read JAVA solution:
C++11.
We need to:
Traverse a 6x6 2D array.
For each possible “hourglass” (shape of 7 cells), calculate its sum.
Track and return the maximum sum.
Here’s a clean solution:
include
using namespace std;
int hourglassSum(vector> arr) { int maxSum = INT_MIN; // since values can be negative
}
int main() { vector> arr(6, vector(6)); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { cin >> arr[i][j]; } } cout << hourglassSum(arr) << endl; return 0; }
✅ Explanation:
We only go up to index 3 (not 5), since an hourglass needs 3 rows and 3 columns.
At each (i, j), we compute the hourglass sum.
Keep updating maxSum.
Return the largest sum found.
🔹 For the sample input in your problem, this outputs 19.
max_sum = -float("inf")
The 2D Array – DS problem tests how you handle multi-dimensional arrays efficiently, especially for pattern extraction like hourglass sums. In the same way, planning an Umrah from Montréal requires organizing multiple elements (flights, hotels, guidance) in a structured way, ensuring everything fits together smoothly for the best experience.The 2D Array – DS problem tests how you handle multi-dimensional arrays efficiently, especially for pattern extraction like hourglass sums. In the same way, planning an Umrah from Montréal requires organizing multiple elements (flights, hotels, guidance) in a structured way, ensuring everything fits together smoothly for the best experience.