C++ code to check given matrix is good or not

Suppose we have one n x n matrix. The matrix is said to be a good matrix where every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. We have to check whether given matrix is good or not.

So, if the input is like

1 1 2
2 3 1
6 4 1

Then the output will be True, because the 6 in the bottom left corner is valid because when the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this matrix.

Steps

To solve this, we will follow these steps −

n := size of M
for initialize i := 0, when i 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
bool solve(vector> M){
   int n = M.size();
   int c;
   bool ok;
   for (int i = 0; i > matrix = { { 1, 1, 2 }, { 2, 3, 1 }, { 6, 4, 1 } };
   cout 

Input

{ { 1, 1, 2 }, { 2, 3, 1 }, { 6, 4, 1 } }

Output

1
Updated on: 2022-03-30T13:15:01+05:30

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements