Maximum Determinant of a Matrix with Values 0 or N in C++



Problem statement

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 determinants.

Example

If n = 15 then we can create matrix as follows −

{{15, 15, 0}{0, 15, 15}{15, 0, 0}}

For any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3. Hence answer is −

2 * (15)3 = 6750

Algorithm

For any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3

Example

Let us now see an example −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int getMaxDeterminant(int n){
   return (2 * n * n * n);
}
void printMatrix(int n){
   for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
         if (i == 0 && j == 2) {
            printf("%-5d", 0);
         } else if (i == 1 && j == 0) {
            printf("%-5d", 0);
         } else if (i == 2 && j == 1) {
            printf("%-5d", 0);
         } else {
            printf("%-5d", n);
         }
      }
      printf("\n");
   }
}
int main() {
   int n = 15;
   cout << "Matrix is:\n";
   printMatrix(n);
   cout << "\nMaximum determinant = " << getMaxDeterminant(n) << endl;
   return 0;
}

Output

Matrix is:
15150
0 15 15
15 015
Maximum determinant = 6750
Updated on: 2019-12-31T12:22:55+05:30

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements