Print the pattern 1*2*5*6 –3*4
Last Updated :
16 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void printPattern( int n)
{
vector<vector< int > > arr(n);
int num = 1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n - i; j++) {
arr[i].push_back(num++);
}
}
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);
ch++) {
cout << ' ' ;
}
for ( int j = 0; j < arr[i].size(); j++) {
cout << arr[i][j];
if (j != arr[i].size() - 1) {
cout << '*' ;
}
}
cout << endl;
}
}
int main()
{
int N = 3;
printPattern(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
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 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n - i; j++) {
list.get(i).add(num++);
}
}
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++) {
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 ) {
System.out.print( '*' );
}
}
System.out.println();
}
}
public static void main(String[] args)
{
int N = 3 ;
printPattern(N);
}
}
|
Python3
def printPattern(n):
l = []
num = 1
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)
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 ()
if __name__ = = '__main__' :
N = 3
printPattern(N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
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;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n - i; j++) {
list[i].Add(num++);
}
}
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++) {
Console.Write( " " );
}
for ( int j = 0; j < list[i].Count; j++) {
Console.Write(list[i][j]);
if (j != list[i].Count - 1) {
Console.Write( "*" );
}
}
Console.WriteLine();
}
}
static public void Main()
{
int N = 3;
printPattern(N);
}
}
|
Javascript
function printPattern(n)
{
list = new Array();;
for (let i = 0; i < n; i++) {
list.push( new Array());
}
let num = 1;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i; j++) {
list[i].push(num++);
}
}
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++)
{
console.log( " " );
}
for (let j = 0; j < list[i].length; j++)
{
console.log(list[i][j]);
if (j != list[i].length - 1)
{
console.log( "*" );
}
}
console.log( "<br/>" );
}
}
let N = 3;
printPattern(N);
|
Output
1*2*3*10*11*12
4*5*8*9
6*7
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Share your thoughts in the comments
Please Login to comment...