Given a number N, the task is to print the triangle-separated pattern.
Triangle Separated Pattern: Pattern in which four triangles (left, down, right, up) are separated by forward and backward slash, see this below:
\*****/
*\***/*
**\*/**
***/***
**/*\**
*/***\*
/*****\
Note: N should be an odd number and the value of N should be more than 4.
Examples:
Input: N = 5
Output:
\***/
*\*/*
**/**
*/*\*
/***\
Input: N = 7
Output:
\*****/
*\***/*
**\*/**
***/***
**/*\**
*/***\*
/*****\
Approach: By observing the above pattern, when the indexes of rows and columns are equal, then ” is printed and when the sum of indexes of rows and columns is N, then ‘/’ is printed. Below is the recursive approach:
- Use two values i for rows and j for columns, which iterates from (0, 0) to (N-1, N-1) for printing the required pattern.
- Recursively iterates from (0, 0) to (N-1, N-1):
- Base Case: If indexes of rows and columns are greater than or equal to N is the termination condition for the given pattern.
if(i >= N) {
return 0;
}
if(j >= N) {
return 1;
}
- Print Statement: If the base case condition is not met, then print ‘/’, ” and ‘*’ on the basis of the below conditions:
if(i==j) {
print('')
}
else if(i + j == N-1) {
print('/')
}
else {
print('*')
}
- Recursive Call: At each recursive call(except the base case), return the recursive function for next iteration for rows and columns:
// Recursive call for rows
recursive_function(i, j+1, N)
// Recursive call for changing rows
recursive_function(i+1, j, N)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int printPattern(
int i, int j, int n)
{
if (j >= n) {
return 0;
}
if (i >= n) {
return 1;
}
if (j == i || j == n - 1 - i) {
if (i == n - 1 - j) {
cout << "/" ;
}
else {
cout << "\\" ;
}
}
else {
cout << "*" ;
}
if (printPattern(i, j + 1, n)
== 1) {
return 1;
}
cout << endl;
return printPattern(i + 1, 0, n);
}
int main()
{
int N = 9;
printPattern(0, 0, N);
return 0;
}
|
Java
class GFG{
static int printPattern(
int i, int j, int n)
{
if (j >= n) {
return 0 ;
}
if (i >= n) {
return 1 ;
}
if (j == i || j == n - 1 - i) {
if (i == n - 1 - j) {
System.out.print( "/" );
}
else {
System.out.print( "\\" );
}
}
else {
System.out.print( "*" );
}
if (printPattern(i, j + 1 , n)
== 1 ) {
return 1 ;
}
System.out.println();
return printPattern(i + 1 , 0 , n);
}
public static void main(String[] args)
{
int N = 9 ;
printPattern( 0 , 0 , N);
}
}
|
Python3
def printPattern(i,j, n):
if (j > = n) :
return 0
if (i > = n):
return 1
if (j = = i or j = = n - 1 - i):
if (i = = n - 1 - j):
print ( "/" ,end = "")
else :
print ( "\\",end=" ")
else :
print ( "*" ,end = "")
if (printPattern(i, j + 1 , n)
= = 1 ):
return 1
print ()
return printPattern(i + 1 , 0 , n)
if __name__ = = "__main__" :
N = 9
printPattern( 0 , 0 , N)
|
C#
using System;
class GFG{
static int printPattern(
int i, int j, int n)
{
if (j >= n) {
return 0;
}
if (i >= n) {
return 1;
}
if (j == i || j == n - 1 - i) {
if (i == n - 1 - j) {
Console.Write( "/" );
}
else {
Console.Write( "\\" );
}
}
else {
Console.Write( "*" );
}
if (printPattern(i, j + 1, n)
== 1) {
return 1;
}
Console.WriteLine();
return printPattern(i + 1, 0, n);
}
public static void Main(String[] args)
{
int N = 9;
printPattern(0, 0, N);
}
}
|
Javascript
<script>
function printPattern(i,j,n)
{
if (j >= n) {
return 0;
}
if (i >= n) {
return 1;
}
if (j == i || j == n - 1 - i) {
if (i == n - 1 - j) {
document.write( "/" );
}
else {
document.write( "\\" );
}
}
else {
document.write( "*" );
}
if (printPattern(i, j + 1, n)
== 1) {
return 1;
}
document.write( "<br>" );
return printPattern(i + 1, 0, n);
}
let N = 9;
printPattern(0, 0, N);
</script>
|
Output: \*******/
*\*****/*
**\***/**
***\*/***
****/****
***/*\***
**/***\**
*/*****\*
/*******\
Time Complexity: O(N2), as we are making recursive calls for n times, and each time the loop is executed for n times.
Auxiliary Space: 0(1), as no extra space is used.