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(2^{N*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++

`// 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*

*filter_none*

**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.

## Recommended Posts:

- Product of middle row and column in an odd square matrix
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Count ways to form minimum product triplets
- Count the number of ways to traverse a Matrix
- Count of ways to traverse a Matrix according to given conditions
- Count number of ways to reach a given score in a Matrix
- Count of ways to traverse a Matrix and return to origin in K steps
- Count of ways to select K consecutive empty cells from a given Matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Sum of middle row and column in Matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Minimum element of each row and each column in a matrix
- Maximum sum of a Matrix where each value is from a unique row and column
- Check if sums of i-th row and i-th column are same in matrix
- Find column with maximum sum in a Matrix
- Print an N x M matrix such that each row and column has all the vowels in it

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.