Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 iExample
Let us see the following implementation to get better understanding −
#includeusing 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
Advertisements
