We are given three values , and where is number of rows in matrix, is number of columns in the matrix and is the number that can have only two values -1 and 1. Our aim is to find the number of ways of filling the matrix of such that the product of all the elements in each row and each column is equal to . Since the number of ways can be large we will output
Input : n = 2, m = 4, k = -1 Output : 8 Following configurations satisfy the conditions:- Input : n = 2, m = 1, k = -1 Output : The number of filling the matrix are 0
From the above conditions, it is clear that the only elements that can be entered in the matrix are 1 and -1. Now we can easily deduce some of the corner cases
- If k = -1, then the sum of number of rows and columns cannot be odd because -1 will be present odd number of times in each row and column therefore if the sum is odd then answer is .
- If n = 1 or m = 1 then there is only one way of filling the matrix therefore answer is 1.
- If none of the above cases are applicable then we fill the first rows and the first columns with 1 and -1. Then the remaining numbers can be uniquely identified since the product of each row an each column is already known therefore the answer is .
The time complexity of above solution is .
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Remove first X rows and columns from a matrix
- Remove any corner X rows and columns from a matrix
- Enlarge a Matrix such that each element occurs in R rows and C columns
- Number of rows and columns in a Matrix that contain repeated values
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Count rows/columns with sum equals to diagonal sum
- Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted
- Sum of columns of a 2-D Matrix where first element is odd
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Interchange elements of first and last columns in matrix
- Count all the columns in a matrix which are sorted in descending
- Count all sorted rows in a matrix
- Find all permuted rows of a given row in a matrix
- Interchange elements of first and last rows in matrix
- Common elements in all rows of a given matrix
- Check if all rows of a matrix are circular rotations of each other
- Find duplicate rows in a binary matrix
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.