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 Printing patterns problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Printing patterns problem solution You are required to form a matrix of size r x c where r is the number of rows and c is the number of columns. You are required to form the waves of numbers around the provided center, (Ci, Cj) (0-based indexing).
HackerEarth Printing patterns problem solution

HackerEarth Printing patterns problem solution.

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6;

int dx[] = {-1 , -1 , -1, 0 , 0 , 1 , 1 , 1};// xdirection
int dy[] = {-1 , 0 , 1 , -1, 1 , -1, 0, 1};// ydirection

int row , col , x , y;
int pat[1001][1001];
bool visit[1001][1001];

struct Point{
int x , y , d;
Point(int a ,int b , int c){
x = a , y = b , d = c;
}
};

bool check(int x , int y){
if(x < 0 || y < 0 || x >= row || y >= col || visit[x][y] == true){
return false;
}
return true;
}

int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> row >> col;
cin >> x >> y;
queue < Point > Q;
Point p(x , y , 0) ;
pat[x][y] = 0;
Q.push(p);
visit[x][y] = 1;
while(Q.size() > 0){
Point P = Q.front();
Q.pop();
int x = P.x;
int y = P.y;
int d = P.d;
for(int i = 0 ; i < 8 ; i++){
int X = x + dx[i];
int Y = y + dy[i];
if(check(X , Y)){
Point A(X , Y , d + 1);
visit[X][Y] = 1;
pat[X][Y] = A.d;
Q.push(A);
}
}
}
for(int i = 0 ; i < row ; i++){
for(int j = 0 ; j < col ; j++){
cout << pat[i][j] << " ";
}
cout << endl;
}

return 0;

}

Second solution

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

const int maxn = 3e2 + 14;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int r, c, x, y;
cin >> r >> c >> x >> y;
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
cout << max(abs(i - x), abs(j - y)) << " n"[j == c - 1];
}
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