Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Magic Land problem solution

YASH PAL, 31 July 202411 February 2026
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;
}
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes