Open In App

Print the pattern 2 2 1 1 $2 1

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads