Subtraction of Two Matrices

Difficulty Level Easy
Frequently asked in Capgemini MAQ o9 solutions
Array Matrix School ProgrammingViews 2624

Problem Statement

In the “Subtraction of Two Matrices” problem, we have given two matrices a and b. We have to find the final matrix after subtracting matrix b from matrix a. If the order is the same for both the matrices then only we can subtract them otherwise we can’t.

Input Format

The first line containing four integer values r1, c1, r2, c2. Where r1 and c1 denote the number of rows and columns of the first matrix. And r2, c2 denotes the number of rows, columns of the second matrix.

Next r1 lines containing c1 integer values.

And next r2 lines containing c2 integer values.

Output Format

Print the final matrix after subtraction in such a way that every row starts from the new line and every element separated by space in each row.

Constraints

  • 1<=r1, c1, r2, c2<=5000.
  • 1<= |m[i][j]| <=10^9 where m is the matrix and the position of element at ith row and jth column.

Example

2 2 2 2
2 8
0 9
5 6
11 3
-3 2
-11 6

Explanation: We can find the subtraction of matrices by subtracting the corresponding entries in matrix C and D.

Subtraction of Two Matrices

Subtraction of Two Matrices

Subtraction of Two Matrices

Subtraction of Two Matrices

Algorithm for Subtraction of Two Matrices

1. For each row in the two matrices

1.1  Subtract the respective elements in matrix B with the elements in matrix A.

1.2 Store the result in the same position in some auxiliary matrix.

2.  Move to the next row and follow steps 1.1 and 1.2 till the end of the matrices.

3. Print the auxiliary matrix.

Implementation

C++ Program for Subtraction of Two Matrices

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int r1,c1,r2,c2;
    cin>>r1>>c1>>r2>>c2;
    int a[r1][c1];
    int b[r2][c2];
    for(int i=0;i<r1;i++)
    {
        for(int j=0;j<c1;j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=0;i<r2;i++)
    {
        for(int j=0;j<c2;j++)
        {
            cin>>b[i][j];
        }
    }
    if(c1!=c2 || r1!=r2)
    {
        cout<<"We can’t subtract these matrices.";
    }
    else
    {
    int c[r1][c2];
    for(int i=0;i<r1;i++)
    {
        for(int j=0;j<c2;j++)
        {
            c[i][j]=a[i][j]-b[i][j];
            cout<<c[i][j]<<"  ";
        }
        cout<<endl;
    }
    }
    return 0;
}

Java Program for Subtraction of Two Matrices

import java.io.*; 
import java.util.Scanner;
class TutorialCup
{ 
    // Driver code 
    public static void main(String[] args) 
    { 
        int r1,c1,r2,c2;
        Scanner inp = new Scanner(System.in);
        r1 = inp.nextInt();
        c1 = inp.nextInt();
        r2 = inp.nextInt();
        c2 = inp.nextInt();
        int a[][] = new int[r1][c1];
        for(int i=0;i<r1;i++)
        {
            for(int j=0;j<c1;j++)
            {
                a[i][j]=inp.nextInt();
            }
        }
        int b[][] = new int[r2][c2];
        for(int i=0;i<r2;i++)
        {
            for(int j=0;j<c2;j++)
            {
                b[i][j]=inp.nextInt();
            }
        }
        if(r1!=r2 && c1!=c2) 
        { 
            System.out.println("\nWe can’t subtract these matrices."); 
        } 
        else
        {
            int c[][] = new int[r1][c1];
            for(int i=0;i<r1;i++)
            {
                for(int j=0;j<c1;j++)
                {
                    c[i][j]=a[i][j]-b[i][j];
                    System.out.print(c[i][j] + " ");
                }
                System.out.println();
            }
        }
    } 
}
2 2 2 2
2 8 
0 9 
5 6 
11 3
-3  2  
-11  6

Complexity Analysis for Subtraction of Two Matrices

Time Complexity

O(n^2) where n is the maximum of r1 and c1. Here we simply run two loops first loop run r1 times and the second loop runs c1 times.

Space Complexity

O(m^2) where m is the maximum of r1 and c1. Here we create extra space for storing the result of the subtraction of matrices. Here we also declared r1*c1 size for taking input the first matrix and r2*c2 size for taking input the second matrix.

Reference

Translate »