Posted on by Kalkicode
Code Pattern

Print reverse character bridge pattern

In this article, we will explore the problem of printing a reverse character bridge pattern. The pattern consists of characters arranged in a bridge-like structure, with each row displaying characters in a reverse order. We will provide an explanation of the problem, a suitable example, an algorithm, pseudocode, and an explanation of the resultant output with the time complexity of the code.

Introduction

The problem is to print a bridge pattern formed by characters in reverse order. The pattern starts with 'A' and gradually builds up to the given input 'n', displaying the characters in reverse order on each subsequent row. The pattern is symmetric, with a bridge-like structure formed by the characters.

Problem Statement

Given a positive integer 'n', we need to print a reverse character bridge pattern using the first 'n' characters of the English alphabet.

The pattern consists of 'n' rows, with each row displaying a series of characters. The first row starts with 'A' and ends with 'A'. The second row starts with 'A' and ends with 'B'. This pattern continues until the last row, which starts and ends with the same character, 'A'. The characters between the start and end characters form an intermediate pattern.

The task is to write a program that takes an input 'n' and prints the reverse character bridge pattern accordingly.

Example

Let's take an example where 'n' is 7:

Given n: 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

In this example, the pattern consists of 7 rows. Each row is displayed with the characters in reverse order, forming a bridge-like structure. The intermediate spaces are included to maintain the symmetry of the pattern.

Algorithm

The algorithm for printing the reverse character bridge pattern can be summarized as follows:

  1. Check if the input 'n' is within the valid range (greater than 0 and less than or equal to 26).
  2. Iterate through 'i' from 0 to 'n-1', representing the current row.
  3. Print the left side pattern by iterating through 'j' from 0 to 'n-i-1' and printing the character ('A' + j).
  4. Print the intermediate spaces by iterating through 'j' from 0 to 'i+i' and printing a space character.
  5. Print the right side pattern by iterating through 'j' from 'A' + (n - i - 1) down to 'A' and printing the character.
  6. Print a new line to move to the next row.
  7. Repeat steps 3-6 for each row.

Pseudocode

function printBridgePattern(n):
    if n <= 0 or n > 26:
        return
    for i in range(0, n):
        for j in range(0, n - i):
            print character ('A' + j)
        for j in range(0, i + i):
            print a space character
        for j in range('A' + (n - i - 1), 'A' - 1, -1):
            print character j
        print a new line

Code Solution

Here given code implementation process.

// C Program
// Print reverse character bridge pattern
#include <stdio.h>

void printBridgePattern(int n)
{
	if (n <= 0 || n > 26)
	{
		return;
	}
	// Loop controlling variables
	int i = 0;
	int j = 0;
	// Display given n
	printf("\nGiven n : %d \n\n", n);
	// iterating the loop through by size n
	for (i = 0; i < n; ++i)
	{
		// Print left side pattern
		for (j = 0; j < n - i; ++j)
		{
			printf("%c", ('A' + j));
		}
		for (j = 0; j < i + i; ++j)
		{
			// Include intermediate space
			printf(" ");
		}
		// Print right side pattern
		for (j = 'A' + (n - i - 1); j >= 'A'; --j)
		{
			printf("%c", j);
		}
		// Include new line
		printf("\n");
	}
}
int main(int argc, char const *argv[])
{
	// Test cases
	printBridgePattern(7);
	printBridgePattern(10);
	return 0;
}

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Java program for
// Print reverse character bridge pattern
public class BridgePattern
{
	public void printBridgePattern(int n)
	{
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Display given n
		System.out.print("\nGiven n : " + n + " \n\n");
		// iterating the loop through by size n
		for (i = 0; i < n; ++i)
		{
			// Print left side pattern
			for (j = 0; j < n - i; ++j)
			{
				System.out.print( (char)('A' + j));
			}
			for (j = 0; j < i + i; ++j)
			{
				// Include intermediate space
				System.out.print(" ");
			}
			// Print right side pattern
			for (j = 'A' + (n - i - 1); j >= 'A'; --j)
			{
				System.out.print((char) j);
			}
			// Include new line
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		BridgePattern task = new BridgePattern();
		// Test cases
		task.printBridgePattern(7);
		task.printBridgePattern(10);
	}
}

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Print reverse character bridge pattern
class BridgePattern
{
	public: void printBridgePattern(int n)
	{
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Display given n
		cout << "\nGiven n : " << n << " \n\n";
		// iterating the loop through by size n
		for (i = 0; i < n; ++i)
		{
			// Print left side pattern
			for (j = 0; j < n - i; ++j)
			{
				cout << (char)('A' + j);
			}
			for (j = 0; j < i + i; ++j)
			{
				// Include intermediate space
				cout << " ";
			}
			// Print right side pattern
			for (j = 'A' + (n - i - 1); j >= 'A'; --j)
			{
				cout << (char) j;
			}
			// Include new line
			cout << "\n";
		}
	}
};
int main()
{
	BridgePattern *task = new BridgePattern();
	// Test cases
	task->printBridgePattern(7);
	task->printBridgePattern(10);
	return 0;
}

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Include namespace system
using System;
// Csharp program for
// Print reverse character bridge pattern
public class BridgePattern
{
	public void printBridgePattern(int n)
	{
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Display given n
		Console.Write("\nGiven n : " + n + " \n\n");
		// iterating the loop through by size n
		for (i = 0; i < n; ++i)
		{
			// Print left side pattern
			for (j = 0; j < n - i; ++j)
			{
				Console.Write((char)('A' + j));
			}
			for (j = 0; j < i + i; ++j)
			{
				// Include intermediate space
				Console.Write(" ");
			}
			// Print right side pattern
			for (j = 'A' + (n - i - 1); j >= 'A'; --j)
			{
				Console.Write((char) j);
			}
			// Include new line
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		BridgePattern task = new BridgePattern();
		// Test cases
		task.printBridgePattern(7);
		task.printBridgePattern(10);
	}
}

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
<?php
// Php program for
// Print reverse character bridge pattern
class BridgePattern
{
	public	function printBridgePattern($n)
	{
		if ($n <= 0 || $n > 26)
		{
			return;
		}
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// Display given n
		echo("\nGiven n : ".$n.
			" \n\n");
		// iterating the loop through by size n
		for ($i = 0; $i < $n; ++$i)
		{
			// Print left side pattern
			for ($j = 0; $j < $n - $i; ++$j)
			{
				echo(chr((ord('A') + $j)));
			}
			for ($j = 0; $j < $i + $i; ++$j)
			{
				// Include intermediate space
				echo(" ");
			}
			// Print right side pattern
			for ($j = ord('A') + ($n - $i - 1); $j >= ord('A'); --$j)
			{
				echo(chr($j));
			}
			// Include new line
			echo("\n");
		}
	}
}

function main()
{
	$task = new BridgePattern();
	// Test cases
	$task->printBridgePattern(7);
	$task->printBridgePattern(10);
}
main();

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Node JS program for
// Print reverse character bridge pattern
class BridgePattern
{
	printBridgePattern(n)
	{
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Display given n
		process.stdout.write("\nGiven n : " + n + " \n\n");
		// iterating the loop through by size n
		for (i = 0; i < n; ++i)
		{
			// Print left side pattern
			for (j = 0; j < n - i; ++j)
			{
				process.stdout.write(
                  String.fromCharCode(('A'.charCodeAt(0) + j)));
			}
			for (j = 0; j < i + i; ++j)
			{
				// Include intermediate space
				process.stdout.write(" ");
			}
			// Print right side pattern
			for (j = 'A'.charCodeAt(0) + (n - i - 1); 
                 j >= 'A'.charCodeAt(0); 
                 --j)
			{
				process.stdout.write(String.fromCharCode(j));
			}
			// Include new line
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new BridgePattern();
	// Test cases
	task.printBridgePattern(7);
	task.printBridgePattern(10);
}
main();

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
#  Python 3 program for
#  Print reverse character bridge pattern
class BridgePattern :
	def printBridgePattern(self, n) :
		if (n <= 0 or n > 26) :
			return
		
		#  Loop controlling variables
		i = 0
		j = 0
		#  Display given n
		print("\nGiven n : ", n ," \n")
		#  iterating the loop through by size n
		while (i < n) :
			#  Print left side pattern
			j = 0
			while (j < n - i) :
				print(chr((ord('A') + j)), end = "")
				j += 1
			
			j = 0
			while (j < i + i) :
				#  Include intermediate space
				print(" ", end = "")
				j += 1
			
			#  Print right side pattern
			j = ord('A') + (n - i - 1)
			while (j >= ord('A')) :
				print(chr(j), end = "")
				j -= 1
			
			#  Include new line
			print(end = "\n")
			i += 1
		
	

def main() :
	task = BridgePattern()
	#  Test cases
	task.printBridgePattern(7)
	task.printBridgePattern(10)

if __name__ == "__main__": main()

input

Given n :  7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n :  10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
#  Ruby program for
#  Print reverse character bridge pattern
class BridgePattern 
	def printBridgePattern(n) 
		if (n <= 0 || n > 26) 
			return
		end

		#  Loop controlling variables
		i = 0
		j = 0
		#  Display given n
		print("\nGiven n : ", n ," \n\n")
		#  iterating the loop through by size n
		while (i < n) 
			#  Print left side pattern
			j = 0
			while (j < n - i) 
				print((('A'.ord + j)).chr)
				j += 1
			end

			j = 0
			while (j < i + i) 
				#  Include intermediate space
				print(" ")
				j += 1
			end

			#  Print right side pattern
			j = 'A'.ord + (n - i - 1)
			while (j >= 'A'.ord) 
				print((j).chr)
				j -= 1
			end

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

	end

end

def main() 
	task = BridgePattern.new()
	#  Test cases
	task.printBridgePattern(7)
	task.printBridgePattern(10)
end

main()

input

Given n : 7 

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10 

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Scala program for
// Print reverse character bridge pattern
class BridgePattern()
{
	def printBridgePattern(n: Int): Unit = {
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Display given n
		print("\nGiven n : " + n + " \n\n");
		// iterating the loop through by size n
		while (i < n)
		{
			// Print left side pattern
			j = 0;
			while (j < n - i)
			{
				print(('A'.toInt + j).toChar);
				j += 1;
			}
			j = 0;
			while (j < i + i)
			{
				// Include intermediate space
				print(" ");
				j += 1;
			}
			// Print right side pattern
			j = 'A'.toInt + (n - i - 1);
			while (j >= 'A'.toInt)
			{
				print(j.toChar);
				j -= 1;
			}
			// Include new line
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BridgePattern = new BridgePattern();
		// Test cases
		task.printBridgePattern(7);
		task.printBridgePattern(10);
	}
}

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Swift 4 program for
// Print reverse character bridge pattern
class BridgePattern
{
	func printBridgePattern(_ n: Int)
	{
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Display given n
		print("\nGiven n : ", n ," \n");
		// iterating the loop through by size n
		while (i < n)
		{
			// Print left side pattern
			j = 0;
			while (j < n - i)
			{
				print(Character(UnicodeScalar(65 + j)!), terminator: "");
				j += 1;
			}
			j = 0;
			while (j < i + i)
			{
				// Include intermediate space
				print(" ", terminator: "");
				j += 1;
			}
			// Print right side pattern
			j = 65 + (n - i - 1);
			while (j >= 65)
			{
				print(Character(UnicodeScalar(j)!), terminator: "");
				j -= 1;
			}
			// Include new line
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let task = BridgePattern();
	// Test cases
	task.printBridgePattern(7);
	task.printBridgePattern(10);
}
main();

input

Given n :  7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n :  10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A
// Kotlin program for
// Print reverse character bridge pattern
class BridgePattern
{
	fun printBridgePattern(n: Int): Unit
	{
		if (n <= 0 || n > 26)
		{
			return;
		}
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Display given n
		print("\nGiven n : " + n + " \n\n");
		// iterating the loop through by size n
		while (i < n)
		{
			// Print left side pattern
			
			while (j < n - i)
			{
				print(('A'.toInt() + j).toChar());
				j += 1;
			}
			j = 0;
			while (j < i + i)
			{
				// Include intermediate space
				print(" ");
				j += 1;
			}
			// Print right side pattern
			j = 'A'.toInt() + (n - i - 1);
			while (j >= 'A'.toInt())
			{
				print(j.toChar());
				j -= 1;
			}
			// Include new line
			print("\n");
			i += 1;
          	j = 0;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: BridgePattern = BridgePattern();
	// Test cases
	task.printBridgePattern(7);
	task.printBridgePattern(10);
}

input

Given n : 7

ABCDEFGGFEDCBA
ABCDEF  FEDCBA
ABCDE    EDCBA
ABCD      DCBA
ABC        CBA
AB          BA
A            A

Given n : 10

ABCDEFGHIJJIHGFEDCBA
ABCDEFGHI  IHGFEDCBA
ABCDEFGH    HGFEDCBA
ABCDEFG      GFEDCBA
ABCDEF        FEDCBA
ABCDE          EDCBA
ABCD            DCBA
ABC              CBA
AB                BA
A                  A

The output consists of the reverse character bridge pattern as described earlier. Each row is printed, and the intermediate spaces are included to maintain symmetry.

Time Complexity

The time complexity of the code is O(n^2), where 'n' is the given input. The nested loops iterate through 'n' rows and 'n' columns, resulting in a quadratic time complexity.

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.

New Comment