Find determinant of matrix generated by array rotation
Last Updated :
08 Jul, 2022
Given an array of three elements. The task is to construct a matrix of order 3×3 by using all three rotations of the array as a row of the matrix and find the determinant of the resultant matrix.
Examples:
Input : arr[] = {1, 2, 3}
Output : 18
Input : arr[] = {1, 1, 1}
Output : 0
Approach: As per the problem statement, construct a 3*3 matrix using the given array. If a1, a2, a3 are array elements then the corresponding matrix will be:
{{a1, a2, a3},
{a3, a1, a2},
{a2, a3, a1}}
The task is to calculate the determinant of the above matrix.
The determinant can be calculated by using the proper method for this but on the other hand, if the resultant matrix is expanded for calculation, the result will be a13 + a23 + a33 – 3*a1*a2*a3. Hence, instead of calculating determinants by proper expansion use the above-generated formula.
Therefore, the Determinant of the above Matrix will be:
a13 + a23 + a33 - (3*a1*a2*a3)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
int calcDeterminant( int arr[])
{
int determinant = 0;
for ( int i = 0; i < N; i++) {
determinant += pow (arr[i], 3);
}
determinant -= 3 * arr[0] * arr[1] * arr[2];
return determinant;
}
int main()
{
int arr[] = { 4, 5, 3 };
cout << calcDeterminant(arr);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int N = 3 ;
static double calcDeterminant( int arr[])
{
double determinant = 0 ;
for ( int i = 0 ; i < N; i++)
{
determinant += Math.pow(arr[i], 3 );
}
determinant -= 3 * arr[ 0 ] *
arr[ 1 ] * arr[ 2 ];
return determinant;
}
static public void main (String args[])
{
int []arr = { 4 , 5 , 3 };
System.out.println(calcDeterminant(arr));
}
}
|
Python3
def calcDeterminant(arr,n):
determinant = 0
for i in range (n):
determinant + = pow (arr[i], 3 )
determinant - = 3 * arr[ 0 ] * arr[ 1 ] * arr[ 2 ]
return determinant
arr = [ 4 , 5 , 3 ]
n = len (arr)
print (calcDeterminant(arr,n))
|
C#
using System;
class GFG
{
static int N = 3;
static double calcDeterminant( int []arr)
{
double determinant = 0;
for ( int i = 0; i < N; i++)
{
determinant += Math.Pow(arr[i], 3);
}
determinant -= 3 * arr[0] *
arr[1] * arr[2];
return determinant;
}
static public void Main ()
{
int []arr = { 4, 5, 3 };
Console.WriteLine(calcDeterminant(arr));
}
}
|
PHP
<?php
$N = 3;
function calcDeterminant( $arr )
{
global $N ;
$determinant = 0;
for ( $i = 0; $i < $N ; $i ++)
{
$determinant += pow( $arr [ $i ], 3);
}
$determinant -= 3 * $arr [0] *
$arr [1] * $arr [2];
return $determinant ;
}
$arr = array ( 4, 5, 3 );
echo calcDeterminant( $arr );
?>
|
Javascript
<script>
let N = 3;
function calcDeterminant(arr)
{
let determinant = 0;
for (let i = 0; i < N; i++)
{
determinant += Math.pow(arr[i], 3);
}
determinant -= 3 * arr[0] *
arr[1] * arr[2];
return determinant;
}
let arr =[ 4, 5, 3 ];
document.write(calcDeterminant(arr));
</script>
|
Time Complexity : O(N) as only one traversal is requires on array.
Auxiliary Space : O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...