Created
August 31, 2018 12:07
-
-
Save anon767/94bfd295f1e5aa0de168fa5fcd9d3f95 to your computer and use it in GitHub Desktop.
hackerrank #1 https://www.hackerrank.com/challenges/2d-array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int BOARDSIZE = 16; | |
int sumVector(vector<int> arr){ | |
int sum_of_elems = 0; | |
for (auto& n : arr) | |
sum_of_elems += n; | |
return sum_of_elems; | |
} | |
// Complete the hourglassSum function below. | |
int hourglassSum(vector<vector<int>> arr) { | |
int max = -2147483646; | |
for (int i=0;i < BOARDSIZE/4; i++){ | |
for(int j=0;j < BOARDSIZE/4; j++){ | |
int summed = sumVector({arr[i][j], arr[i][j+1], arr[i][j+2], arr[i+1][j+1],arr[i+2][j],arr[i+2][j+1],arr[i+2][j+2]}); | |
if(max < summed) | |
max = summed; | |
} | |
} | |
return max; | |
} | |
int main() | |
{ | |
ofstream fout(getenv("OUTPUT_PATH")); | |
vector<vector<int>> arr(6); | |
for (int i = 0; i < 6; i++) { | |
arr[i].resize(6); | |
for (int j = 0; j < 6; j++) { | |
cin >> arr[i][j]; | |
} | |
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
} | |
int result = hourglassSum(arr); | |
fout << result << "\n"; | |
fout.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment