What Are Basic Pattern Problems? | Part – 2

What Are Basic Pattern Problems? | Part - 2
What Are Basic Pattern Problems? | Part - 2

Introduction

Pattern Problems are the most asked question during the Interviews. Understanding the fundamentals of Patterns, one can solve any random Pattern Problem. In this lesson, we’ll be understanding the Basic Pattern Problems. Before moving further, we have to recognise Patterns.

What are the Patterns? How to approach the pattern problems? If you are already aware of these concepts, then proceed, otherwise, please check our article on How to Master in Pattern Problems | Part – 1.

Basic Patterns

  1. Hollow Rectangle
  • In the above pictorial representation, rows = cols = 5, and the starting index is 0.
  • Other patterns that can be made with the help above explanation are, E.g.:- H, V, I, X, N, F, E, Z, T, etc.
  • Give a try to print the Hollow Rectangle pattern before jumping to the implementation.

C

#include <stdio.h>
 
// Function to print hollow rectangle
void print_rectangle(int rows, int cols)
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            if (i==0 || i==rows-1 || j==0 || j==cols-1)           
                printf("*");           
            else
                printf(" ");           
        }
        printf("\n");
    }
 
}
 
// Driver program for above function
int main()
{
    int rows = 9, columns = 25;
    print_rectangle(rows, columns);
    return 0;
} 

C++

#include<bits/stdc++.h>
using namespace std;
void print_Hollow_Rectangle(int rows ,  int cols){
    int i,j;
    for(i=0;i<rows;i++){
        for(j=0;j<cols;j++){
            if(i==0 || j==0 || i==rows-1 || j==cols-1){
                cout<<"*"<<" ";
            }
            else{
                cout<<" "<<" ";
            }
        }
        cout<<"\n";
    }
}
int main(){
    int no_of_rows;
    cin>>no_of_rows;
    int no_of_cols;
    cin>>no_of_cols;
    print_Hollow_Rectangle(no_of_rows,no_of_cols);
    return 0;
}

Java

import java.util.Scanner;
public class HollowRectangle {
    public static void print_Hollow_Rect(int rows, int cols){
        int i,j;
        for(i=0;i<rows;i++){
            for(j=0;j<cols;j++){
                if(i==0 || j==0 || i==rows-1 || j==cols-1){
                    System.out.print("*"+" ");
                }
                else{
                    System.out.print("  ");
                }
            }
            System.out.print("\n");
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows : ");
        int rows = sc.nextInt();
        System.out.println("Enter the number of columns : ");
        int cols = sc.nextInt();
        sc.close();
        HollowRectangle obj = new HollowRectangle();
        obj.print_Hollow_Rect(rows,cols);
    }
}

Python

def print_rectangle(rows, cols) :
     
    for i in range(0, rows) :
        for j in range(0, cols) :
            if (i == 0 or i == rows-1 or
                j == 0 or j == cols-1) :
                print("*", end="")           
            else :
                print(" ", end="")           
         
        print()
 
rows = 6
columns = 20
print_rectangle(rows, columns)

2. Hollow Inverted Half Pyramid

  • In the above representation, [i] represents the row index, whereas [j] represents the column index. This problem is quite similar to the previous one, just with a minor change. Here, the critical point is when [i==j] prints Ninja, which is nothing but the diagonal of the matrix.
  • Ninja, Give it a try first, then proceed with the Implementation.

C

#include <stdio.h>
void print_hollow_halfPyramid(int rows, int cols)
{
    int i,j;
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            if(j==cols-1 || i==j || i==0){
                printf("Ninja");
            }
            else{
                printf("     ");
            }
        }
        printf("\n");
    }
 
}
 
// Driver program for above function
int main()
{
    int rows = 10, columns = 10;
    print_hollow_halfPyramid(rows, columns);
    return 0;
} 

C++


#include<bits/stdc++.h>
using namespace std;
void print_hollow_halfPyramid(int rows, int cols)
{
    int i,j;
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            if(j==cols-1 || i==j || i==0){
                cout<<"Ninja"<<" ";
            }
            else{
                cout<<"     "<<" ";
            }
        }
        cout<<"\n";
    }
 
}
 
// Driver program for above function
int main()
{
    int rows = 7, columns = 7;
    print_hollow_halfPyramid(rows, columns);
    return 0;
} 

Java

import java.util.Scanner;
public class HollowPyramid {
    public static void print_Hollow_Half_Pyramid(int rows, int cols){
        int i,j;
        for(i=0;i<rows;i++){
            for(j=0;j<cols;j++){
                if(i==0 || i==j || j==cols-1){
                    System.out.print("Ninja"+" ");
                }
                else{
                    System.out.print("      ");
                }
            }
            System.out.print("\n");
        }
    }
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter the number of rows : ");
         int rows = sc.nextInt();
         System.out.println("Enter the number of columns : ");
         int cols = sc.nextInt();
         sc.close();
        HollowPyramid obj = new HollowPyramid();
        obj.print_Hollow_Half_Pyramid(rows,cols);
    }
}

Python


def print_Hollow_Pyramid(rows, cols) :
     
    for i in range(0, rows) :
        for j in range(0, cols) :
            if (i == 0 or i == j or
                 j == cols-1) :
                print("Ninja", end="")           
            else :
                print("     ", end="")           
         
        print()
 
 
rows = int(input("Enter the number of rows "))
columns = int(input("Enter the number of coloumns "))
print_Hollow_Pyramid(rows, columns)

3. Inverted Half Triangle

  • The critical point to remember over here is to print the spaces before the alphabet. To understand every aspect of any pattern problem, try to remove the specific condition, then observe the outcome.

C

#include<stdio.h>
void print_hollow_halfPyramid(int rows, int cols)
{
    int i,j;
    for(i=rows;i>0;i--){
        j=0;
        while(j<cols){
            if(j<i-1){
            printf("   ");
            j++;
            }
            else{
            printf("A");
            printf(" ");
            printf("B");
            j++;
            }
        }
        printf("\n");
    }
}
 
// Driver program for above function
int main()
{
    int rows = 7, columns = 7;
    print_hollow_halfPyramid(rows, columns);
    return 0;
} 

C++

#include<bits/stdc++.h>
using namespace std;
void print_hollow_halfPyramid(int rows, int cols)
{
    int i,j;
        for(i=rows;i>0;i--){
        j=0;
        while(j<cols){
            if(j<i-1){
            cout<<" "<<" "<<" ";
            j++;
            }
            else{
            cout<<"A"<<" "<<"B";
            j++;
            }
        }
        cout<<"\n";
    }
}
 
int main()
{
    int rows = 7, columns = 7;
    print_hollow_halfPyramid(rows, columns);
    return 0;
} 

Java

public class InvertedHalfPyramid {
	
    public static void main(String[] args) {
	
		int rows=7;
		int cols=7;
		int i,j;
		for(i=rows; i>0; i--) {
			for(j=0; j<cols; j++) {
				if(j<i-1) {
					System.out.print("   ");
				}
				else {
					System.out.print("A"+" "+"B");
				}
			}
			System.out.print("\n");
			
		}
      
	}

}

Python

rows = int(input("Enter the number of rows : "))
for i in range(rows):
    for j in range((rows-i-1)*3):
      print(" ",end=" ")
    print("A",end=" ")
    for j in range(i):
        print("BA", end=" ")
    print("B")    

4. Inverted Half Pyramid using numbers

  • In this pattern, the arrangement of the numbers is in descending order from top to down. Moreover, if we observe, the column index value is being printed concerning the row index. 
  • Here, the initialisation of the row index starts from the n, which is 5 in this case. To clarify, the numbers that are making the pattern precisely equal the index value of [row].
  • Get it? Now, it’s your turn to write the code for the pattern.

C

#include <stdio.h>
int main()
{
    int rows;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    printf("\n");
    for(int i = rows; i >= 1; --i)
    {
        for(int j = 1; j <= i; ++j)
        {
            printf("%d",j);
            printf(" ");
        }
        printf("\n");
    }
    return 0;
}

C++

#include <iostream>
using namespace std;

int main()
{
    int rows;

    cout << "Enter number of rows: ";
    cin >> rows;

    for(int i = rows; i >= 1; --i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << j << " ";
        }
        cout << endl;
    }

    return 0;
}

Java

import java.util.Scanner;
public class InvertedHalfPyramid {	
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the number of rows:");
		int rows=sc.nextInt();
		int i,j;
		for(i = rows; i >= 1; --i)
	    {
	        for(j = 1; j <= i; ++j)
	        {
	            System.out.print(j+" ");
	        }
	        System.out.print("\n");
	    }
			
		}
      
}

Python

rows = int(input("Enter the number of rows : "))
for i in range(rows+1,1,-1):
    for j in range(1,i):
        print(j, end=' ')
    print( )   

Key Takeaways

To sum up, Pattern Problems are nothing but patterns consisting of alphabets, numbers, or symbols in a particular manner. We’ve discussed the most Basic Pattern Problems, and several pattern problems are eagerly waiting for you to solve. These kinds of pattern programs can be solved easily by the loop’s condition.

I hope you find this article helpful, do check our next article on Intermediate Level Pattern Problems | Part – 3; if you have any doubt, post them in the comment section.

By Alisha Chhabra