Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Magic Land problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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