# Maximum determinant of a matrix with every values either 0 or n

We have given a positive number n, and we have to find a 3*3 matrix which can be formed with combination of 0 or n and has maximum determinant.

Examples :

```Input : n = 3
Output : Maximum determinant = 54
Resultant Matrix :
3 3 0
0 3 3
3 0 3

Input : n = 13
Output : Maximum determinant = 4394
Resultant Matrix :
13 13  0
0  13 13
13  0 13
```

Explanation:
For any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2*(n^3).. Also a matrix having maximum determinant is of form:
n n 0
0 n n
n 0 0

## C++

 `// C++ program to find  maximum possible determinant ` `// of 0/n matrix. ` `#include ` `using` `namespace` `std; ` ` `  `// Function for maximum determinant ` `int` `maxDet(``int` `n) ` `{ ` `    ``return` `(2*n*n*n); ` `} ` ` `  `// Function to print resulatant matrix ` `void` `resMatrix ( ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < 3; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0; j < 3; j++) ` `        ``{ ` `            ``// three position where 0 appears ` `            ``if` `(i == 0 && j == 2) ` `                ``cout << ``"0 "``; ` `            ``else` `if` `(i == 1 && j == 0) ` `                ``cout << ``"0 "``; ` `            ``else` `if` `(i == 2 && j == 1) ` `                ``cout << ``"0 "``; ` ` `  `            ``// position where n appears ` `            ``else` `                ``cout << n << ``" "``; ` `        ``} ` `        ``cout << ``"\n"``; ` `    ``} ` `}  ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 15; ` `    ``cout << ``"Maximum Determinant = "` `<< maxDet(n); ` ` `  `    ``cout << ``"\nResultant Matrix :\n"``; ` `    ``resMatrix(n);  ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find maximum possible ` `// determinant of 0/n matrix. ` `import` `java.io.*; ` ` `  `public` `class` `GFG ` `{ ` `     `  `// Function for maximum determinant ` `static` `int` `maxDet(``int` `n) ` `{ ` `    ``return` `(``2` `* n * n * n); ` `} ` ` `  ` `  `// Function to print resulatant matrix ` `void` `resMatrix(``int` `n) ` `{ ` `    ``for` `(``int` `i = ``0``; i < ``3``; i++) ` `    ``{ ` `        ``for` `(``int` `j = ``0``; j < ``3``; j++) ` `        ``{ ` `            ``// three position where 0 appears ` `            ``if` `(i == ``0` `&& j == ``2``) ` `                ``System.out.print(``"0 "``); ` `            ``else` `if` `(i == ``1` `&& j == ``0``) ` `                ``System.out.print(``"0 "``); ` `            ``else` `if` `(i == ``2` `&& j == ``1``) ` `                ``System.out.print(``"0 "``); ` ` `  `            ``// position where n appears ` `            ``else` `                ``System.out.print(n +``" "``); ` `        ``} ` `        ``System.out.println(``""``); ` `    ``} ` `}  ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `            ``int` `n = ``15``; ` `            ``GFG geeks=``new` `GFG(); ` `            ``System.out.println(``"Maximum Determinant = "` `                                ``+ maxDet(n)); ` ` `  `            ``System.out.println(``"Resultant Matrix :"``);  ` `            ``geeks.resMatrix(n);  ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python3

 `# Python 3 program to find maximum ` `# possible determinant of 0/n matrix.  ` `# Function for maximum determinant ` `def` `maxDet(n): ` `    ``return` `2` `*` `n ``*` `n ``*` `n ` ` `  `# Function to print resulatant matrix  ` `def` `resMatrix(n): ` `    ``for` `i ``in` `range``(``3``): ` `        ``for` `j ``in` `range``(``3``): ` ` `  `            ``# three position where 0 appears ` `            ``if` `i ``=``=` `0` `and` `j ``=``=` `2``: ` `                ``print``(``"0"``, end ``=` `" "``) ` `            ``elif` `i ``=``=` `1` `and` `j ``=``=` `0``: ` `                ``print``(``"0"``, end ``=` `" "``) ` `            ``elif` `i ``=``=` `2` `and` `j ``=``=` `1``: ` `                ``print``(``"0"``, end ``=` `" "``) ` ` `  `            ``# position where n appears ` `            ``else``: ` `                ``print``(n, end ``=` `" "``) ` `        ``print``(``"\n"``) ` `         `  `# Driver code ` `n ``=` `15` `print``(``"Maximum Detrminat="``, maxDet(n)) ` `print``(``"Resultant Matrix:"``) ` `resMatrix(n) ` ` `  `# This code is contributed by Shrikant13 `

## C#

 `// C# program to find maximum possible ` `// determinant of 0/n matrix. ` `using` `System; ` ` `  `public` `class` `GFG ` `{ ` `     `  `// Function for maximum determinant ` `static` `int` `maxDet(``int` `n) ` `{ ` `    ``return` `(2 * n * n * n); ` `} ` ` `  ` `  `// Function to print resulatant matrix ` `void` `resMatrix(``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < 3; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0; j < 3; j++) ` `        ``{ ` `            ``// three position where 0 appears ` `            ``if` `(i == 0 && j == 2) ` `                ``Console.Write(``"0 "``); ` `            ``else` `if` `(i == 1 && j == 0) ` `                ``Console.Write(``"0 "``); ` `            ``else` `if` `(i == 2 && j == 1) ` `                ``Console.Write(``"0 "``); ` ` `  `            ``// position where n appears ` `            ``else` `                ``Console.Write(n +``" "``); ` `        ``} ` `        ``Console.WriteLine(``""``); ` `    ``} ` `}  ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main (String []args) ` `    ``{ ` `            ``int` `n = 15; ` `            ``GFG geeks=``new` `GFG(); ` `            ``Console.WriteLine(``"Maximum Determinant = "` `                                ``+ maxDet(n)); ` ` `  `            ``Console.WriteLine(``"Resultant Matrix :"``);  ` `            ``geeks.resMatrix(n);  ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```Maximum Determinant = 6750
Resultant Matrix :
15 15  0
0  15 15
15 0  15
```

Exercise: Extend the above solution for a generalized k x k matrix.

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

Improved By : vt_m, nitin mittal, shrikanth13

Article Tags :
Practice Tags :

1

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