-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeterminant of a Matrix.java
More file actions
38 lines (34 loc) · 956 Bytes
/
Determinant of a Matrix.java
File metadata and controls
38 lines (34 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution
{
//Function for finding determinant of matrix.
static int determinantOfMatrix(int mat[][], int n)
{
// code here
if(n==1) return mat[0][0];
if(n==2) return mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0];
int det = 0;
for(int i=0; i<n; i++){
int cof = (int)Math.pow(-1,i)*mat[0][i]*determinantOfMatrix(sub(mat,0,i),n-1);
det = det + cof;
}
return det;
}
static int[][] sub(int[][] mat, int r, int c){
int n = mat.length;
int arr[][] = new int[n-1][n-1];
int row=0, col =0;
for(int i=0; i<n; i++){
if(i!=r){
for(int j=0; j<n; j++){
if(j!=c){
arr[row][col] = mat[i][j];
col++;
}
}
col = 0;
row++;
}
}
return arr;
}
}