Why are my getting the wrong answer for the “Gift Rift” question.
It works with all of the examples they provided.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int r, c;
bool flag = false;
cin >> r >> c;
int ** matrix = new int*[r];
for (int a = 0; a < r; a++){
matrix[a] = new int[c];
for (int b = 0; b < c; b++){
cin >> matrix[a][b];
}
}
pair<int, int> * rows = new pair<int, int>[r];
for (int a = 0; a < r; a++){
for (int b = 0; b < c; b++){
if (rows[a].first > matrix[a][b] || !rows[a].first){
rows[a].first = matrix[a][b];
rows[a].second = b;
}
}
}
pair<int, int> columns;
for (int a = 0; a < c; a++){
columns.first = -1;
for (int b = 0; b < r; b++){
if (matrix[b][a] > columns.first){
columns.first = matrix[b][a];
columns.second = b;
}
}
if (columns.first == rows[columns.second].first && rows[columns.second].second == a){
cout << columns.first << endl;
flag = true;
break;
}
}
if (flag == false){
cout << "GUESS" << endl;
}
return 0;
}