Open In App

Count of distinct permutations of length N having Bitwise AND as zero

Last Updated : 19 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N., The task is to find the number of distinct permutations of length N, such that the bitwise AND value of each permutation is zero

Examples:

Input: N = 1
Output:
Explanation: There is only one permutation of length 1: [1] and it’s bitwise AND is 1 . 
 

Input: N = 3 
Output:
Explanation: Permutations of length N having bitwise AND as 0 are : [1, 2, 3], [1, 3, 2], [2, 1, 3], [3, 1, 2], [2, 3, 1], [3, 2, 1] . 

 

Approach:  The task can be solved using observations. One can observe that if a number is a power of 2, let’s say ‘x‘, bitwise AND of x & (x-1) is always zero. All permutations of length greater than 1, have bitwise AND as zero, and for N = 1, the count of distinct permutations is 0. Therefore, the required count is equal to the number of possible permutations i.e N!

Below is the implementation of the above approach – 

C++




// C++ program for the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate factorial
// of a number
long long int fact(long long N)
{
    long long int ans = 1;
    for (int i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
long long permutation_count(long long n)
{
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
// Driver code
int main()
{
 
    long long N = 3;
    cout << permutation_count(N);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to calculate factorial
// of a number
static long fact( long N)
{
    long  ans = 1;
    for (int i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
static long  permutation_count(long n)
{
   
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
    public static void main (String[] args) {
       long N = 3;
     System.out.println(permutation_count(N));
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python 3 program for the
# above approach
 
# Function to calculate factorial
# of a number
def fact(N):
 
    ans = 1
    for i in range(2,  N + 1):
        ans *= i
 
    return ans
 
# Function to find distinct no of
# permutations having bitwise and (&)
# equals to 0
def permutation_count(n):
 
    # corner case
    if (n == 1):
        return 0
 
    return fact(n)
 
# Driver code
if __name__ == "__main__":
 
    N = 3
    print(permutation_count(N))
 
    # This code is contributed by ukasp.


C#




// C# program for the
// above approach
using System;
 
class GFG
{
// Function to calculate factorial
// of a number
static long fact(long N)
{
    long ans = 1;
    for (int i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
static long permutation_count(long n)
{
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
// Driver code
public static void Main()
{
 
    long N = 3;
    Console.Write(permutation_count(N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
// Javascript program for the
// above approach
 
// Function to calculate factorial
// of a number
function fact(N)
{
    let ans = 1;
    for (let i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
function permutation_count(n)
{
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
// Driver code
let N = 3;
document.write(permutation_count(N));
 
// This code is contributed by Samim Hossain Mondal.
</script>


Output

6

Time complexity: O(N) 
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads