# Find determinant of matrix generated by array rotation

Given an array of three element. The task is to construct a matrix of order 3*3 by using all three rotation of array as row of matrix and find the determinant of resultant matrix.

Examples:

```Input : arr[] = {1, 2, 3}
Output : 18

Input : arr[] = {1, 1, 1}
Output : 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: As per 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.

Determinant can be calculated by using proper method for this but on the other hand if resultant matrix is expanded for calculation, the result will be a13 + a23 + a33 – 3*a1*a2*a3. Hence, instead of calculating determinant by proper expansion use the above generated formula.

Therefore, Determinant of the above Matrix will be:

```a13 + a23 + a33 - (3*a1*a2*a3)
```

Below is the implementation of the above approach:

## C++

 `// C++ program for finding determinant of generated matrix ` ` `  `#include ` `#define N 3 ` `using` `namespace` `std; ` ` `  `// Function to calculate determinant ` `int` `calcDeterminant(``int` `arr[]) ` `{ ` `    ``int` `determinant = 0; ` ` `  `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``determinant += ``pow``(arr[i], 3); ` `    ``} ` ` `  `    ``determinant -= 3 * arr * arr * arr; ` ` `  `    ``return` `determinant; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 5, 3 }; ` `    ``cout << calcDeterminant(arr); ` `    ``return` `0; ` `} `

## Java

 `// Java program for finding determinant ` `// of generated matrix  ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `class` `GFG ` `{ ` `static` `int` `N = ``3``; ` ` `  `// Function to calculate determinant  ` `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;  ` `}  ` ` `  `// Driver code ` `static` `public` `void` `main (String args[]) ` `{ ` `    ``int` `[]arr = { ``4``, ``5``, ``3` `};  ` `    ``System.out.println(calcDeterminant(arr));  ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## Python3

 `# Python3 program for finding determinant of generated matrix ` ` `  `# Function to calculate determinant ` `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 ` ` `  `# Driver code ` `arr ``=` `[``4``,``5``,``3``] ` `n ``=` `len``(arr) ` `print``(calcDeterminant(arr,n)) ` ` `  `# This code is contributed by Shrikant13 `

## C#

 `// C# program for finding determinant ` `// of generated matrix  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `static` `int` `N = 3; ` ` `  `// Function to calculate determinant  ` `static` `double` `calcDeterminant(``int` `[]arr)  ` `{  ` `    ``double` `determinant = 0;  ` ` `  `    ``for` `(``int` `i = 0; i < N; i++)  ` `    ``{  ` `        ``determinant += Math.Pow(arr[i], 3);  ` `    ``}  ` ` `  `    ``determinant -= 3 * arr *  ` `                       ``arr * arr;  ` ` `  `    ``return` `determinant;  ` `}  ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `[]arr = { 4, 5, 3 };  ` `    ``Console.WriteLine(calcDeterminant(arr));  ` `} ` `} ` ` `  `// This code is contributed by akt_mit `

## PHP

 ` `

Output:

```36
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.