Header Ad

HackerEarth Magic Land problem solution

In this HackerEarth Magic Land problem solution, You are given a grid of size N * M where N and M denote the number of rows and columns respectively. Each cell of the grid is filled with some number.

Now, We define a quantity called Magic Number for each row and column as the maximum number of consecutive adjacent elements having the same number. Now the password of the entry gate is defined as the MAX of MR[i]*MC[j] where MR[i] and MC[j] refer to the Magic number of ith row and jth column respectively. Can you find the password?


HackerEarth Magic Land problem solution


HackerEarth Magic Land problem solution.

#include<bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--) {
int n,m;
cin>>n>>m;
int a[n][m];
int row = 1, col = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin>>a[i][j];
}
}
for(int i = 0; i < n; i++) {
int num = a[i][0], count = 0;
for(int j = 0; j < m; j++) {
if(a[i][j] == num) {
count++;
row = max(row, count);
}
else {
row = max(row, count);
count = 1;
num = a[i][j];
}
}
}

for(int i = 0; i < m; i++) {
int num = a[0][i], count = 0;
for(int j = 0; j < n; j++) {
if(a[j][i] == num) {
count++;
col = max(col, count);
}
else {
col = max(col, count);
count = 1;
num = a[j][i];
}
}
}
cout<<col*row<<"\n";
}
return 0;
}


Post a Comment

0 Comments