Find if a binary matrix exists with given row and column sums
Last Updated :
01 Nov, 2022
Given an array Row[] of size R where Row[i] is the sum of elements of the ith row and another array Column[] of size C where Column[i] is the sum of elements of the ith column. The task is to check if it is possible to construct a binary matrix of R * C dimension which satisfies given row sums and column sums. A binary matrix is a matrix which is filled with only 0's and 1's.
Sum means the number of 1's in particular row or column.
Examples:
Input: Row[] = {2, 2, 2, 2, 2}, Column[] = {5, 5, 0, 0}
Output: YES
Matrix is
{1, 1, 0, 0}
{1, 1, 0, 0}
{1, 1, 0, 0}
{1, 1, 0, 0}
{1, 1, 0, 0}
Input: Row[] = {0, 0, 3} Column[] = {3, 0, 0}
Output: NO
Approach:
- Key idea is that any cell in the matrix will contribute equally to both row and column sum, so sum of all the row sums must be equal to column sums.
- Now, find the maximum of row sums, if this value is greater than the number of non zero column sums than matrix does not exist.
- If the maximum of column sums is greater than the number of non zero row sums than matrix is not possible to construct.
- If all the above 3 conditions is satisfied than matrix exists.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if matrix exists
bool matrix_exist(int row[], int column[], int r, int c)
{
int row_sum = 0;
int column_sum = 0;
int row_max = -1;
int column_max = -1;
int row_non_zero = 0;
int column_non_zero = 0;
// Store sum of rowsums, max of row sum
// number of non zero row sums
for (int i = 0; i < r; i++) {
row_sum += row[i];
row_max = max(row_max, row[i]);
if (row[i])
row_non_zero++;
}
// Store sum of column sums, max of column sum
// number of non zero column sums
for (int i = 0; i < c; i++) {
column_sum += column[i];
column_max = max(column_max, column[i]);
if (column[i])
column_non_zero++;
}
// Check condition 1, 2, 3
if ((row_sum != column_sum) ||
(row_max > column_non_zero) ||
(column_max > row_non_zero))
return false;
return true;
}
// Driver Code
int main()
{
int row[] = { 2, 2, 2, 2, 2 };
int column[] = { 5, 5, 0, 0 };
int r = sizeof(row) / sizeof(row[0]);
int c = sizeof(column) / sizeof(column[0]);
if (matrix_exist(row, column, r, c))
cout << "YES\n";
else
cout << "NO\n";
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
// Function to check if matrix exists
static boolean matrix_exist(int row[], int column[],
int r, int c)
{
int row_sum = 0;
int column_sum = 0;
int row_max = -1;
int column_max = -1;
int row_non_zero = 0;
int column_non_zero = 0;
// Store sum of rowsums, max of row sum
// number of non zero row sums
for (int i = 0; i < r; i++)
{
row_sum += row[i];
row_max = Math.max(row_max, row[i]);
if (row[i] > 0)
{
row_non_zero++;
}
}
// Store sum of column sums, max of column sum
// number of non zero column sums
for (int i = 0; i < c; i++)
{
column_sum += column[i];
column_max = Math.max(column_max, column[i]);
if (column[i] > 0)
{
column_non_zero++;
}
}
// Check condition 1, 2, 3
if ((row_sum != column_sum)
|| (row_max > column_non_zero)
|| (column_max > row_non_zero))
{
return false;
}
return true;
}
// Driver Code
public static void main(String[] args)
{
int row[] = { 2, 2, 2, 2, 2 };
int column[] = { 5, 5, 0, 0 };
int r = row.length;
int c = column.length;
if (matrix_exist(row, column, r, c))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python implementation of the above approach
# Function to check if matrix exists
def matrix_exist(row, column, r, c):
row_sum = 0
column_sum = 0
row_max = -1
column_max = -1
row_non_zero = 0
column_non_zero = 0
# Store sum of rowsums, max of row sum
# number of non zero row sums
for i in range(r):
row_sum += row[i]
row_max = max(row_max, row[i])
if (row[i]):
row_non_zero = row_non_zero + 1
# Store sum of column sums, max of column sum
# number of non zero column sums
for i in range(c):
column_sum = column_sum + column[i]
column_max = max(column_max, column[i])
if (column[i]):
column_non_zero = column_non_zero + 1
# Check condition 1, 2, 3
if ((row_sum != column_sum)
or (row_max > column_non_zero)
or (column_max > row_non_zero)):
return False
return True
# Driver Code
if __name__ == '__main__':
row = [2, 2, 2, 2, 2]
column = [5, 5, 0, 0]
r = len(row)
c = len(column)
if matrix_exist(row, column, r, c):
print("YES")
else:
print("NO")
# this code is contributed by nirajgusain5
C#
// C# implementation of above approach
using System;
public class GFG{
// Function to check if matrix exists
static bool matrix_exist(int[] row, int[] column,
int r, int c)
{
int row_sum = 0;
int column_sum = 0;
int row_max = -1;
int column_max = -1;
int row_non_zero = 0;
int column_non_zero = 0;
// Store sum of rowsums, max of row sum
// number of non zero row sums
for (int i = 0; i < r; i++)
{
row_sum += row[i];
row_max = Math.Max(row_max, row[i]);
if (row[i] > 0)
{
row_non_zero++;
}
}
// Store sum of column sums, max of column sum
// number of non zero column sums
for (int i = 0; i < c; i++)
{
column_sum += column[i];
column_max = Math.Max(column_max, column[i]);
if (column[i] > 0)
{
column_non_zero++;
}
}
// Check condition 1, 2, 3
if ((row_sum != column_sum)
|| (row_max > column_non_zero)
|| (column_max > row_non_zero))
{
return false;
}
return true;
}
// Driver Code
static public void Main ()
{
int[] row = { 2, 2, 2, 2, 2 };
int[] column = { 5, 5, 0, 0 };
int r = row.Length;
int c = column.Length;
if (matrix_exist(row, column, r, c))
Console.Write("YES");
else
Console.Write("NO");
}
}
// This code has been contributed by shubhamsingh10
JavaScript
<script>
// Javascript implementation of the above approach
// Function to check if matrix exists
function matrix_exist(row, column, r, c)
{
var row_sum = 0;
var column_sum = 0;
var row_max = -1;
var column_max = -1;
var row_non_zero = 0;
var column_non_zero = 0;
// Store sum of rowsums, max of row sum
// number of non zero row sums
for (var i = 0; i < r; i++) {
row_sum += row[i];
row_max = Math.max(row_max, row[i]);
if (row[i])
row_non_zero++;
}
// Store sum of column sums, max of column sum
// number of non zero column sums
for (var i = 0; i < c; i++) {
column_sum += column[i];
column_max = Math.max(column_max, column[i]);
if (column[i])
column_non_zero++;
}
// Check condition 1, 2, 3
if ((row_sum != column_sum) ||
(row_max > column_non_zero) ||
(column_max > row_non_zero))
return false;
return true;
}
// Driver Code
var row = [2, 2, 2, 2, 2];
var column = [5, 5, 0, 0];
var r = row.length;
var c = column.length;
if (matrix_exist(row, column, r, c))
document.write( "YES");
else
document.write( "NO");
</script>
Time Complexity : O(N)
Auxiliary Space: O(1)
Similar Reads
Find Matrix With Given Row and Column Sums Given two arrays rowSum[] and colSum[] of size n and m respectively, the task is to construct a matrix of dimensions n à m such that the sum of matrix elements in every ith row is rowSum[i] and the sum of matrix elements in every jth column is colSum[j].Note: The resultant matrix can have only non-n
15 min read
Find row and column pair in given Matrix with equal row and column sum Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns where the sum of elements in the row is equal to the sum of elements in the columns. Examples: Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}Output: {{1, 1}}Explanation: The sum of elements of rows and columns of m
8 min read
Maximize 1s after Flipping exactly one row and one column in given Binary Matrix Given a binary matrix mat[][]. The task is to find the maximum 1's that can be obtained after flipping all the elements of exactly one row followed by flipping all elements of exactly one column. Example: Input: mat[][] = {{1, 0, 1}, {0, 1, 0}, {1, 0, 0}}Output: 8Explanation: Flip row 1 to get: {{1,
9 min read
Check if sums of i-th row and i-th column are same in matrix Given a matrix mat[][] of dimensions n*m, we have to check if the sum of i-th row is equal to the sum of i-th column or not. Note: Check only up to valid row and column numbers i.e. if the dimensions are 3x5, check only for the first 3 rows and columns, i.e. min(n, m).Examples: Input: mat = [[1,2],[
5 min read
Counting sets of 1s and 0s in a binary matrix Given n à m binary matrix, count the number of sets where a set can be formed one or more same values in a row or column. Examples: Input: 1 0 1 0 1 0 Output: 8 Explanation: There are six one-element sets (three 1s and three 0s). There are two two- element sets, the first one consists of the first a
7 min read
Find a Square Matrix such that sum of elements in every row and column is K Given two integers N and K, the task is to find an N x N square matrix such that sum of every row and column should be equal to K. Note that there can be multiple such matrices possible. Print any one of them.Examples: Input: N = 3, K = 15 Output: 2 7 6 9 5 1 4 3 8Input: N = 3, K = 7 Output: 7 0 0 0
4 min read