Mountain Sequence Pattern
Last Updated :
08 Mar, 2023
Given a number N, the task is to generate the pyramid sequence pattern which contains N pyramids one after the other as shown in the examples below.
Examples:
Input: N = 3
Output:
* * *
*** *** ***
***************
Input: N = 4
Output:
* * * *
*** *** *** ***
********************
Iterative Approach: The steps for an iterative approach to print the Mountain Sequence Pattern for a given number N:
- Run two nested loops.
- The outer loop will care for the row of the pattern.
- The inner loop will be caring for the column of the pattern.
- Take three variable k1, k2, and gap which helps in generating a pattern.
- After printing the row of the pattern update the value of k1 and k2 as:
- k1 = k1 + gap
- k2 = k2 + gap
Below is the implementation of the iterative approach:
C++
#include <iostream>
using namespace std;
void printPatt( int n)
{
int k1 = 3;
int k2 = 3;
int gap = 5;
for ( int i = 1; i <= 3; i++) {
for ( int j = 1;
j <= (5 * n); j++) {
if (j > k2 && i < 3) {
k2 += gap;
k1 += gap;
}
if (j >= k1 && j <= k2) {
cout << "*" ;
}
else {
cout << " " ;
}
}
if (i + 1 == 3) {
k1 = 1;
k2 = (5 * n);
}
else {
k1 = 3;
k2 = 3;
k1--;
k2++;
}
cout << endl;
}
}
int main()
{
int N = 5;
printPatt(N);
}
|
Java
class GFG{
static void printPatt( int n)
{
int k1 = 3 ;
int k2 = 3 ;
int gap = 5 ;
for ( int i = 1 ; i <= 3 ; i++)
{
for ( int j = 1 ; j <= ( 5 * n); j++)
{
if (j > k2 && i < 3 )
{
k2 += gap;
k1 += gap;
}
if (j >= k1 && j <= k2)
{
System.out.print( "*" );
}
else
{
System.out.print( " " );
}
}
if (i + 1 == 3 )
{
k1 = 1 ;
k2 = ( 5 * n);
}
else
{
k1 = 3 ;
k2 = 3 ;
k1--;
k2++;
}
System.out.println();
}
}
public static void main (String[] args)
{
int N = 5 ;
printPatt(N);
}
}
|
Python3
def printPatt(n):
k1 = 3 ; k2 = 3 ; gap = 5 ;
for i in range ( 1 , 4 ):
for j in range ( 1 , ( 5 * n) + 1 ):
if (j > k2 and i < 3 ):
k2 + = gap;
k1 + = gap;
if (j > = k1 and j < = k2):
print ( "*" , end = "");
else :
print ( " " , end = "");
print ( "\n" , end = "");
if (i + 1 = = 3 ):
k1 = 1 ;
k2 = ( 5 * n);
else :
k1 = 3 ;
k2 = 3 ;
k1 - = 1 ;
k2 + = 1 ;
print (end = "");
N = 5 ;
printPatt(N);
|
C#
using System;
class GFG{
static void printPatt( int n)
{
int k1 = 3;
int k2 = 3;
int gap = 5;
for ( int i = 1; i <= 3; i++)
{
for ( int j = 1; j <= (5 * n); j++)
{
if (j > k2 && i < 3)
{
k2 += gap;
k1 += gap;
}
if (j >= k1 && j <= k2)
{
Console.Write( "*" );
}
else
{
Console.Write( " " );
}
}
if (i + 1 == 3)
{
k1 = 1;
k2 = (5 * n);
}
else
{
k1 = 3;
k2 = 3;
k1--;
k2++;
}
Console.WriteLine();
}
}
public static void Main (String[] args)
{
int N = 5;
printPatt(N);
}
}
|
Javascript
<!-- Javascript program for the above approach. -->
<script>
function printPatt( n)
{
var k1 = 3;
var k2 = 3;
var gap = 5;
for (let i = 1; i <= 3; i++)
{
for (let j = 1; j <= (5 * n); j++)
{
if (j > k2 && i < 3)
{
k2 += gap;
k1 += gap;
}
if (j >= k1 && j <= k2)
{ document.write( "*" );
}
else
{
document.write( "  " );
}
}
if (i + 1 == 3)
{
k1 = 1;
k2 = (5 * n);
}
else
{
k1 = 3;
k2 = 3;
k1--;
k2++;
}
document.write( "<br>" );
}
}
var N=3;
printPatt(N);
</script>
<!-- This code in contributed by nirajgusain5 -->
|
Output
* * * * *
*** *** *** *** ***
*************************
Time Complexity: O(N)
Auxiliary Space: O(1)
Recursive Approach: The pattern can be generated using Recursion. Below are the steps:
- Run two nested loops.
- The outer loop will care for the row of the pattern.
- The inner loop will be caring for the column of the pattern.
- Apart from these, variables K1, K2, and gap are needed.
- K1 and K2 will cover the cases when the * is to be printed.
- The gap will cover the cases when spaces are to be printed.
- Recursively call the function fun(i, j + 1) for handling columns.
- Recursive call the function fun(i + 1, 0) for handling rows.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int k1 = 2;
int k2 = 2;
int gap = 5;
int printPattern(
int i, int j, int n)
{
if (j >= n) {
k1 = 2;
k2 = 2;
k1--;
k2++;
if (i == 2) {
k1 = 0;
k2 = n - 1;
}
return 0;
}
if (i >= 3) {
return 1;
}
if (j > k2) {
k1 += gap;
k2 += gap;
}
if (j >= k1
&& j <= k2
|| i == 2) {
cout << "*" ;
}
else {
cout << " " ;
}
if (printPattern(i, j + 1, n)
== 1) {
return 1;
}
cout << endl;
return printPattern(i + 1,
0, n);
}
int main()
{
int N = 3;
printPattern(0, 0, N * 5);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int k1 = 2 ;
static int k2 = 2 ;
static int gap = 5 ;
public static int printPattern( int i, int j, int n)
{
if (j >= n) {
k1 = 2 ;
k2 = 2 ;
k1--;
k2++;
if (i == 2 ) {
k1 = 0 ;
k2 = n - 1 ;
}
return 0 ;
}
if (i >= 3 ) {
return 1 ;
}
if (j > k2) {
k1 += gap;
k2 += gap;
}
if (j >= k1 && j <= k2 || i == 2 ) {
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 = 3 ;
printPattern( 0 , 0 , N * 5 );
}
}
|
Python3
k1 = 2
k2 = 2
gap = 5
def printPattern(i, j, n):
global k1
global k2
global gap
if (j > = n):
k1 = 2
k2 = 2
k1 - = 1
k2 + = 1
if (i = = 2 ):
k1 = 0
k2 = n - 1
return 0
if (i > = 3 ):
return 1
if (j > k2):
k1 + = gap
k2 + = gap
if (j > = k1 and j < = k2 or
i = = 2 ):
print ( "*" , end = "")
else :
print ( " " , end = "")
if (printPattern(i, j + 1 , n) = = 1 ):
return 1
print ()
return (printPattern(i + 1 , 0 , n))
N = 3
printPattern( 0 , 0 , N * 5 )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int k1 = 2;
static int k2 = 2;
static int gap = 5;
static int printPattern( int i, int j, int n)
{
if (j >= n)
{
k1 = 2;
k2 = 2;
k1--;
k2++;
if (i == 2)
{
k1 = 0;
k2 = n - 1;
}
return 0;
}
if (i >= 3)
{
return 1;
}
if (j > k2)
{
k1 += gap;
k2 += gap;
}
if (j >= k1 && j <= k2 || i == 2)
{
Console.Write( "*" );
}
else
{
Console.Write( " " );
}
if (printPattern(i, j + 1, n) == 1)
{
return 1;
}
Console.WriteLine();
return printPattern(i + 1, 0, n);
}
static void Main()
{
int N = 3;
printPattern(0, 0, N * 5);
}
}
|
Javascript
let k1 = 2;
let k2 = 2;
let gap = 5;
function printPattern(i, j, n)
{
if (j >= n) {
k1 = 2;
k2 = 2;
k1--;
k2++;
if (i == 2) {
k1 = 0;
k2 = n - 1;
}
return 0;
}
if (i >= 3) {
return 1;
}
if (j > k2) {
k1 += gap;
k2 += gap;
}
if (j >= k1 && j <= k2 || i == 2) {
console.log( "*" );
}
else {
console.log( "\xa0\xa0" );
}
if (printPattern(i, j + 1, n) == 1) {
return 1;
}
console.log( "<br>" );
return printPattern(i + 1, 0, n);
}
let N = 3;
printPattern(0, 0, N * 5);
|
Output
* * *
*** *** ***
***************
Time Complexity: O(N)
Auxiliary Space: O(N) for call stack
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...