## Idea

The idea is to take an auxiliary space multiple of the size of both the matrices to store the resultant product. Afterward, loop over the elements of matrix A and B, i.e., each element of matrix A is multiplied by the whole matrix B and stored in matrix C.

Now, let us discuss the Implementation of the Kronecker product of two matrices in Java and C++:

## Implementation in Javađź¤ą

```
/*
Java code to find the Kronecker Product of
two matrices A and B and stores it as matrix C
*/
import java.io.*;
import java.util.*;
public class KroneckerProduct {
/*
Utility function to compute Kronecker Product of two matrices
*/
static void Kroneckerproduct(int A[][], int B[][], int row1, int row2, int col1, int col2) {
int[][] C = new int[row1 * row2][col1 * col2];
// i loops till row1
for (int i = 0; i < row1; i++) {
// k loops till row2
for (int k = 0; k < row2; k++) {
// j loops till col1
for (int j = 0; j < col1; j++) {
// l loops till col2
for (int l = 0; l < col2; l++) {
/* Each element of matrix A is
multiplied by whole Matrix B
stored in Matrix C
*/
C[i + l + 1][j + k + 1] = A[i][j] * B[k][l];
System.out.print(C[i + l + 1][j + k + 1] + " ");
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the row size of the first matrix: ");
int rowsize1 = sc.nextInt();
System.out.println("Enter the col size of the first matrix: ");
int colsize1 = sc.nextInt();
System.out.println("Enter the row size of the second matrix: ");
int rowsize2 = sc.nextInt();
System.out.println("Enter the col size of the second matrix: ");
int colsize2 = sc.nextInt();
int[][] A = new int[rowsize1][colsize1];
int[][] B = new int[rowsize2][colsize2];
System.out.println("Enter the elements of first matrix ");
for (int i = 0; i < rowsize1; i++) {
for (int j = 0; j < colsize1; j++) {
A[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of second matrix ");
for (int i = 0; i < rowsize2; i++) {
for (int j = 0; j < colsize2; j++) {
B[i][j] = sc.nextInt();
}
}
System.out.println("Kronecker Product of matrix A and B is: ");
Kroneckerproduct(A, B, rowsize1, rowsize2, colsize1, colsize2);
}
}
```

**Output**

## Implementation in C++đź¤ą

```
/*
C++ code to find the Kronecker Product of two
matrices A and B
*/
#include <bits/stdc++.h>
using namespace std;
const int colsize1 = 2,
rowsize1 = 2,
colsize2 = 2,
rowsize2 = 2;
/*
Utility function to compute Kronecker Product of two matrices
*/
void Kroneckerproduct(int A[][colsize1], int B[][colsize2])
{
int C[rowsize1 *rowsize2][colsize1 *colsize2];
// i loops till rowsize1
for (int i = 0; i < rowsize1; i++)
{
// j loops till rowsize2
for (int j = 0; j < rowsize2; j++)
{
// k loops till colsize1
for (int k = 0; k < colsize1; k++)
{
// l loops till colsize2
for (int l = 0; l < colsize2; l++)
{
/*Each element of matrix A is
multiplied by whole Matrix B
stored in Matrix C
*/
C[i + l + 1][k + j + 1] = A[i][k] *B[j][l];
cout << C[i + l + 1][k + j + 1] << " ";
}
}
cout << endl;
}
}
}
int main()
{
int A[2][2] = {
{ 1, 2 },
{ 3, 4 }
},
B[2][2] = {
{ 0, 5 },
{ 6, 7 }
};
cout << "Kronecker Product of the matrix A and B: " << endl;
Kroneckerproduct(A, B);
return 0;
}
```

**Output**

## Complexity AnalysisđźĄ·

**Time Complexity**: **O(rowsize1 * rowsize2 * colsize1 * colsize2)**, where rowsize1, rowsize2, colsize1 and colsize2 are the dimensions of the matrix A and B.

**Auxiliary Space**: **O((rowsize1 * rowsize2) * (colsize1 * colsize2))**, where rowsize1, rowsize2, colsize1 and colsize2 are the dimensions of the matrix A and B.

## Frequently Asked Questions

### What is the application of Kronecker Product?

Kronecker Product is often used to solve difficult problems in linear algebra and it is also used in Image Processing.

### What is the other name of Kronecker Product?

The Kronecker product is also referred to as the matrix direct product.

### What makes the Kronecker Product different from a normal matrix product?

The Kronecker product, unlike the ordinary product of two matrices, is defined regardless of the dimensions of the two matrices A and B.

### What is the purpose of the Kronecker product?

The Kronecker product is an operation that combines two matrices to create a larger matrix that contains all of the possible products of the two matrices' entries.

### What is the need of taking the auxiliary space?

Auxiliary space is temporary or extra space. This temporary space is created to solve the problem. The total space taken by the algorithm in relation to the input size is referred to as space complexity.

**Conclusion**

In this article, we have extensively discussed the Kronecker Product of two matrices.

We hope that this blog has helped you enhance your knowledge regarding the matrices in programming and if you would like to learn more, check out our articles on

Refer to our __guided paths on Coding Ninjas Studio__ to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.

Enroll in our __courses__ and refer to the __mock test__ and __problems__ available.

Take a look at the __interview experiences__ and __interview bundle__ for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Coding!