1

Can anybody please explain the calculation of time and space complexity of this code(finding determinant of a matrix) in detail? I am not able to understand the exact time complexity of this program using recurrence tree.

class Solution
{
    static void getCofactor(int arr[][], int temp[][], int n, int i, int j){
        int x = 0, y = 0;
        
        for(int row=0; row<n; row++){
            for(int col=0; col<n; col++){
                if(row!=i && col!=j){
                    temp[x][y++] = arr[row][col];
                    if(y == n-1){
                        y = 0;
                        x++;
                    }
                }
            }
        }
    }
    
    static int determinantOfMatrix(int matrix[][], int n)
    {
        // code here 
        if(n == 1)
            return matrix[0][0];
        
        int temp[][] = new int[n-1][n-1];
        int determinant = 0;
        int sign = 1;
        
        for(int i=0; i<n; i++){
            getCofactor(matrix, temp, n, 0, i);
            determinant+=sign*matrix[0][i]*determinantOfMatrix(temp, n-1);
            sign=-sign;
        }
        return determinant;
    }
}
Ankur Bose
  • 29
  • 5

0 Answers0