Open In App

Count of ways to make sum of all columns 0 by changing any number of 1s in Matrix to -1

Last Updated : 04 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a matrix of size N x M initially filled with 1, the task is to find the number of ways to make the sum of all columns 0 by changing any number of 1’s to -1.

Note: As the answer can be very large print it modulo 1e9 + 7

Examples:  

Input:  N = 2, M = 1
Output: 2
Explanation: Upper example consist 2 rows and 1 column and all cells are filled with 1. 
So to make individual column sum = 0, there are 2 possibilities [1, -1] and [-1, 1].

Input: N = 4, M = 2
Output: 36 

 

Approach: The idea to solve this problem is based on the following observation:

To make column sum 0 there is need to change N/2 number of 1 to -1. The number of ways to do is X = NCN/2 which can be found using the formula of combinatorics. So total number of ways to make all column sum 0 are XM.

Follow the illustration given below for a better understanding of the approach:

Illustration:

Now consider N = 4 and M = 2
So the matrix is:
1 1
1 1
1 1
1 1
Here each column consist of 4 ones, Consider 1st column to make column sum equal to zero replace two 1 with -1
1. -1      2. -1      3. -1      4. 1      5. 1      6. 1      
    -1           1           1         -1         -1         1
    1           -1           1         -1         1         -1
    1            1          -1          1         -1        -1 

There are total 4C2 = 6 ways to make the sum of first column sum 0 as seen from above:
For each way to change sum of first column 0 there are 6 ways to make second column sum to 0.
So total number of ways  = 6 * 6 = 62 = 36

Follow the steps mentioned below to solve the problem:

  • Use the formula of combinatorics NCN/2 = ( N! / ((N/2)! * (N/2)!) ) to get possible ways for a single column (say X).
  • Now calculate XM
  • This will be the required final answer.

Below is the implementation of the above approach:

C++14




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate factorial
long long fact(long long x)
{
    long long Fact = 1;
    for (long long i = x; i >= 1; i--) {
        Fact = (Fact * i);
    }
    return Fact;
}
 
// Function to calculate
// total number of ways
long long solve(long long A, long long B)
{
    if (A % 2) {
        return 0;
    }
    long long ans = 1, mod = 1000000007;
    long long A_C_Aby2
        = (fact(A) / (fact(A / 2)
                      * fact(A / 2)));
    for (long long i = 0; i < B; i++) {
        ans = (ans * A_C_Aby2) % mod;
    }
    return ans;
}
 
// Driver code
int main()
{
    long long A = 4;
    long long B = 2;
    long long ways = solve(A, B);
    cout << ways << endl;
}


Java




// Java code to implement the approach
import java.io.*;
class GFG {
 
  // Function to calculate factorial
  static long fact(long x)
  {
    long Fact = 1;
    for (long i = x; i >= 1; i--) {
      Fact = (Fact * i);
    }
    return Fact;
  }
 
  // Function to calculate
  // total number of ways
  static long solve( long A,  long B)
  {
    if (A % 2 == 1) {
      return 0;
    }
    long  ans = 1, mod = 1000000007;
    long  A_C_Aby2
      = (fact(A) / (fact(A / 2)
                    * fact(A / 2)));
    for (long  i = 0; i < B; i++) {
      ans = (ans * A_C_Aby2) % mod;
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    long A = 4;
    long B = 2;
    long ways = solve(A, B);
    System.out.println(ways);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# python3 code to implement the approach
 
# Function to calculate factorial
def fact(x):
 
    Fact = 1
    for i in range(x, 0, -1):
        Fact = (Fact * i)
 
    return Fact
 
# Function to calculate
# total number of ways
def solve(A, B):
 
    if (A % 2):
        return 0
 
    ans, mod = 1, 1000000007
    A_C_Aby2 = (fact(A) // (fact(A // 2)
                            * fact(A // 2)))
    for i in range(0, B):
        ans = (ans * A_C_Aby2) % mod
 
    return ans
 
# Driver code
if __name__ == "__main__":
 
    A = 4
    B = 2
    ways = solve(A, B)
    print(ways)
 
# This code is contributed by rakeshsahni


C#




// C# code to implement the approach
using System;
 
class GFG {
 
  // Function to calculate factorial
  static long fact(long x)
  {
    long Fact = 1;
    for (long i = x; i >= 1; i--) {
      Fact = (Fact * i);
    }
    return Fact;
  }
 
  // Function to calculate
  // total number of ways
  static long solve(long A, long B)
  {
    if (A % 2 == 1) {
      return 0;
    }
    long ans = 1, mod = 1000000007;
    long A_C_Aby2
      = (fact(A) / (fact(A / 2) * fact(A / 2)));
    for (long i = 0; i < B; i++) {
      ans = (ans * A_C_Aby2) % mod;
    }
    return ans;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    long A = 4;
    long B = 2;
    long ways = solve(A, B);
    Console.WriteLine(ways);
  }
}
 
// This code is contributed by ukasp.


Javascript




<script>
    // JavaScript code to implement the approach
 
    // Function to calculate factorial
    const fact = (x) => {
        x = parseInt(x);
        let Fact = 1;
        for (let i = x; i >= 1; i--) {
            Fact = (Fact * i);
        }
        return Fact;
    }
 
    // Function to calculate
    // total number of ways
    let solve = (A, B) => {
        if (A % 2) {
            return 0;
        }
        let ans = 1, mod = 1000000007;
        let A_C_Aby2 = (fact(A) / (fact(A / 2)
            * fact(A / 2)));
        for (let i = 0; i < B; i++) {
            ans = (ans * A_C_Aby2) % mod;
        }
        return ans;
    }
 
    // Driver code
    let A = 4;
    let B = 2;
    let ways = solve(A, B);
    document.write(ways);
 
// This code is contributed by rakeshsahni
 
</script>


 
 

Output

36

 

Time Complexity: O(N + M)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads