Mountain Sequence Pattern

The Mountain Sequence Pattern is a sequence of asterisks (*) arranged in a specific pattern that resembles a mountain. This pattern is formed by incrementing the number of asterisks in each row, starting from one, and then decrementing the number of asterisks until the middle row is reached. After the middle row, the number of asterisks is again incremented until the maximum number is reached.

The goal of this pattern is to create a visual representation of a mountain, where the number of asterisks represents the height of the mountain. The pattern is symmetric, with equal numbers of asterisks on both sides of the mountain.

Example:

Let's take an example to understand the Mountain Sequence Pattern better. Consider the given size of the mountain as 4.

```    *         *         *         *
* *       * *       * *       * *
* * *     * * *     * * *     * * *
* * * *   * * * *   * * * *   * * * *
* * * * * * * * * * * * * * * * * * * *
```

In this example, the mountain has a height of 5 rows. Each row is composed of asterisks (*) and spaces. The number of asterisks in each row increases from 1 to 5 and then decreases back to 1, creating a mountain-like shape. The spaces are included to align the asterisks and create the symmetric pattern.

Algorithm:

1. Start with the given size of the mountain.
2. Set the height of the mountain as a constant value.
3. Iterate through each row of the mountain from 0 to the height:
• Iterate through each element in the row from 0 to the given size:
• Include initial spaces to align the elements.
• Display the mountain element (asterisk).
• Include end spaces to align the elements.
• Move to the next line.
4. Repeat the above steps for all test cases.

Pseudocode

``````mountainSequence(n)
if n <= 0
return
height = 5
for i = 0 to height
for j = 0 to n
includeSpace(height - i)
includeStar(i)
includeSpace(height - i)
print newline
end

includeSpace(n)
for i = 0 to n
print space
end

includeStar(n)
for i = 0 to n
print asterisk followed by space
end``````

Code Solution

Here given code implementation process.

``````// C program for
// Mountain Sequence Pattern
#include <stdio.h>

// Include n space
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
printf(" ");
}
}
// Include n star with space
void includeStar(int n)
{
for (int i = 0; i < n; ++i)
{
printf("* ");
}
}
void mountainSequence(int n)
{
if (n <= 0)
{
return;
}
int height = 5;
printf("\n Given Size : %d \n", n);
// Outer loop is executing in through by mountain height
for (int i = 0; i <= height; ++i)
{
// Inner loop is executing in through by 0..n
for (int j = 0; j < n; ++j)
{
// Include initial space
includeSpace(height - i);
// Display the mountain element
includeStar(i);
// Include end space
includeSpace(height - i);
}
// Include new line
printf("\n");
}
}
int main(int argc, char const *argv[])
{
// Test Cases
mountainSequence(1);
mountainSequence(2);
mountainSequence(4);
mountainSequence(3);
return 0;
}``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````/*
Java Program
Mountain Sequence Pattern
*/
public class MountainPattern
{
// Include n space
public void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" ");
}
}
// Include n star with space
public void includeStar(int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print("* ");
}
}
public void mountainSequence(int n)
{
if (n <= 0)
{
return;
}
int height = 5;
System.out.println("\n Given Size : " + n);
// Outer loop is executing in through by mountain height
for (int i = 0; i <= height; ++i)
{
// Inner loop is executing in through by 0..n
for (int j = 0; j < n; ++j)
{
// Include initial space
includeSpace(height - i);
// Display the mountain element
includeStar(i);
// Include end space
includeSpace(height - i);
}
// Include new line
System.out.print("\n");
}
}
public static void main(String[] args)
{
MountainPattern task = new MountainPattern();
// Test Cases
task.mountainSequence(1);
task.mountainSequence(2);
task.mountainSequence(4);
task.mountainSequence(3);
}
}``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Mountain Sequence Pattern
*/
class MountainPattern
{
public:
// Include n space
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
cout << " ";
}
}
// Include n star with space
void includeStar(int n)
{
for (int i = 0; i < n; ++i)
{
cout << "* ";
}
}
void mountainSequence(int n)
{
if (n <= 0)
{
return;
}
int height = 5;
cout << "\n Given Size : " << n << endl;
// Outer loop is executing in through by mountain height
for (int i = 0; i <= height; ++i)
{
// Inner loop is executing in through by 0..n
for (int j = 0; j < n; ++j)
{
// Include initial space
this->includeSpace(height - i);
// Display the mountain element
this->includeStar(i);
// Include end space
this->includeSpace(height - i);
}
// Include new line
cout << "\n";
}
}
};
int main()
{
MountainPattern *task = new MountainPattern();
// Test Cases
task->mountainSequence(1);
task->mountainSequence(2);
task->mountainSequence(4);
task->mountainSequence(3);
return 0;
}``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````// Include namespace system
using System;
/*
Csharp Program
Mountain Sequence Pattern
*/
public class MountainPattern
{
// Include n space
public void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" ");
}
}
// Include n star with space
public void includeStar(int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write("* ");
}
}
public void mountainSequence(int n)
{
if (n <= 0)
{
return;
}
int height = 5;
Console.WriteLine("\n Given Size : " + n);
// Outer loop is executing in through by mountain height
for (int i = 0; i <= height; ++i)
{
// Inner loop is executing in through by 0..n
for (int j = 0; j < n; ++j)
{
// Include initial space
this.includeSpace(height - i);
// Display the mountain element
this.includeStar(i);
// Include end space
this.includeSpace(height - i);
}
// Include new line
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MountainPattern task = new MountainPattern();
// Test Cases
task.mountainSequence(1);
task.mountainSequence(2);
task.mountainSequence(4);
task.mountainSequence(3);
}
}``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````<?php
/*
Php Program
Mountain Sequence Pattern
*/
class MountainPattern
{
// Include n space
public	function includeSpace(\$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ");
}
}
// Include n star with space
public	function includeStar(\$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo("* ");
}
}
public	function mountainSequence(\$n)
{
if (\$n <= 0)
{
return;
}
\$height = 5;
echo("\n Given Size : ".\$n."\n");
// Outer loop is executing in through by mountain height
for (\$i = 0; \$i <= \$height; ++\$i)
{
// Inner loop is executing in through by 0..n
for (\$j = 0; \$j < \$n; ++\$j)
{
// Include initial space
\$this->includeSpace(\$height - \$i);
// Display the mountain element
\$this->includeStar(\$i);
// Include end space
\$this->includeSpace(\$height - \$i);
}
// Include new line
echo("\n");
}
}
}

