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 = 2Output:4Explanation:

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 = 3Output:32Explanation:

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(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 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++ 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*

*filter_none*

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

*filter_none*

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

*filter_none*

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

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

- 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
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Minimum operations required to make each row and column of matrix equals
- Print an N x M matrix such that each row and column has all the vowels in it
- Maximum sum of a Matrix where each value is from a unique row and column
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
- Product of middle row and column in an odd square matrix
- Count positions in Binary Matrix having equal count of set bits in corresponding row and column
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Count 1s in binary matrix having remaining indices of its row and column filled with 0s
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- 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?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Check if sums of i-th row and i-th column are same in 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 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.