Print symmetric double triangle pattern
Last Updated :
01 Mar, 2023
Given a value n, we need to print the following pattern accordingly, using only constant extra space.
Examples:
Input : n = 1
Output : x
Input : n = 2
Output :
x
x x
x
Input: n = 5
Output:
x
x
o x
o x
x o x o x
x o
x o
x
x
Input: n = 6
Output:
x
x
o x
o x
x o x
x o x x o x
x o x
x o
x o
x
x
Input : n = 7;
Output :
x
x
o x
o x
x o x
x o x
x o x o x o x
x o x
x o x
x o
x o
x
x
Input : n = 8;
Output :
x
x
o x
o x
x o x
x o x
o x o x
x o x o o x o x
x o x o
x o x
x o x
x o
x o
x
x
We can divide this problem into 3 parts:
1) Print upper half with n-1 lines for odd n or n-2 lines for even n.
2) Print middle lines, 1 line for odd n or 3 lines for even n.
3) Print lower half, with n-1 lines for odd n or n-2 lines for even n.
For such complex patterns it may be easier if we can use 1-based indexing
and separate functions to print characters beginning with x or o.
C++
#include <iostream>
using namespace std;
void printx( int n)
{
for ( int i = 1; i <= n; i++) {
if (i % 2 != 0)
cout << "x " ;
else
cout << "o " ;
}
return ;
}
void printo( int n)
{
for ( int i = 1; i <= n; i++) {
if (i % 2 != 0)
cout << "o " ;
else
cout << "x " ;
}
return ;
}
void printPattern( int n)
{
int x = n;
if (n % 2 == 0)
x = x - 1;
int p = n - 1;
int s = 1;
for ( int i = 1; i <= (x - 1) / 2; i++) {
for ( int j = 1; j <= p; j++) {
cout << " " ;
}
if (i % 2 != 0)
printx(s);
else
printo(s);
cout << endl;
p++;
for ( int j = 1; j <= p; j++)
cout << " " ;
if (i % 2 != 0)
printx(s);
else
printo(s);
cout << endl;
p--;
s++;
}
if (n % 2 == 0) {
for ( int i = 1; i <= p; i++)
cout << " " ;
if (n % 4 != 0)
printx(n / 2);
else
printo(n / 2);
cout << endl;
}
if (n % 2 != 0)
printx(n);
else {
if (n % 4 != 0) {
printx(n / 2);
printx(n / 2);
}
else {
printx(n / 2);
printo(n / 2);
}
}
cout << endl;
if (n % 2 == 0) {
cout << " " ;
printx(n / 2);
cout << endl;
}
p = 1;
if (n % 2 == 0) {
x--;
p = 2;
}
int q = x / 2;
for ( int i = 1; i <= x; i++) {
for ( int j = 1; j <= p; j++)
cout << " " ;
printx(q);
if (i % 2 == 0)
q--;
cout << endl;
p++;
}
cout << endl;
}
int main()
{
int n = 7;
printPattern(n);
n = 8;
printPattern(n);
return 0;
}
|
Java
class GFG
{
static void printx( int n)
{
for ( int i = 1 ; i <= n; i++) {
if (i % 2 != 0 )
System.out.print( "x " );
else
System.out.print( "o " );
}
return ;
}
static void printo( int n)
{
for ( int i = 1 ; i <= n; i++) {
if (i % 2 != 0 )
System.out.print( "o " );
else
System.out.print( "x " );
}
return ;
}
static void printPattern( int n)
{
int x = n;
if (n % 2 == 0 )
x = x - 1 ;
int p = n - 1 ;
int s = 1 ;
for ( int i = 1 ; i <= (x - 1 ) / 2 ; i++) {
for ( int j = 1 ; j <= p; j++) {
System.out.print( " " );
}
if (i % 2 != 0 )
printx(s);
else
printo(s);
System.out.println();
p++;
for ( int j = 1 ; j <= p; j++)
System.out.print( " " );
if (i % 2 != 0 )
printx(s);
else
printo(s);
System.out.println();
p--;
s++;
}
if (n % 2 == 0 ) {
for ( int i = 1 ; i <= p; i++)
System.out.print( " " );
if (n % 4 != 0 )
printx(n / 2 );
else
printo(n / 2 );
System.out.println();
}
if (n % 2 != 0 )
printx(n);
else {
if (n % 4 != 0 ) {
printx(n / 2 );
printx(n / 2 );
}
else {
printx(n / 2 );
printo(n / 2 );
}
}
System.out.println();
if (n % 2 == 0 ) {
System.out.print( " " );
printx(n / 2 );
System.out.println();
}
p = 1 ;
if (n % 2 == 0 ) {
x--;
p = 2 ;
}
int q = x / 2 ;
for ( int i = 1 ; i <= x; i++) {
for ( int j = 1 ; j <= p; j++)
System.out.print( " " );
printx(q);
if (i % 2 == 0 )
q--;
System.out.println();
p++;
}
System.out.println();
}
public static void main (String[] args)
{
int n = 7 ;
printPattern(n);
n = 8 ;
printPattern(n);
}
}
|
Python3
def printx(n):
for i in range ( 1 , n + 1 ):
if (i % 2 ! = 0 ):
print ( "x " , end = "")
else :
print ( "o " , end = "")
return
def printo(n):
for i in range ( 1 , n + 1 ):
if (i % 2 ! = 0 ):
print ( "o " , end = "")
else :
print ( "x " , end = "")
return
def printPattern(n):
x = n
if (n % 2 = = 0 ):
x = x - 1
p = n - 1
s = 1
for i in range ( 1 , (x - 1 ) / / 2 + 1 ):
for j in range ( 1 , p + 1 ):
print ( " " , end = "")
if (i % 2 ! = 0 ):
printx(s)
else :
printo(s)
print ()
p + = 1
for j in range ( 1 , p + 1 ):
print ( " " , end = "")
if (i % 2 ! = 0 ):
printx(s)
else :
printo(s)
print ()
p - = 1
s + = 1
if (n % 2 = = 0 ):
for i in range ( 1 , p + 1 ):
print ( " " , end = "")
if (n % 4 ! = 0 ):
printx(n / / 2 )
else :
printo(n / / 2 )
print ()
if (n % 2 ! = 0 ):
printx(n)
else :
if (n % 4 ! = 0 ):
printx(n / / 2 )
printx(n / / 2 )
else :
printx(n / / 2 )
printo(n / / 2 )
print ()
if (n % 2 = = 0 ):
print ( " " , end = "")
printx(n / / 2 )
print ()
p = 1
if (n % 2 = = 0 ):
x - = 1
p = 2
q = x / / 2
for i in range ( 1 , x + 1 ):
for j in range ( 1 , p + 1 ):
print ( " " , end = "")
printx(q)
if (i % 2 = = 0 ):
q - = 1
print ()
p + = 1
print ()
n = 7
printPattern(n)
n = 8
printPattern(n)
|
Javascript
<script>
function printx(n) {
for ( var i = 1; i <= n; i++) {
if (i % 2 !== 0) document.write( "x " );
else document.write( "o " );
}
return ;
}
function printo(n) {
for ( var i = 1; i <= n; i++) {
if (i % 2 !== 0) document.write( "o " );
else document.write( "x " );
}
return ;
}
function printPattern(n) {
var x = n;
if (n % 2 === 0) x = x - 1;
var p = n - 1;
var s = 1;
for ( var i = 1; i <= (x - 1) / 2; i++) {
for ( var j = 1; j <= p; j++) {
document.write( " " );
}
if (i % 2 !== 0) printx(s);
else printo(s);
document.write( "<br>" );
p++;
for ( var j = 1; j <= p; j++) document.write( " " );
if (i % 2 != 0) printx(s);
else printo(s);
document.write( "<br>" );
p--;
s++;
}
if (n % 2 === 0) {
for ( var i = 1; i <= p; i++) document.write( " " );
if (n % 4 !== 0) printx(n / 2);
else printo(n / 2);
document.write( "<br>" );
}
if (n % 2 !== 0) printx(n);
else {
if (n % 4 !== 0) {
printx(n / 2);
printx(n / 2);
} else {
printx(n / 2);
printo(n / 2);
}
}
document.write( "<br>" );
if (n % 2 === 0) {
document.write( " " );
printx(n / 2);
document.write( "<br>" );
}
p = 1;
if (n % 2 === 0) {
x--;
p = 2;
}
var q = x / 2;
for ( var i = 1; i <= x; i++) {
for ( var j = 1; j <= p; j++) document.write( " " );
printx(q);
if (i % 2 === 0) q--;
document.write( "<br>" );
p++;
}
document.write( "<br>" );
}
var n = 7;
printPattern(n);
n = 8;
printPattern(n);
</script>
|
C#
using System;
class GFG
{
static void PrintX( int n)
{
for ( int i = 1; i <= n; i++)
{
if (i % 2 != 0)
Console.Write( "x " );
else
Console.Write( "o " );
}
return ;
}
static void PrintO( int n)
{
for ( int i = 1; i <= n; i++)
{
if (i % 2 != 0)
Console.Write( "o " );
else
Console.Write( "x " );
}
return ;
}
static void PrintPattern( int n)
{
int x = n;
if (n % 2 == 0)
x = x - 1;
int p = n - 1;
int s = 1;
for ( int i = 1; i <= (x - 1) / 2; i++)
{
for ( int j = 1; j <= p; j++)
{
Console.Write( " " );
}
if (i % 2 != 0)
PrintX(s);
else
PrintO(s);
Console.WriteLine();
p++;
for ( int j = 1; j <= p; j++)
Console.Write( " " );
if (i % 2 != 0)
PrintX(s);
else
PrintO(s);
Console.WriteLine();
p--;
s++;
}
if (n % 2 == 0)
{
for ( int i = 1; i <= p; i++)
Console.Write( " " );
if (n % 4 != 0)
PrintX(n / 2);
else
PrintO(n / 2);
Console.WriteLine();
}
if (n % 2 != 0)
PrintX(n);
else
{
if (n % 4 != 0)
{
PrintX(n / 2);
PrintX(n / 2);
}
else
{
PrintX(n / 2);
PrintO(n / 2);
}
}
Console.WriteLine();
if (n % 2 == 0)
{
Console.Write( " " );
PrintX(n / 2);
Console.WriteLine();
}
p = 1;
if (n % 2 == 0)
{
x--;
p = 2;
}
int q = x / 2;
for ( int i = 1; i <= x; i++)
{
for ( int j = 1; j <= p; j++)
Console.Write( " " );
PrintX(q);
if (i % 2 == 0)
q--;
Console.WriteLine();
p++;
}
Console.WriteLine();
}
public static void Main (String[] args)
{
int n = 7;
PrintPattern(n);
n = 8;
PrintPattern(n);
}
}
|
Output:
x
x
o x
o x
x o x
x o x
x o x o x o x
x o x
x o x
x o
x o
x
x
x
x
o x
o x
x o x
x o x
o x o x
x o x o o x o x
x o x o
x o x
x o x
x o
x o
x
x
Time Complexity: .
Auxiliary Space: O(1) because it is using constant space for variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...