Count of ways to generate a Matrix with product of each row and column as 1 or -1
Last Updated :
05 May, 2022
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 column as 1 are,
{{1, 1}, {1, 1}} and {{-1, -1}, {-1, -1}}
Possible ways to get product of each row and column 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 matrices 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++
#include<bits/stdc++.h>
using namespace std;
void Solve( int N, int M)
{
int temp = (N - 1) * (M - 1);
int ans = pow (2, temp);
if ((N + M) % 2 != 0)
cout << ans;
else
cout << 2 * ans;
cout << endl;
}
int main()
{
int N = 3;
int M = 3;
Solve(N, M);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static void Solve( int N, int M)
{
int temp = (N - 1 ) * (M - 1 );
int ans = ( int )(Math.pow( 2 , temp));
if ((N + M) % 2 != 0 )
System.out.print(ans);
else
System.out.print( 2 * ans);
}
public static void main (String[] args)
{
int N = 3 ;
int M = 3 ;
Solve(N, M);
}
}
|
Python3
def Solve(N, M):
temp = (N - 1 ) * (M - 1 )
ans = pow ( 2 , temp)
if ((N + M) % 2 ! = 0 ):
print (ans)
else :
print ( 2 * ans)
if __name__ = = '__main__' :
N, M = 3 , 3
Solve(N, M)
|
C#
using System;
class GFG{
static void Solve( int N, int M)
{
int temp = (N - 1) * (M - 1);
int ans = ( int )(Math.Pow(2, temp));
if ((N + M) % 2 != 0)
Console.Write(ans);
else
Console.Write(2 * ans);
}
public static void Main( string [] args)
{
int N = 3;
int M = 3;
Solve(N, M);
}
}
|
Javascript
<script>
function Solve(N, M)
{
let temp = (N - 1) * (M - 1);
let ans = (Math.pow(2, temp));
if ((N + M) % 2 != 0)
document.write(ans);
else
document.write(2 * ans);
}
let N = 3;
let M = 3;
Solve(N, M);
</script>
|
C
#include<stdio.h>
#include<math.h>
void Solve( int N, int M)
{
int temp = (N - 1) * (M - 1);
int ans = pow (2, temp);
if ((N + M) % 2 != 0)
printf ( "%d" ,ans);
else
printf ( "%d" ,(2*ans));
printf ( "\n" );
}
void main()
{
int N = 3;
int M = 3;
Solve(N, M);
}
|
Time complexity: O(log(N*M))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...