-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0002.cpp
More file actions
36 lines (31 loc) · 839 Bytes
/
E0002.cpp
File metadata and controls
36 lines (31 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Problem Code: https://www.hackerrank.com/challenges/2d-array/problem
*/
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
int hourGlass(int i, int j, vector< vector<int> > &arr) {
int sum = 0;
for (int m = i; m < i + 3; m++)
for (int n = j; n < j + 3; n++)
sum += arr[m][n];
sum -= arr[i + 1][j] + arr[i + 1][j + 2];
return sum;
}
int hourglassSum(vector< vector<int> > &arr) {
int maxSum = numeric_limits<int>::min();
for (int i = 0; i < arr.size() - 2; i++)
for (int j = 0; j < arr[i].size() - 2; j++)
maxSum = max(hourGlass(i, j, arr), maxSum);
return maxSum;
}
int main() {
vector< vector<int> > arr(6, vector<int>(6));
for (int i = 0; i < arr.size(); i++)
for (int j = 0; j < arr[i].size(); j++)
cin >> arr[i][j];
cout << hourglassSum(arr);
return 0;
}