function main()
{
\$task = new MountainPattern();
// Test Cases
\$task->mountainSequence(1);
\$task->mountainSequence(2);
\$task->mountainSequence(4);
\$task->mountainSequence(3);
}
main();``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````/*
Node JS Program
Mountain Sequence Pattern
*/
class MountainPattern
{
// Include n space
includeSpace(n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" ");
}
}
// Include n star with space
includeStar(n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write("* ");
}
}
mountainSequence(n)
{
if (n <= 0)
{
return;
}
var height = 5;
console.log("\n Given Size : " + n);
// Outer loop is executing in through by mountain height
for (var i = 0; i <= height; ++i)
{
// Inner loop is executing in through by 0..n
for (var j = 0; j < n; ++j)
{
// Include initial space
this.includeSpace(height - i);
// Display the mountain element
this.includeStar(i);
// Include end space
this.includeSpace(height - i);
}
// Include new line
process.stdout.write("\n");
}
}
}

function main()
{
var task = new MountainPattern();
// Test Cases
task.mountainSequence(1);
task.mountainSequence(2);
task.mountainSequence(4);
task.mountainSequence(3);
}
main();``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````#    Python 3 Program
#    Mountain Sequence Pattern
class MountainPattern :
#  Include n space
def includeSpace(self, n) :
i = 0
while (i < n) :
print(" ", end = "")
i += 1

#  Include n star with space
def includeStar(self, n) :
i = 0
while (i < n) :
print("* ", end = "")
i += 1

def mountainSequence(self, n) :
if (n <= 0) :
return

height = 5
print("\n Given Size : ", n)
#  Outer loop is executing in through by mountain height
i = 0
while (i <= height) :
#  Inner loop is executing in through by 0..n
j = 0
while (j < n) :
#  Include initial space
self.includeSpace(height - i)
#  Display the mountain element
self.includeStar(i)
#  Include end space
self.includeSpace(height - i)
j += 1

#  Include new line
print(end = "\n")
i += 1

def main() :
task = MountainPattern()
#  Test Cases
task.mountainSequence(1)
task.mountainSequence(2)
task.mountainSequence(4)
task.mountainSequence(3)

if __name__ == "__main__": main()``````

input

`````` Given Size :  1

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

Given Size :  2

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

Given Size :  4

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

Given Size :  3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````#    Ruby Program
#    Mountain Sequence Pattern
class MountainPattern
#  Include n space
def includeSpace(n)
i = 0
while (i < n)
print(" ")
i += 1
end

end

#  Include n star with space
def includeStar(n)
i = 0
while (i < n)
print("* ")
i += 1
end

end

def mountainSequence(n)
if (n <= 0)
return
end

height = 5
print("\n Given Size : ", n, "\n")
#  Outer loop is executing in through by mountain height
i = 0
while (i <= height)
#  Inner loop is executing in through by 0..n
j = 0
while (j < n)
#  Include initial space
self.includeSpace(height - i)
#  Display the mountain element
self.includeStar(i)
#  Include end space
self.includeSpace(height - i)
j += 1
end

