# Java Program to subtract the two matrices

In this tutorial, we will learn to subtract two matrices in Java.

Two matrices let’s say A and B can be subtracted if and only if their dimensions are identical i.e., the same number of rows and columns.

Matrix A and Matrix B cannot be subtracted, if the dimension of one is 2 x 4 and of the other is 3 x 4.

Subtraction of two matrices is performed by subtracting their corresponding elements in the following manner:

Example 1:

Considering two matrices of dimension 2 x 2 Example 2:

Considering two matrices of dimension 3 x 3 ### Algorithm:

Step 1. Import the java.util.Scanner class.

Step 2.  Create a new class, here named MatrixSubtraction.

Step 3. In the main method, create a new Scanner object to read user input.

Step 4. Prompt the user to enter the number of rows and columns of the matrices.

Step 5.  Use the nextInt method of the Scanner object to read the number of rows and columns and store them in variables rows and columns.

Step 6. Create two 2-dimensional arrays firstMatrix and secondMatrix with rows and columns.

Step 7.  Prompt the user to enter the elements of the first matrix and use a nested loop to read and store the elements in the firstMatrix array.

Step 8.  Prompt the user to enter the elements of the second matrix and use a nested loop to read and store the elements in the secondMatrix array.

Step 9.  Create a 2-dimensional array resultantMatrix with rows and columns.

Step 10. Use a nested loop to perform element-wise subtraction of the two matrices and store the result in the resultantMatrix array.

Step 11. Use another nested loop to print the elements of the resultantMatrix array.

Step 12. End the program.

Implementation 1:

File name: MatrixSubtraction.java

``````import java.util.Scanner;

public class MatrixSubtraction {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of the matrices:");
int rows = scanner.nextInt();
int columns = scanner.nextInt();

int[][] firstMatrix = new int[rows][columns];
int[][] secondMatrix = new int[rows][columns];
int[][] resultantMatrix = new int[rows][columns];

System.out.println("Enter the elements of the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
firstMatrix[i][j] = scanner.nextInt();
}
}

System.out.println("Enter the elements of the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
secondMatrix[i][j] = scanner.nextInt();
}
}

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
resultantMatrix[i][j] = firstMatrix[i][j] - secondMatrix[i][j];
}
}

System.out.println("The result of subtraction of the two matrices is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(resultantMatrix[i][j] + " ");
}
System.out.println();
}
}
}
``````

Output 1: Output 2: Complexity Analysis:

Time complexity: The time complexity of the above program to subtract two matrices is O(mn), where m is the number of rows and n is the number of columns of the matrices.

The main operations performed in the program are reading the input, subtracting the matrices element-wise, and printing the result. Each of these operations requires a loop over the elements of the matrices, which takes O(mn) time in total.

Thus, the overall time complexity of the program is O(mn). This makes the program efficient for small to moderate-sized matrices, but for very large matrices it might become slow.

Space complexity: The space complexity of the program is O(mn), as it requires storing the elements of the three matrices in memory.

Implementation 2:

File name: MatrixSubtraction.java

``````public class MatrixSubtraction
{
public static void main(String[] args) {
int rows, cols;

//Initialize matrix a
int p[][] = {
{8, 9, 5},
{6, 3, 3},
{10, 5, 2}
};

//Initialize matrix b
int q[][] = {
{6, 2, 12},
{3, 0, 7},
{9, 0, 9}
};

//Calculates number of rows and columns present in the given matrix
rows = p.length;
cols = p.length;

//Array diff will hold the result
int resultantM[][] = new int[rows][cols];

//Performs subtraction of matrices p and q. Store the result in matrix resultantM
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
resultantM[i][j] = p[i][j] - q[i][j];
}
}

System.out.println("Resulting matrix after subtracting one from other: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.print(resultantM[i][j] + " ");
}
System.out.println();
}
}
}
``````

Output: Explanation: The above program is a modified version of the previous one. Here, instead of taking the value of matrices as input from the user, the value of matrices is already provided.

The time and space complexities are same as that of previous one.