Related Articles

Related Articles

Mountain Sequence Pattern
  • Last Updated : 03 Dec, 2020

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:

  1. Run two nested loops.
  2. Outer loop will care for the row of the pattern.
  3. Inner loop will be caring for the column of the pattern.
  4. Take three variable k1, k2, and gap which helps in generating a pattern.
  5. After printing row of pattern update the value of k1 and k2 as: 
    • k1 = k1 + gap
    • k2 = k2 + gap

Below is the implementation of the iterative approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to create the mountain
// sequence pattern
void printPatt(int n)
{
    int k1 = 3;
    int k2 = 3;
    int gap = 5;
 
    // Outer loop to handle the row
    for (int i = 1; i <= 3; i++) {
 
        // Inner loop to handle the
        // Coloumn
        for (int j = 1;
             j <= (5 * n); j++) {
 
            if (j > k2 && i < 3) {
                k2 += gap;
                k1 += gap;
            }
 
            // Condition to print the
            // star in mountain pattern
            if (j >= k1 && j <= k2) {
                cout << "*";
            }
            else {
                cout << " ";
            }
        }
 
        // Condition to adjust the value of
        // K1 and K2 for printing desire
        // Pattern
        if (i + 1 == 3) {
            k1 = 1;
            k2 = (5 * n);
        }
        else {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 5;
 
    // Function call
    printPatt(N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
class GFG{
 
// Function to create the mountain
// sequence pattern
static void printPatt(int n)
{
    int k1 = 3;
    int k2 = 3;
    int gap = 5;
 
    // Outer loop to handle the row
    for(int i = 1; i <= 3; i++)
    {
         
       // Inner loop to handle the
       // Coloumn
       for(int j = 1; j <= (5 * n); j++)
       {
          if (j > k2 && i < 3)
          {
              k2 += gap;
              k1 += gap;
          }
           
          // Condition to print the
          // star in mountain pattern
          if (j >= k1 && j <= k2)
          {
              System.out.print("*");
          }
          else
          {
              System.out.print(" ");
          }
       }
        
       // Condition to adjust the value of
       // K1 and K2 for printing desire
       // Pattern
       if (i + 1 == 3)
       {
           k1 = 1;
           k2 = (5 * n);
       }
       else
       {
           k1 = 3;
           k2 = 3;
           k1--;
           k2++;
       }
       System.out.println();
    }
}
     
// Driver code
public static void main (String[] args)
{
     
    // Given Number N
    int N = 5;
 
    // Function call
    printPatt(N);
}
}
 
// This code is contributed by Pratima Pandey

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to create the mountain
# sequence pattern
def printPatt(n):
 
    k1 = 3; k2 = 3; gap = 5;
 
    # Outer loop to handle the row
    for i in range(1, 4):
 
        # Inner loop to handle the
        # Coloumn
        for j in range(1, (5 * n) + 1):
 
            if (j > k2 and i < 3):
                k2 += gap;
                k1 += gap;
             
            # Condition to print the
            # star in mountain pattern
            if (j >= k1 and j <= k2):
                print("*", end = "");
            else:
                print(" ", end = "");
        print("\n", end = "");
             
        # Condition to adjust the value of
        # K1 and K2 for printing desire
        # Pattern
        if (i + 1 == 3):
            k1 = 1;
            k2 = (5 * n);
         
        else:
            k1 = 3;
            k2 = 3;
            k1 -= 1;
            k2 += 1;
    print(end = "");
     
# Driver Code
 
# Given Number N
N = 5;
 
# Function call
printPatt(N);
 
# This code is contributed by Code_Mech

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
class GFG{
 
// Function to create the mountain
// sequence pattern
static void printPatt(int n)
{
    int k1 = 3;
    int k2 = 3;
    int gap = 5;
 
    // Outer loop to handle the row
    for(int i = 1; i <= 3; i++)
    {
         
        // Inner loop to handle the
        // Coloumn
        for(int j = 1; j <= (5 * n); j++)
        {
            if (j > k2 && i < 3)
            {
                k2 += gap;
                k1 += gap;
            }
             
            // Condition to print the
            // star in mountain pattern
            if (j >= k1 && j <= k2)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
             
        // Condition to adjust the value of
        // K1 and K2 for printing desire
        // Pattern
        if (i + 1 == 3)
        {
            k1 = 1;
            k2 = (5 * n);
        }
        else
        {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
        }
        Console.WriteLine();
    }
}
     
// Driver code
public static void Main (String[] args)
{
     
    // Given Number N
    int N = 5;
 
    // Function call
    printPatt(N);
}
}
 
// This code is contributed by shivanisinghss2110

chevron_right


Output: 

  *    *    *    *    *  
 ***  ***  ***  ***  *** 
*************************

 

Time Complexity: O(N)
Recursive Approach: The pattern can be generated using Recursion. Below are the steps:



  1. Run two nested loops.
  2. Outer loop will care for the row of the pattern.
  3. Inner loop will be caring for the column of the pattern.
  4. Apart from these, variables K1, K2, and gap are needed.
  5. K1, K2 will cover the cases when the * is to be printed.
  6. Gap will cover the cases when spaces are to be printed.
  7. Recursively call the function fun(i, j + 1) for handling columns.
  8. Recursive call the function fun(i + 1, 0) for handling rows.

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
int k1 = 2;
int k2 = 2;
int gap = 5;
 
// Function to print pattern
// recursively
int printPattern(
    int i, int j, int n)
{
 
    // Base Case
    if (j >= n) {
        k1 = 2;
        k2 = 2;
        k1--;
        k2++;
        if (i == 2) {
            k1 = 0;
            k2 = n - 1;
        }
        return 0;
    }
 
    // Condition to check row limit
    if (i >= 3) {
        return 1;
    }
 
    // Condition for assigning gaps
    if (j > k2) {
        k1 += gap;
        k2 += gap;
    }
 
    // Conditions to print *
    if (j >= k1
            && j <= k2
        || i == 2) {
 
        cout << "*";
    }
 
    // Else print ' '
    else {
 
        cout << " ";
    }
 
    // Recursive call for columns
    if (printPattern(i, j + 1, n)
        == 1) {
        return 1;
    }
 
    cout << endl;
 
    // Recursive call for rows
    return printPattern(i + 1,
                        0, n);
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 3;
 
    // Function Call
    printPattern(0, 0, N * 5);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the
// above approach
class GFG{
 
static int k1 = 2;
static int k2 = 2;
static int gap = 5;
     
// Function to print pattern
// recursively
public static int printPattern(int i,
                               int j,
                               int n)
{      
  // Base Case
  if (j >= n)
  {
    k1 = 2;
    k2 = 2;
    k1--;
    k2++;
    if (i == 2)
    {
      k1 = 0;
      k2 = n - 1;
    }
    return 0;
  }
 
  // Condition to check
  // row limit
  if (i >= 3)
  {
    return 1;
  }
 
  // Condition for assigning gaps
  if (j > k2)
  {
    k1 += gap;
    k2 += gap;
  }
 
  // Conditions to print *
  if (j >= k1 && j <= k2 || i == 2)
  {
    System.out.print("*");
  }
 
  // Else print ' '
  else
  {
    System.out.print(" ");
  }
 
  // Recursive call for columns
  if (printPattern(i, j + 1, n) == 1)
  {
    return 1;
  }
 
  System.out.println();
 
  // Recursive call for rows
  return printPattern(i + 1, 0, n);
}
 
// Driver code
public static void main(String[] args)
{
  // Given Number N
  int N = 3;
 
  // Function Call
  printPattern(0, 0, N * 5);
}
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the
# above approach
k1 = 2
k2 = 2
gap = 5
 
# Function to print pattern
# recursively
def printPattern(i, j, n):
   
    global k1
    global k2
    global gap
 
    # Base Case
    if(j >= n):
        k1 = 2
        k2 = 2
        k1 -= 1
        k2 += 1
        if(i == 2):
            k1 = 0
            k2 = n - 1
        return 0
 
    # Condition to check row limit
    if(i >= 3):
        return 1
 
    # Condition for assigning gaps
    if(j > k2):
        k1 += gap
        k2 += gap
 
    # Conditions to print *
    if(j >= k1 and j <= k2 or
       i == 2):
        print("*", end = "")
 
    # Else print ' '
    else:
        print(" ", end = "")
 
    # Recursive call for columns
    if(printPattern(i, j + 1, n) == 1):
        return 1
 
    print()
 
    # Recursive call for rows
    return (printPattern(i + 1, 0, n))
 
# Driver Code
 
# Given Number N
N = 3
 
# Function Call
printPattern(0, 0, N * 5)
 
#This code is contributed by avanitrachhadiya2155

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG {
     
    static int k1 = 2;
    static int k2 = 2;
    static int gap = 5;
 
    // Function to print pattern
    // recursively
    static int printPattern(int i, int j, int n)
    {      
      // Base Case
      if (j >= n)
      {
        k1 = 2;
        k2 = 2;
        k1--;
        k2++;
        if (i == 2)
        {
          k1 = 0;
          k2 = n - 1;
        }
        return 0;
      }
      
      // Condition to check
      // row limit
      if (i >= 3)
      {
        return 1;
      }
      
      // Condition for assigning gaps
      if (j > k2)
      {
        k1 += gap;
        k2 += gap;
      }
      
      // Conditions to print *
      if (j >= k1 && j <= k2 || i == 2)
      {
        Console.Write("*");
      }
      
      // Else print ' '
      else
      {
        Console.Write(" ");
      }
      
      // Recursive call for columns
      if (printPattern(i, j + 1, n) == 1)
      {
        return 1;
      }
      
      Console.WriteLine();
      
      // Recursive call for rows
      return printPattern(i + 1, 0, n);
    
   
  // Driver code
  static void Main()
  {
     
      // Given Number N
      int N = 3;
      
      // Function Call
      printPattern(0, 0, N * 5);
  }
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Output: 

  *    *    *  
 ***  ***  *** 
***************

 

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :