Pattern Printing question asked in CGI Coding Round
Last Updated :
13 Mar, 2023
Write a program that receives a number as input and prints it in the following format as shown below.
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
Asked in CGI coding round
Approach: The approach is to see the problem, not as a single task but three tasks which, on combining, complete the main task. The three tasks are printing the left-half of the pattern, printing dashes(-), and printing the right-half of the pattern. Combining all three tasks, we would be able to print the pattern.
left-half of pattern
1*2*3*
--4*5*
----6*
A function printdashes() to print the "-".
right-half of
pattern
10*11*12
*8*9
7
Below is the implementation.
C++
Java
Python3
C#
PHP
Javascript
Output
1*2*3*10*11*12
--4*5*8*9
----6*7
Time Complexity: O(n2)
Auxiliary Space: O(1), As constant extra space is used.
Another approach :
C++
C
Java
Python3
def printPattern(row):
x = 1
z = (row * row) + 1
if row = = 1 :
col = 1
else :
col = (row * 4 ) - 1
for i in range ( 1 , row + 1 ):
t = z
for j in range ( 1 , col - ((i - 1 ) * 2 ) + 1 ):
if ((i * 2 ) - 2 > = j):
print (" ", end = " - ")
else :
if (col = = 1 ):
print (x, end = "")
elif (j < = col / 2 and j % 2 = = 1 ):
print (x, end = "")
x + = 1
elif (j > col / 2 and j % 2 = = 1 ):
print (t, end = "")
t + = 1
else :
print ( "*" , end = "")
z = (z - row) + i
print ()
row = 3
printPattern(row)
|
C#
using System;
class GFG
{
static void printPattern( int row)
{
int x = 1;
int z = (row * row) + 1;
int col = row == 1 ? 1 : (row * 4) - 1;
for ( int i = 1; i <= row; i++)
{
int t = z;
for ( int j = 1; j <= col -((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
Console.Write( "-" );
}
else
{
if (col == 1)
{
Console.Write(x);
}
else if (j <= col/2 && j % 2 == 1)
{
Console.Write(x);
x++;
}
else if (j > col/2 && j % 2 == 1)
{
Console.Write(t);
t++;
}
else
{
Console.Write( "*" );
}
}
}
z = (z - row) + i;
Console.Write( "\n" );
}
}
public static void Main(String[] args)
{
int row = 3;
printPattern(row);
}
}
|
Javascript
<script>
function printPattern(row)
{
var x = 1;
var z = (row * row) + 1;
var col = row == 1 ? 1 : (row * 4) - 1;
for ( var i = 1; i <= row; i++)
{
var t = z;
for ( var j = 1; j <= col -((i - 1) * 2); j++)
{
if ((i * 2) - 2 >= j)
{
document.write( "-" );
}
else
{
if (col == 1)
{
document.write(x);
}
else if (j <= col/2 && j % 2 == 1)
{
document.write(x);
x++;
}
else if (j > col/2 && j % 2 == 1)
{
document.write(t);
t++;
}
else
{
document.write( "*" );
}
}
}
z = (z - row) + i;
document.write( "<br>" );
}
}
var row = 3;
printPattern(row);
</script>
|
Output
1*2*3*10*11*12
--4*5*8*9
----6*7
Time Complexity: O(n2)
Auxiliary Space: O(1), As constant extra space is used.
Another approach :
C++
#include <bits/stdc++.h>
using namespace std;
void pattern( int n) {
int size = n * (n + 1);
int prev1 = 0;
int prev2 = size;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 2 * i; j++) cout << "-" ;
vector< int > l1;
for ( int j = prev1 + 1; j <= prev1 + n - i; j++) l1.push_back(j);
vector< int > l2;
for ( int j = prev2 - (n - i) + 1; j <= prev2; j++) l2.push_back(j);
for ( int j = 0; j < l1.size(); j++) cout << l1[j] << "*" ;
for ( int j = 0; j < l2.size(); j++) cout << l2[j] << "*" ;
cout << endl;
prev2 -= (n - i);
prev1 += (n - i);
}
}
int main() {
int n = 3;
pattern(n);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void pattern( int n)
{
int size = n * (n + 1 );
int prev1 = 0 ;
int prev2 = size;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < 2 * i; j++)
System.out.print( "-" );
List<Integer> l1 = new ArrayList<>();
for ( int j = prev1 + 1 ; j <= prev1 + n - i; j++)
l1.add(j);
List<Integer> l2 = new ArrayList<>();
for ( int j = prev2 - (n - i) + 1 ; j <= prev2;
j++)
l2.add(j);
for ( int j = 0 ; j < l1.size(); j++)
System.out.print(l1.get(j) + "*" );
for ( int j = 0 ; j < l2.size(); j++)
System.out.print(l2.get(j) + "*" );
System.out.println();
prev2 -= (n - i);
prev1 += (n - i);
}
}
public static void main(String[] args)
{
int n = 3 ;
pattern(n);
}
}
|
Python3
def pattern(n):
size = n * (n + 1 )
prev1 = 0
prev2 = size
for i in range (n):
print ( '-' * ( 2 * i), end = '')
l1 = [j for j in range (prev1 + 1 , prev1 + n - i + 1 )]
l2 = [j for j in range (prev2 - (n - i) + 1 ,prev2 + 1 )]
print ( * l1 + l2, sep = '*' )
prev2 - = (n - i)
prev1 + = (n - i)
n = 3
pattern(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void pattern( int n)
{
int size = n * (n + 1);
int prev1 = 0;
int prev2 = size;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < 2 * i; j++)
Console.Write( "-" );
List< int > l1 = new List< int >();
for ( int j = prev1 + 1; j <= prev1 + n - i; j++)
l1.Add(j);
List< int > l2 = new List< int >();
for ( int j = prev2 - (n - i) + 1; j <= prev2; j++)
l2.Add(j);
for ( int j = 0; j < l1.Count; j++)
Console.Write(l1[j] + "*" );
for ( int j = 0; j < l2.Count; j++)
Console.Write(l2[j] + "*" );
Console.WriteLine();
prev2 -= (n - i);
prev1 += (n - i);
}
}
public static void Main( string [] args)
{
int n = 3;
pattern(n);
}
}
|
Javascript
function pattern( n) {
let size = n * (n + 1);
let prev1 = 0;
let prev2 = size;
for (let i = 0; i < n; i++) {
for (let j = 0; j < 2 * i; j++)
console.log( "-" );
let l1=[];
for (let j = prev1 + 1; j <= prev1 + n - i; j++)
l1.push(j);
let l2=[];
for (let j = prev2 - (n - i) + 1; j <= prev2; j++)
l2.push(j);
for (let j = 0; j < l1.length; j++)
console.log( l1[j] + "*" );
for (let j = 0; j < l2.length; j++)
console.log( l2[j] + "*" );
console.log( "<br>" );
prev2 -= (n - i);
prev1 += (n - i);
}
}
let n = 3;
pattern(n);
|
Output
1*2*3*10*11*12*
--4*5*8*9*
----6*7*
Time Complexity: O(n2)
Auxiliary Space: O(1)
As constant extra space is used.
Another approach :
C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n = 4;
int temp_number = (n * n) + n;
int counter = 1;
for ( int i = 0; i < n; i++) {
vector< int > temp_list;
for ( int j = 0; j < n - i; j++) {
temp_list.push_back(counter);
temp_list.push_back(temp_number - counter + 1);
counter += 1;
}
sort(temp_list.begin(), temp_list.end());
for ( int k = 0; k < i; k++) {
cout << "--" ;
}
for ( int num : temp_list) {
cout << num;
if (num != temp_list.back()) {
cout << "*" ;
}
}
cout << endl;
}
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args)
{
int n = 4 ;
int temp_number = (n * n) + n;
int counter = 1 ;
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> temp_list
= new ArrayList<Integer>();
for ( int j = 0 ; j < n - i; j++) {
temp_list.add(counter);
temp_list.add(temp_number - counter + 1 );
counter += 1 ;
}
Collections.sort(temp_list);
for ( int k = 0 ; k < i; k++) {
System.out.print( "--" );
}
for ( int num : temp_list) {
System.out.print(num);
if (num
!= temp_list.get(temp_list.size()
- 1 )) {
System.out.print( "*" );
}
}
System.out.println();
}
}
}
|
Python3
n = 4
temp_number = (n * n) + n
counter = 1
for i in range (n):
temp_list = []
for j in range (n - i):
temp_list.append(counter)
temp_list.append(temp_number - counter + 1 )
counter + = 1
temp_list.sort()
temp_list = [ str (each) for each in temp_list]
[ print ( "--" , end = "") for k in range (i)]
print ( "*" .join(temp_list))
|
Javascript
let n = 4;
let temp_number = n * n + n;
let counter = 1;
for (let i = 0; i < n; i++) {
let temp_list = [];
for (let j = 0; j < n - i; j++) {
temp_list.push(counter);
temp_list.push(temp_number - counter + 1);
counter += 1;
}
temp_list.sort( function (a, b) { return a - b; });
let temp = "" ; for (let k = 0; k < i; k++) {
temp = temp+ "--" ;
}
for (let num of temp_list) {
temp= temp+ num.toString();
if (num != temp_list[temp_list.length - 1]) {
temp = temp + "*" ;
}
} console.log(temp);
}
|
C#
Output
1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
Time Complexity: O(n2)
Auxiliary Space: O(1)
As constant extra space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...