#  Include new line
print("\n")
i += 1
end

end

end

def main()
task = MountainPattern.new()
#  Test Cases
task.mountainSequence(1)
task.mountainSequence(2)
task.mountainSequence(4)
task.mountainSequence(3)
end

main()``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *
``````
``````/*
Scala Program
Mountain Sequence Pattern
*/
class MountainPattern()
{
// Include n space
def includeSpace(n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" ");
i += 1;
}
}
// Include n star with space
def includeStar(n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print("* ");
i += 1;
}
}
def mountainSequence(n: Int): Unit = {
if (n <= 0)
{
return;
}
var height: Int = 5;
println("\n Given Size : " + n);
// Outer loop is executing in through by mountain height
var i: Int = 0;
while (i <= height)
{
// Inner loop is executing in through by 0..n
var j: Int = 0;
while (j < n)
{
// Include initial space
includeSpace(height - i);
// Display the mountain element
includeStar(i);
// Include end space
includeSpace(height - i);
j += 1;
}
// Include new line
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MountainPattern = new MountainPattern();
// Test Cases
task.mountainSequence(1);
task.mountainSequence(2);
task.mountainSequence(4);
task.mountainSequence(3);
}
}``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````/*
Swift 4 Program
Mountain Sequence Pattern
*/
class MountainPattern
{
// Include n space
func includeSpace(_ n: Int)
{
var i = 0;
while (i < n)
{
print(" ", terminator: "");
i += 1;
}
}
// Include n star with space
func includeStar(_ n: Int)
{
var i = 0;
while (i < n)
{
print("* ", terminator: "");
i += 1;
}
}
func mountainSequence(_ n: Int)
{
if (n <= 0)
{
return;
}
let height = 5;
print("\n Given Size : ", n);
// Outer loop is executing in through by mountain height
var i = 0;
while (i <= height)
{
// Inner loop is executing in through by 0..n
var j = 0;
while (j < n)
{
// Include initial space
self.includeSpace(height - i);
// Display the mountain element
self.includeStar(i);
// Include end space
self.includeSpace(height - i);
j += 1;
}
// Include new line
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let task = MountainPattern();
// Test Cases
task.mountainSequence(1);
task.mountainSequence(2);
task.mountainSequence(4);
task.mountainSequence(3);
}
main();``````

input

`````` Given Size :  1

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

Given Size :  2

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

Given Size :  4

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

Given Size :  3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````
``````/*
Kotlin Program
Mountain Sequence Pattern
*/
class MountainPattern
{
// Include n space
fun includeSpace(n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" ");
i += 1;
}
}
// Include n star with space
fun includeStar(n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print("* ");
i += 1;
}
}
fun mountainSequence(n: Int): Unit
{
if (n <= 0)
{
return;
}
val height: Int = 5;
println("\n Given Size : " + n);
// Outer loop is executing in through by mountain height
var i: Int = 0;
while (i <= height)
{
// Inner loop is executing in through by 0..n
var j: Int = 0;
while (j < n)
{
// Include initial space
this.includeSpace(height - i);
// Display the mountain element
this.includeStar(i);
// Include end space
this.includeSpace(height - i);
j += 1;
}
// Include new line
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: MountainPattern = MountainPattern();
// Test Cases
task.mountainSequence(1);
task.mountainSequence(2);
task.mountainSequence(4);
task.mountainSequence(3);
}``````

input

`````` Given Size : 1

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

Given Size : 2

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

Given Size : 4

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

Given Size : 3

*         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *``````

Time Complexity:

The time complexity of the Mountain Sequence Pattern code is O(height * n), where height is a constant value (5) and n is the given size of the mountain. The nested loops iterate through all the elements in each row, which is proportional to the height and the size of the mountain.

Resultant Output:

The provided code generates the Mountain Sequence Pattern for different test cases. Here are the outputs for the given sizes:

Given Size: 1

```    *
* *
* * *
* * * *
* * * * *
```

Given Size: 2

```    *         *
* *       * *
* * *     * * *
* * * *   * * * *
* * * * * * * * * *
```

Given Size: 4

```    *         *         *         *
* *       * *       * *       * *
* * *     * * *     * * *     * * *
* * * *   * * * *   * * * *   * * * *
* * * * * * * * * * * * * * * * * * * *
```

Given Size: 3

```    *         *         *
* *       * *       * *
* * *     * * *     * * *
* * * *   * * * *   * * * *
* * * * * * * * * * * * * * *
```

The output demonstrates the Mountain Sequence Pattern for different sizes of the mountain, showcasing the increment and decrement of asterisks in each row, creating the mountain-like shape.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.