Table of contents
1.
Introduction
2.
Implementation
3.
Algorithm
4.
Code
4.1.
C++
4.2.
JAVA
4.3.
Python
5.
Frequently Asked Questions
5.1.
What is the time complexity of the above code?
5.2.
What is the space complexity of the above code?
5.3.
Can the code be optimized further?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Full Pyramid Pattern

Author Lucifer go
0 upvote

Introduction

The task is to print the full pyramid number pattern up to the number of rows N. The user gives the value of the number of rows (N) as an input, and then the output is shown to the user in the form of a complete pyramid or N rows as shown in the figure below:-

Implementation

For implementing the code, we take the following variables: -

  • int N - represents the number of rows to be printed
  • int row - variable to iterate through N rows.
  • Int np - initialized as 1, denotes the number of numbers represented in a row.
  • int space - a variable that iterates through the position with spaces.
  • int number - a variable that iterates through the position with digits.

Algorithm

From the pattern, we can observe that row zero has 1 element, row one has 3 elements, row two has 5 elements, and so on. Therefore, the nth row will have 2n+1 elements. Thus np is incremented by 2 in each iteration. 

Therefore, a loop is needed to run from 0 to N to execute the code for N rows to implement the code. 

For each row, first, the required number of spaces are printed within a for loop that runs from N-1 to row+1 for each iteration.

Then, the required number of numbers is printed within a for loop that runs from 0 to np for each row. The digit to be printed is given by np-row for that row in each iteration.

Code

C++

#include <bits/stdc++.h> 
using namespace std; 
int main()
{
int N, row, space, number; 
int np=1; 
cin>>N; 
for(row=0; row<N;row++)
{
    for(space=N-1; space>row; space--) //prints the required number of spaces
    {
    cout<<" "; 
    } 
    for(number=0; number<np; number++) //prints the required number of digits
    {
    cout<<np-row; 
    }
    np=np+2; //np is incremented by 2 in every iteration 
    cout<<"\n"; //newline
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

JAVA

import java.util.Scanner; 
import java.util.*; 
import java.lang.*; 
import java.io.*;

class P31
{
public static void main (String[] args) throws java.lang.Exception
  {
    Scanner cs=new Scanner(System.in);
    int N, row, space, number; 
    int np=1; 
    N=cs.nextInt();
    
    for(row=0; row<N;row++)
    {
      for(space=N-1; space>row; space--) //prints the required number of spaces
        {System.out.print(" ");}
      for(number=0; number<np; number++) //prints the required number of digits
        {System.out.print(np-row);}
      np+=2; 
      System.out.println();
    }
    
cs.close();
  }
}

Python

N = int(input())
np = 1
for row in range(0,N):
# decrements by -1 in each iteration
for space in range(N-1,row,-1):
print(" ", end="")
for number in range(0, np):
print(np-row,end="")
np+=2
print("\r")

Frequently Asked Questions

What is the time complexity of the above code?

The time complexity for the above code is O(n2) as it contains a for loop within a for loop.

What is the space complexity of the above code?

The space complexity of the above code is O(1) as it does not require any extra space.

Can the code be optimized further?

No, as we require at least two loops, one for printing the required number of digits and another for changing the row.

Conclusion

This article extensively discussed the Full Pyramid Pattern problem and the algorithm to solve it in the time complexity of O(N2) and space complexity of O(N). We hope that this blog has helped you enhance your knowledge regarding using nested for loops. You can check out more problems on Coding_Ninjas_ProblemsCodingNinjas_TestSeries, and CodingNinjas_Puzzles.

If you would like to learn more, check out our articles on Code studio. Do upvote our blog to help other ninjas grow.

“Happy Coding!”

Live masterclass