 # Pattern printing

Given a number a, print the following pattern :

Input:
4

Output:
1111
1112
1122
1222
1223
1233
1234

Pls help. Last time when i posted the quesion, people told it was from contest. donno is still that contest is running.

Why would you make a new topic for that? You could’ve continued the discussion in the old topic.

1 Like

Can u send Sol. ?

``````void solve() {
int n;
cin >> n;
string level = string(n, '1');
cout << level << endl;
for(int i = 2; i <= n; i++) {
for(int j = n - 1; j >= i - 1; j--) {
level[j] = i + '0';
cout << level << endl;
}
}
}
``````
2 Likes

Anee wrong for n>9 .

You are right. I assumed n < 10 and proceeded. Generalizing for any n might be hard.

1 Like

U told u finded out the Sol. Pls share atleast now

Ok , my solution is too long , but wait here is updated @aneee004 's code

``````#define printvec(a) for(lli i=0;i<sz(a);i++)   cout<<a[i]<<" "; cout<<endl;

void solve() {
int n;
cin >> n;
vector<int>level(n,1);
printvec(level);
for(int i = 2; i <= n; i++) {
for(int j = n - 1; j >= i - 1; j--) {
level[j] = i;
printvec(level);
}
}
}

``````
2 Likes

@aneee004 I just use an array instead of string 1 Like

Oh xD. That was all the modification that was required.

P.S: In your macro, you might not want to add a space after `a[i]`?

2 Likes

Yes actually for more clearly visible I add space .

1 Like

I hope @chef_ka_baap understands the code .

1 Like

Yes thankz!

1 Like