Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
Last Updated :
30 May, 2022
Given four integers N, M, A, B where N is the number of rows and M is the number of columns, the task is to check if is possible to create a binary matrix of dimensions N x M such that every row has A number of 1s and every column has a B number of 1s. If any such matrix is possible then print it else print “-1”.
Examples:
Input: N = 3, M = 6, A = 2, B = 1
Output:
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
Explanation:
Every row has A ones i.e. 2 and every column has B ones i.e., 1.
Input: N = 2, M = 2, A = 2, B = 1
Output: No
Explanation:
It is not possible to create such a 2 x 2 matrix in which every row has 2 ones and every column has 1 ones because of the following two observations:
1. For every row place two ones because of which we will never be able to have one 1 in every column.
1 1
1 1
2. For every column place one 1 because of which we can never have 2 ones in every row.
1 0
0 1
Approach: The idea is to observe that since each row should have exactly A 1s, and each column should have exactly B 1s, hence the number of ones in all rows A * N should be equal to the number of 1s in all columns B * M. Thus, the desired matrix exists if and only if A*N = B*M. Below is the illustration:
- Find any number 0 < d < M such that (d * N)%M == 0, where A % B is the remainder of dividing A by B.
- In the first row of the desired matrix, insert the ones at the positions [1, A].
- In the ith row, put the ones, as in the i – 1 row, but cyclically shifted by d to the right.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int n = 3;
const int m = 6;
void printMatrix( int arr[][m],
string ans)
{
if (ans == "No" )
cout << "No\n" ;
else {
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++)
cout << arr[i][j] << " " ;
cout << '\n' ;
}
}
}
void createMatrix( int a, int b)
{
int matrix[n][m], row[n], col[m];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
matrix[i][j] = 0;
}
}
for ( int i = 0; i < n; i++)
row[i] = a;
for ( int i = 0; i < m; i++)
col[i] = b;
int l = 0, d = 0;
if (n * a != m * b)
printMatrix(matrix, "No" );
else {
for ( int i = 0; i < n; i++) {
int j;
if (l == m)
l = 0;
for (j = l; j < m; j++) {
if (row[i] > 0 && col[j] > 0) {
matrix[i][j] = 1;
row[i]--;
col[j]--;
d = j;
}
}
l = d + 1;
if (row[i] != 0) {
for (j = 0; j < m; j++) {
if (row[i] > 0 && col[j] > 0) {
matrix[i][j] = 1;
row[i]--;
col[j]--;
l = j + 1;
}
if (row[i] == 0)
break ;
}
}
}
printMatrix(matrix, "Yes" );
}
}
int main()
{
int a = 2;
int b = 1;
createMatrix(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int n = 3 ;
static int m = 6 ;
static void printMatrix( int arr[][],
String ans)
{
if (ans == "No" )
System.out.print( "No\n" );
else
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
System.out.print(arr[i][j] + " " );
System.out.println();
}
}
}
static void createMatrix( int a, int b)
{
int [][]matrix = new int [n][m];
int []row = new int [n];
int []col = new int [m];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
matrix[i][j] = 0 ;
}
}
for ( int i = 0 ; i < n; i++)
row[i] = a;
for ( int i = 0 ; i < m; i++)
col[i] = b;
int l = 0 , d = 0 ;
if (n * a != m * b)
printMatrix(matrix, "No" );
else
{
for ( int i = 0 ; i < n; i++)
{
int j;
if (l == m)
l = 0 ;
for (j = l; j < m; j++)
{
if (row[i] > 0 && col[j] > 0 )
{
matrix[i][j] = 1 ;
row[i]--;
col[j]--;
d = j;
}
}
l = d + 1 ;
if (row[i] != 0 )
{
for (j = 0 ; j < m; j++)
{
if (row[i] > 0 && col[j] > 0 )
{
matrix[i][j] = 1 ;
row[i]--;
col[j]--;
l = j + 1 ;
}
if (row[i] == 0 )
break ;
}
}
}
printMatrix(matrix, "Yes" );
}
}
public static void main(String[] args)
{
int a = 2 ;
int b = 1 ;
createMatrix(a, b);
}
}
|
Python3
n = 3
m = 6
def printMatrix(arr, ans):
if (ans = = "No" ):
print ( "No" )
else :
for i in range (n):
for j in range (m):
print (arr[i][j], end = " " )
print ()
def createMatrix(a, b):
matrix = [[ 0 for i in range (m)]
for i in range (n)]
row = [a for i in range (n)]
col = [b for i in range (m)]
l = 0
d = 0
if (n * a ! = m * b):
printMatrix(matrix, "No" )
else :
for i in range (n):
j = 0
if (l = = m):
l = 0
for j in range (l, m):
if (row[i] > 0 and col[j] > 0 ):
matrix[i][j] = 1
row[i] - = 1
col[j] - = 1
d = j
l = d + 1
if (row[i] ! = 0 ):
for j in range (m):
if (row[i] > 0 and col[j] > 0 ):
matrix[i][j] = 1
row[i] - = 1
col[j] - = 1
l = j + 1
if (row[i] = = 0 ):
break
printMatrix(matrix, "Yes" )
if __name__ = = '__main__' :
a = 2
b = 1
createMatrix(a, b)
|
C#
using System;
class GFG{
static int n = 3;
static int m = 6;
static void printMatrix( int [,]arr,
String ans)
{
if (ans == "No" )
Console.Write( "No\n" );
else
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
Console.Write(arr[i, j] + " " );
Console.WriteLine();
}
}
}
static void createMatrix( int a, int b)
{
int [,]matrix = new int [n, m];
int []row = new int [n];
int []col = new int [m];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
matrix[i, j] = 0;
}
}
for ( int i = 0; i < n; i++)
row[i] = a;
for ( int i = 0; i < m; i++)
col[i] = b;
int l = 0, d = 0;
if (n * a != m * b)
printMatrix(matrix, "No" );
else
{
for ( int i = 0; i < n; i++)
{
int j;
if (l == m)
l = 0;
for (j = l; j < m; j++)
{
if (row[i] > 0 && col[j] > 0)
{
matrix[i,j] = 1;
row[i]--;
col[j]--;
d = j;
}
}
l = d + 1;
if (row[i] != 0)
{
for (j = 0; j < m; j++)
{
if (row[i] > 0 && col[j] > 0)
{
matrix[i,j] = 1;
row[i]--;
col[j]--;
l = j + 1;
}
if (row[i] == 0)
break ;
}
}
}
printMatrix(matrix, "Yes" );
}
}
public static void Main(String[] args)
{
int a = 2;
int b = 1;
createMatrix(a, b);
}
}
|
Javascript
<script>
let n = 3;
let m = 6;
function printMatrix(arr,
ans)
{
if (ans == "No" )
document.write( "No\n" );
else
{
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
document.write(arr[i][j] + " " );
document.write( "<br/>" );
}
}
}
function createMatrix(a, b)
{
let matrix = new Array(n);
for ( var i = 0; i < matrix.length; i++) {
matrix[i] = new Array(2);
}
let row = Array.from({length: n}, (_, i) => 0);
let col = Array.from({length: m}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
matrix[i][j] = 0;
}
}
for (let i = 0; i < n; i++)
row[i] = a;
for (let i = 0; i < m; i++)
col[i] = b;
let l = 0, d = 0;
if (n * a != m * b)
printMatrix(matrix, "No" );
else
{
for (let i = 0; i < n; i++)
{
let j;
if (l == m)
l = 0;
for (j = l; j < m; j++)
{
if (row[i] > 0 && col[j] > 0)
{
matrix[i][j] = 1;
row[i]--;
col[j]--;
d = j;
}
}
l = d + 1;
if (row[i] != 0)
{
for (j = 0; j < m; j++)
{
if (row[i] > 0 && col[j] > 0)
{
matrix[i][j] = 1;
row[i]--;
col[j]--;
l = j + 1;
}
if (row[i] == 0)
break ;
}
}
}
printMatrix(matrix, "Yes" );
}
}
let a = 2;
let b = 1;
createMatrix(a, b);
</script>
|
Output:
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
Time Complexity: O(N*M), as we are using nested loops to traverse N*M times.
Auxiliary Space: O(N*M), as we are using extra space for matrix.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...