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 existsbool 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 Codeint 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 approachimport 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 Codepublic 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 existsdef 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 Codeif __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 approachusing 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 existsfunction 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 Codevar 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> |
YES
Â
Time Complexity : O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
