Friday, October 10, 2025
HomeData Modelling & AIPrint the pattern 2 2 1 1 $2 1

Print the pattern 2 2 1 1 $2 1

Given the number N, the task is to print a pattern such that in each line all the digits from N to 1 are present in decreasing order and the frequency of the elements in ith line is N-i (the lines are 0 based i.e., i varies in the range [0, N-1]).

Note: Instead of printing a new line print a “$” without quotes. After printing the total output, the end of the line is expected.

Examples:

Input: 2
Output: 2 2 1 1 $2 1 $

Input: 3
Output: 3 3 3 2 2 2 1 1 1 $3 3 2 2 1 1 $3 2 1 $

Approach: Follow the steps to solve this problem:

  • Run a loop from k = 0 to N-1:
    • Run a nested loop from i = N to 1:
      • Run a loop from 0 to N-k and print i.
    • After executing the i loop for every k, print ‘$‘.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the pattern
void printPat(int n)
{
    int i, j, k;
  
    for (k = 0; k < n; k++) {
        for (i = n; i > 0; i--) {
            for (j = 0; j < n - k; j++) {
                cout << i << " ";
            }
        }
        cout << '$';
    }
}
  
// Driver Code
int main()
{
    int N = 2;
  
    // Function Call
    printPat(2);
  
    return 0;
}


Java




// Java code to implement the approach
  
import java.io.*;
  
class GFG {
  
    // Function to print the pattern
    static void printPat(int n)
    {
        int i, j, k;
  
        for (k = 0; k < n; k++) {
            for (i = n; i > 0; i--) {
                for (j = 0; j < n - k; j++) {
                    System.out.print(i + " ");
                }
            }
            System.out.print("$");
        }
    }
  
    public static void main(String[] args)
    {
        int N = 2;
  
        // Function call
        printPat(2);
    }
}
  
// This code is contributed by lokeshmvs21.


Python3




# Python code to implement the approach
  
# Function to print the pattern
def printPat(n):
    for k in range(n):
        for i in range(n,0,-1):
            for j in range(0,n-k):
                print(i,end=" ")
        print("$",end="")
  
# Driver Code
N=2
  
# Function Call
printPat(N)
  
# This code is contributed by Pushpesh Raj.


C#




// C# code to implement the approach
  
using System;
  
public class GFG{
    
      // Function to print the pattern
    static void printPat(int n)
    {
        int i, j, k;
   
        for (k = 0; k < n; k++) {
            for (i = n; i > 0; i--) {
                for (j = 0; j < n - k; j++) {
                    Console.Write(i + " ");
                }
            }
            Console.Write("$");
        }
    }
  
    static public void Main (){
  
        // Code
          int N = 2;
   
        // Function call
        printPat(2);
    }
}
  
// This code is contributed by lokesh.


Javascript




// Javascript code to implement the approach
  
// Function to print the pattern
function printPat(n)
{
    let i = 0, j = 0, k = 0;
  
    for (k = 0; k < n; k++) {
        for (i = n; i > 0; i--) {
            for (j = 0; j < n - k; j++) {
                console.log(i + " ");
            }
        }
        console.log('$');
    }
}
  
// Driver Code
let N = 2;
  
// Function Call
printPat(2);
  
// This code is contributed by Samim Hossain Mondal.


Output

2 2 1 1 $2 1 $

Time Complexity: O(N3)
Auxiliary Space:  O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS