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”.
Input: N = 3, M = 6, A = 2, B = 1
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
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
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.
2. For every column place one 1 because of which we can never have 2 ones in every row.
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:
1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print an N x M matrix such that each row and column has all the vowels in it
- Find a Square Matrix such that sum of elements in every row and column is K
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Place N^2 numbers in matrix such that every row has an equal sum
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Replace every matrix element with maximum of GCD of row or column
- Check if sums of i-th row and i-th column are same in matrix
- Check if the given matrix is increasing row and column wise
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Count number of islands where every island is row-wise and column-wise separated
- Find an N x N grid whose xor of every row and column is equal
- Print all possible paths from the first row to the last row in a 2D array
- Print all elements in sorted order from row and column wise sorted matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.