1.
Introduction
2.
Problem Statement
3.
Approach: Iteration
3.1.
Code
3.2.
Complexity Analysis
4.
4.1.
Why is time complexity O(N2)?
4.2.
Can we input a negative number?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Rectangle Pattern Using Number in Ascending Order

Rhythm Jain
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

There are numerous problems out there that make you scratch your brains but occasionally there are some on the easier side as well. In this article, we are going to look at one such problem Rectangle Pattern Using Number in Ascending Order.

So let us move to the actual problem statement.

## Problem Statement

Given a non-negative number N as input, print its rectangular pattern. For example, if N=5, the design would be:

12345

12345

12345

12345

12345

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Approach: Iteration

We can simply use two for loops to iterate over numbers and print the numbers. It is quite a naive and easy problem.

Let's See Its implementation.

### Code

``````#include<iostream>
using namespace std;
int main()
{

int N=5;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++){
cout<<j;
}

cout<<"\n";
}
}``````

Output

``````12345
12345
12345
12345
12345``````

### Complexity Analysis

Time Complexity: O(N2)

We are iterating over two nested for loops, and each ranges from 0 to N.

Space Complexity: O(1)

No extra space is used.

### Why is time complexity O(N2)?

Time complexity is O(N2) because we are iterating over two nested for loops, and each ranges from 0 to N. So when we sum, it is something like this:
=> N+N+N+Nâ€¦â€¦ (N Times)
=> N*N
=> O(N2)

### Can we input a negative number?

No, we cannot input negative numbers since it is a problem constraint that the number must be non-negative.

## Conclusion

In this article, we have learned how to create rectangle patterns using numbers in ascending order in C++. We have given a brief idea of our problem statement of how we will approach this problem. We also explained it is working and how it will go through step by step.