Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Print the pattern 1*2*5*6 –3*4

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given integer N, the task is to print an upside-down triangle where the left half is made of elements in the range [1, N*(N+1)/2] and the right half is made of elements in the range [N*(N+1)/2 + 1, N*(N+1)].

Examples:

Input: N = 3 
Output: 
1*2*3*10*11*12
   4*5*8*9
      6*7

Input: n = 4
Output:
1*2*3*4*17*18*19*20
   5*6*7*14*15*16
      8*9*12*13
       10*11 

Approach:

Looking at the pattern we can easily observed that it is following a ZIG-ZAG pattern first few numbers are printed from the top row to the bottom and then numbers are printed from the bottom row to the top increasingly

Pattern

Hyphen Pattern: No of spaces in ith line is i*2 [Here i is 0 based]

Since numbers are in increasing order in this zigzag format we can store those numbers in a 2d dynamic array and print them accordingly with respect to the condition of number of spaces at each row.

Follow the steps mentioned below to implement the idea:

  • Create a 2D vector arr[] of size N*N, so that the matrix would be of N*N.
  • Then, traverse the array from top to bottom and push numbers (N-i) numbers for the ith line.
  • Similarly, traverse from bottom to top and push elements in the same manner from bottom to top.
  • Start a loop and traverse the column and print array:
    • Then, print spaces before each row in increasing order by traversing the length of i*2.
    • Print * after each element in a row

Below is the implementation of the above approach.

C++

// C++ code to print the above pattern

#include <bits/stdc++.h>
using namespace std;

// Function to print the pattern
void printPattern(int n)
{
    // Initializing 2D array of size N
    vector<vector<int> > arr(n);

    int num = 1;

    // First Traversal top to bottom
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i; j++) {
            arr[i].push_back(num++);
        }
    }

    // Second Traversal bottom to top
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < n - i; j++) {
            arr[i].push_back(num++);
        }
    }

    for (int i = 0; i < n; i++) {
        for (int ch = 0; ch < (i * 2);

             // Space is printed before each
             // row in a increasing order
             ch++) {
            cout << ' ';
        }
        for (int j = 0; j < arr[i].size(); j++) {
            cout << arr[i][j];

            // * is printed after the
            // last element in a row
            if (j != arr[i].size() - 1) {
                cout << '*';
            }
        }
        cout << endl;
    }
}

// Driver Code
int main()
{
    int N = 3;

    // Function call
    printPattern(N);
    return 0;
}

Java

// Java code to print the above pattern

import java.util.*;

class GFG {

    // Function to print the pattern
    public static void printPattern(int n)
    {
        List<List<Integer> > list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(new ArrayList<>());
        }

        int num = 1;

        // First Traversal top to bottom
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                list.get(i).add(num++);
            }
        }

        // Second Traversal bottom to top
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j < n - i; j++) {
                list.get(i).add(num++);
            }
        }

        for (int i = 0; i < n; i++) {
            for (int ch = 0; ch < (i * 2); ch++) {
                // '-'s is printed before each row
                // in a increasing order
                System.out.print(' ');
            }
            for (int j = 0; j < list.get(i).size(); j++) {
                System.out.print(list.get(i).get(j));
                if (j != list.get(i).size() - 1) {
                    // * is not printed after
                    // the last element in a
                    // row
                    System.out.print('*');
                }
            }
            System.out.println();
        }
    }

    // Driver code
    public static void main(String[] args)
    {
        int N = 3;

        // Function call
        printPattern(N);
    }
}

Python3

# Python code to print the above pattern

# Function to print the pattern
def printPattern(n):
    l = []
    num = 1

    # First Traversal top to bottom
    for i in range(n):
        tem = []
        for j in range((i)*2):
            tem.append(' ')
        for j in range(n - i):
            tem.append(num)
            tem.append('*')
            num += 1
        l.append(tem)

    # Second Traversal bottom to top
    for i in range(n-1, -1, -1):
        tem = []
        for j in range(0, n-i):
            tem.append(num)
            tem.append('*')
            num += 1
        tem.pop()
        l[i] += tem

    for row in l:
        for i in row:
            print(i, end ="")
        print()


# Driver code
if __name__ == '__main__':
    N = 3

    # Function call
    printPattern(N)

C#

// C# code to print the above pattern
using System;
using System.Collections.Generic;

public class GFG 
{
  
  // Function to print the pattern
  public static void printPattern(int n)
  {
    List<List<int> > list = new List<List<int> >();
    for (int i = 0; i < n; i++) {
      list.Add(new List<int>());
    }

    int num = 1;

    // First Traversal top to bottom
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i; j++) {
        list[i].Add(num++);
      }
    }

    // Second Traversal bottom to top
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < n - i; j++) {
        list[i].Add(num++);
      }
    }

    for (int i = 0; i < n; i++) {
      for (int ch = 0; ch < (i * 2); ch++) {
        // '-'s is printed before each row
        // in a increasing order
        Console.Write(" ");
      }
      for (int j = 0; j < list[i].Count; j++) {
        Console.Write(list[i][j]);
        if (j != list[i].Count - 1) {
          // * is not printed after
          // the last element in a
          // row
          Console.Write("*");
        }
      }
      Console.WriteLine();
    }
  }

  // Driver code
  static public void Main()
  {
    int N = 3;

    // Function call
    printPattern(N);
  }
}

// This code is contributed by Rohit Pradhan

Javascript

    // JavaScript code to implement the approach

 // Function to print the pattern
  function printPattern(n)
  {
    list = new Array();;
    for (let i = 0; i < n; i++) {
      list.push(new Array());
    }

    let num = 1;

    // First Traversal top to bottom
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n - i; j++) {
        list[i].push(num++);
      }
    }

    // Second Traversal bottom to top
    for (let i = n - 1; i >= 0; i--) {
      for (let j = 0; j < n - i; j++) {
        list[i].push(num++);
      }
    }

    for (let i = 0; i < n; i++) 
    {
      for (let ch = 0; ch < (i * 2); ch++)
      {
      
        // '-'s is printed before each row
        // in a increasing order
        console.log(" ");
      }
      for (let j = 0; j < list[i].length; j++)
      {
        console.log(list[i][j]);
        if (j != list[i].length - 1)
        {
        
          // * is not printed after
          // the last element in a
          // row
          console.log("*");
        }
      }
      console.log("<br/>");
    }
  }

    // Driver Code
        let N = 3;

        // Function call
        printPattern(N);
Output

1*2*3*10*11*12
  4*5*8*9
    6*7

Time Complexity: O(N2)
Auxiliary Space: O(N2) 

My Personal Notes arrow_drop_up
Last Updated : 16 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials