Intermediate Level Pattern Problems | Part – 3

Intermediate Level Pattern Problems | Part - 3

Introduction

In this article, we are going to dive a little deeper into discussing, How to Master in Pattern Problems. If you are already familiar with the fundamental pattern problems, then proceed further. If not, then please check out the article on Basic Pattern Problems | Part – 2 first.

Let’s get started with the most asked questions in interviews.

Intermediate Level Pattern Problems

1. Pascal’s Triangle

Image Source: Google

Before moving to the coding section, let us understand some Binomial Expression to understand Pascal’s Triangle. You must have studied Pascal’s triangle in your School; now it is time to recall those memories to master in Pattern Problems.

  • ( a + b )^0 = (1)^1
  • ( a + b)^1 = (1)a +(1)b
  • ( a + b)^2 = (1)a^2 + (2)ab + (1)b^2
  • ( a + b )^3 = (1)a^3 + (3)a^2 b + (3)ab^2 + (1)b^3  upto (a + b)^n

Have you noticed one thing? The values of the coefficients are the same as the upper values of Pascal’s Table, which precisely means that Pascal’s triangle is a triangular array of the binomial coefficients.

C

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

C++

#include<bits/stdc++.h>
using namespace std;
int main(){
    int rows;
    cin>>rows;
    for(int i=1;i<=rows;i++){
        int coef=1;
        for(int k=rows-i;k>0;k--){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<coef<<" ";
            coef = coef * (i-j)/j;
        }
        cout<<endl;
    }
}

Java

import java.util.Scanner;
class PascalTriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();
        for(int i=1;i<=rows;i++){
        int coef=1;
        for(int k=rows-i;k>0;k--){
            System.out.print(" ");
        }
        for(int j=1;j<=i;j++){
            System.out.print(coef + " ");
            coef = coef * (i-j)/j;
        }
        System.out.println();
    }
    }
}

Python

n = int(input())
 
# iterate upto n
for i in range(n):
    # adjust space
    print(' '*(n-i), end='')
 
    # compute power of 11
    print(' '.join(map(str, str(11**i))))

2. Floyd’s Triangle

Floyd’s triangle is a triangular array of consecutive natural numbers. For printing the natural numbers, we can initialise the variable num = 1, print it afterwards, and increment it by 1.

The Pattern is making the next move when it becomes > row+1. Before jumping to the solution, try to solve the problem by yourself. Spoiler alert: It is the easiest one among the Intermediate Level pattern problems.

C

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

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
   int rows, i, j, num = 1;
   cout<<"Enter the number of rows:";
   cin>>rows;
   for (i = 0; i < rows; i++) {
      for (j = 0; j <= i; ++j) {
         cout<<num++<<" ";
      }
      cout<<endl;
   }
   return 0;
}

Java

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

Python

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

3. Inverted Full pyramid using stars

  • Intrinsically, the spaces begin with the one less row index.
  • Try to solve by yourself before moving to the solution.

C

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

C++

#include <iostream>
using namespace std;
int main() {
   int rows, i, j, space;
   cout<<"Enter the number of rows: ";
   cin>>rows;
   for (i = rows; i >= 1; --i) {
      for (space = 0; space < rows - i; ++space)
         cout<<"  ";
      for (j = i; j <= 2 * i - 1; ++j)
         cout<<"* ";
      for (j = 0; j < i - 1; ++j)
         cout<<"* ";
      cout<<endl;
   }
   return 0;
}

Java

import java.util.Scanner;
class InvertedFullPyramid {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();
        int i,j,space;
        for (i = rows; i >= 1; --i) {
            for (space = 0; space < rows - i; ++space)
                System.out.print("  ");
            for (j = i; j <= 2 * i - 1; ++j)
                 System.out.print("* ");
            for (j = 0; j < i - 1; ++j)
                System.out.print("* ");
        System.out.println();
   }
    }
}

Python

rows = int(input())
for i in range(rows,0,-1):
    for k in range(rows-1,i-1,-1):
        print(end=" ")
    for j in range(i):
        print("*", end=" ")
    print()    

4. Print Diamond of stars

The Diamond Pattern Problem is the combination of Full Pyramid and Inverted Full Pyramid. 

If we observe, either the Upper half will have n rows or the lower half will have n number of rows. Let us say we have n = 5; the loop for the lower half starts from the n-1, as we are following the decreasing order.

C

#include<stdio.h>
int main()
{
    int i, j, row, space;
    printf("Enter the Number of Rows: ");
    scanf("%d", &row);
    space = row-1;
    for(i=1; i<=row; i++)
    {
        for(j=1; j<=space; j++)
            printf(" ");
        space--;
        for(j=1; j<=(2*i-1); j++)
            printf("*");
        printf("\n");
    }
    space = 1;
    for(i=1; i<=(row-1); i++)
    {
        for(j=1; j<=space; j++)
            printf(" ");
        space++;
    for(j=1; j<=(2*(row-i)-1); j++)
            printf("*");
        printf("\n");
    }
    printf("\n");
    return 0;
}

C++

#include<iostream>
using namespace std;
int main()
{
    int i, j, row, space;
    cout<<"Enter the Number of Rows: ";
    cin>>row;
    space = row-1;
    for(i=1; i<=row; i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space--;
        for(j=1; j<=(2*i-1); j++)
            cout<<"*";
        cout<<endl;
    }
    space = 1;
    for(i=1; i<=(row-1); i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space++;
        for(j=1; j<=(2*(row-i)-1); j++)
            cout<<"*";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

Java

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

Python

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

Key Takeaways

To summarise, we’ve discussed the most important pattern problems which are being asked several times in Interviews. I hope you are now getting familiar with the Patterns.

Do post your doubt in the comment section. If you like this article, do share it with your friends. Don’t stop here Ninja, do check out our last article in this series on Advance Level Pattern Problems | Part – 4

By Alisha Chhabra

Exit mobile version