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 Special Matrix problem solution

YASH PAL, 31 July 2024
In this HackerEarth Special Matrix problem solution, you are given a square matrix of size n (it will be an odd integer). Rows are indexed 0 to n-1 from top to bottom and columns are indexed 0 to n-1 from left to right. The matrix consists of only ‘*’ and ‘.’. ‘*’ appears only once in the matrix while all other positions are occupied by ‘.’
Your task is to convert this matrix to a special matrix by following any of two operations any number of times.
you can swap any two adjecent rows, i and i+1 (0<= i < n-1)
you can swap any two adjecent columns, j and j+1 (0<= j < n-1)
Special Matrix is one that contains ‘*’ at the middle of the matrix. e.g following is a size 7 special matrix
    …….
    …….
    …….
    …*…
    …….
    …….
    …….
Output no of steps to convert given matrix to special matrix.
HackerEarth Special Matrix problem solution

HackerEarth Special Matrix problem solution.

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<fstream>
#include<cstdlib>
#include<cassert>
#include<vector>
#include<algorithm>
#include<stack>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<ctime>
#define LL long long
#define ULL unsigned long long
#define F firs
#define S second
#define pb push_back
#define FOR(i,lb,ub) for(i=lb;i<=ub;i++)
#define RFOR(i,ub,lb) for(i=ub;i>=lb;i--)
#define FORS(it,v) for(it=v.begin();it!=v.end();it++)
#define st_clk double st=clock();
#define end_clk double en=clock();
#define show_time cout<<"tTIME="<<(en-st)/CLOCKS_PER_SEC<<endl;
#define sc(n) scanf("%d",&n)
#define scst(n) scanf("%s",n)
#define f_in(st) freopen(st,"r",stdin);
#define f_out(st) freopen(st,"w",stdout);
LL gcd(LL a, LL b) { return b?gcd(b,a%b):a; }
using namespace std;
#ifndef ONLINE_JUDGE
inline int getchar_unlocked() { return getchar(); }
#endif
template <class T>
inline void r_f(T &p)
{
char c;
int neg=0;
while ((c=getchar_unlocked()) < 48 || c > 57)
if (c==45)
neg=1;
p=c-48;
while ((c=getchar_unlocked()) >= 48 && c <= 57) p=p*10+c-48;
if (neg) p*=-1;
}
int main()
{
#ifndef ONLINE_JUDGE
f_in("sample_in.txt");
//f_out("out.txt");
#endif
int t,i,j;
cin>>t;
while (t--)
{
int n,ans;
cin>>n;
int x , y;
char mat[100][100];
FOR(i,0,n-1)
{
cin>>mat[i];
FOR(j,0,n-1)
{
if (mat[i][j] == '*')
{
x = i;
y = j;
}
}
}
x = abs(x - n/2);
y = abs(y - n/2);
cout<<x+y<<endl;
}
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
©2025 Programmingoneonone | WordPress Theme by SuperbThemes