Count of ways to generate a Matrix with product of each row and column as 1 or -1

Given two integers N and M, the task is to find the numbers of ways to form a matrix of size N * M consisting only of 1 or -1, such that the product of integers in each row and each column is equal to 1 or -1.

Examples:

Input: N = 2, M = 2
Output: 4
Explanation:
Possible ways to get product of each row and coloumn as 1 are,
{{1, 1}, {1, 1}} and {{-1, -1}, {-1, -1}}
Possible ways to get product of each row and coloumn as -1 are,
{{1, -1}, {-1, 1}} and {{-1, 1}, {1, -1}}
Hence, number of ways = 2 + 2 = 4

Input: N = 3, M = 3
Output: 32
Explanation:
There are 16 ways to get product as 1 and 16 ways to get product as -1.
Hence, number of ways = 16 + 16 = 32

Naive Approach:
The simplest approach to solve this problem is to generate all possible matrix of size N * M and for each of them, calculate the product of all rows and coloumns and check if it is 1 or -1.
Time complexity: O(2N*M)
Auxiliary Space: O(M*N)



Efficient Approach:
Assume, first N-1 rows and first M-1 columns are filled by 1 or -1. Now, the product of each row up to N-1 rows and each coloumn upto M-1 columns would either be 1 or -1. There are total 2 (N-1) * (M-1) ways to form a matrix of size (N-1)*(M-1) filled with 1 or -1. Depending opon what is needed as product of N rows and M columns, last row and column can be filled accordingly.

Follow the steps to solve the problem:

  • If N + M is even,
    Number of possible matrices to get product as 1 = 2 (N-1) * (M-1)
    Number of possible matrices to get product as -1 = 2 (N-1) * (M-1)

  • If N + M is odd,
    Number of possible matrices to get product as 1 = 2 (N-1) * (M-1)
    Number of possible matrices to get product as -1 = 0

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// number of possible ways
void Solve(int N, int M)
{
  
    int temp = (N - 1) * (M - 1);
    int ans = pow(2, temp);
  
    // Check if product can be -1
    if ((N + M) % 2 != 0)
        cout << ans;
    else
        cout << 2 * ans;
  
    cout << endl;
}
// Driver Code
int main()
{
    int N = 3;
    int M = 3;
  
    Solve(N, M);
    return 0;
}

chevron_right


Output:

32

Time complexity: O(log(N*M))
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.