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:
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 columns 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 column up to M-1 columns would either be 1 or -1. There are a total 2 (N-1) * (M-1) Ways to form a matrix of size (N-1)*(M-1) filled with 1 or -1. Depending on what is needed as a product of N rows and M columns, the 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 the 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 the product as 1 = 2 (N-1) * (M-1) 
    Number of possible matrices to get the 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 
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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
import java.util.Arrays;
  
class GFG{ 
      
// Function to return the
// number of possible ways
static void Solve(int N, int M)
{
    int temp = (N - 1) * (M - 1);
    int ans = (int)(Math.pow(2, temp));
  
    // Check if product can be -1
    if ((N + M) % 2 != 0)
        System.out.print(ans);
    else
        System.out.print(2 * ans);
}
  
// Driver code 
public static void main (String[] args) 
    int N = 3;
    int M = 3;
      
    Solve(N, M);
  
// This code is contributed by Shubham Prakash 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
# Function to return 
# possible number of ways
def Solve(N, M):
  temp = (N - 1) * (M - 1)
  ans = pow(2, temp)
  
  # Check if product can be -1
  if ((N + M) % 2 != 0):
    print(ans)
    else:
      print(2 * ans)
  
      # driver code
      if __name__ == '__main__':
        N, M = 3, 3
        Solve(N, M)
  
# This code is contributed by Sri_srajit

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
  
class GFG{
      
// Function to return the
// number of possible ways
static void Solve(int N, int M)
{
    int temp = (N - 1) * (M - 1);
    int ans = (int)(Math.Pow(2, temp));
  
    // Check if product can be -1
    if ((N + M) % 2 != 0)
        Console.Write(ans);
    else
        Console.Write(2 * ans);
}
  
// Driver Code
public static void Main(string[] args)
{
    int N = 3;
    int M = 3;
      
    Solve(N, M);
}
}
  
// This code is contributed by rutvik_56

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

Recommended Posts:


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.