Skip to content

Instantly share code, notes, and snippets.

@fenske
Created December 18, 2019 20:56
Show Gist options
  • Select an option

  • Save fenske/5edd6885007086970b54dd23cbe7424b to your computer and use it in GitHub Desktop.

Select an option

Save fenske/5edd6885007086970b54dd23cbe7424b to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
#define N 3
int main() {
int arr[N][N] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
int maxRow = 0;
int maxRowSum = arr[0][0] + arr[0][1] + arr[0][2];
for (int i = 1; i < N; i++)
{
int currRowSum = 0;
for (int j = 0; j < N; j++)
{
currRowSum += arr[i][j];
}
if (currRowSum > maxRowSum) {
maxRow = i;
maxRowSum = currRowSum;
}
}
int maxCol = 0;
int maxColSum = arr[0][0] + arr[1][0] + arr[2][0];
for (int j = 1; j < N; j++)
{
int currColSum = 0;
for (int i = 0; i < N; i++)
{
currColSum += arr[i][j];
}
if (currColSum > maxColSum) {
maxCol = j;
maxColSum = currColSum;
}
}
if (maxRow == maxCol) {
int maxSum = maxRowSum + maxColSum;
cout << "Max sum: " << maxSum << endl;
} else {
cout << "Max row #: " << maxRow << endl;
cout << "Max col #: " << maxCol << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment