Open In App

Count ways to represent N as XOR of distinct integers not exceeding N

Given a positive integer N, the task is to find the number of ways to represent N as Bitwise XOR of distinct positive integers less than or equal to N.

Examples:



Input: N = 5
Output: 4
Explanation: The given number N(= 5) can be represented as:

  1. 5 = 5
  2. 5 = (4 ^ 1)
  3. 5 = (5 ^ 3 ^ 2 ^ 1)
  4. 5 = (4 ^ 3 ^ 2)

Therefore, the total count is 4.



Input: N = 6
Output: 8

 

Naive Approach: The simplest approach to solve the problem is to find all subsets of first N natural numbers and count those subsets having Bitwise XOR value N. After checking for all the subsets, print the total value of the count obtained. 

Time Complexity: O(N * 2N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by using the observation that the number of ways to represent N as the Bitwise XOR of distinct positive integers is given by .

Below is the implementation of the above approach:

// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to count number of ways
// to represent N as the Bitwise
// XOR of distinct integers
void countXorPartition(int N)
{
     
    // Count number of subsets using
    // above-mentioned formula
    double a = pow(2, floor(N - log(N + 1) /
                                log(2)));
     
    // Print the resultant count
    cout << a;
}
   
// Driver Code
int main()
{
    int N = 5;
     
    countXorPartition(N);
}
 
// This code is contributed by SURENDRA_GANGWAR

                    
// java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count number of ways
// to represent N as the Bitwise
// XOR of distinct integers
static void countXorPartition(int N)
{
     
    // Count number of subsets using
    // above-mentioned formula
    double a = Math.pow(2, (int)(N - Math.log(N + 1) /
                                Math.log(2)));
     
    // Print the resultant count
    System.out.print(a);
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 5;  
    countXorPartition(N);
}
}
 
// This code is contributed by shivanisinghss2110

                    
# Python program for the above approach
 
from math import * 
 
# Function to count number of ways
# to represent N as the Bitwise
# XOR of distinct integers
def countXorPartition(N):
   
  # Count number of subsets using
  # above-mentioned formula
  a = 2**floor(N - log(N + 1)/log(2))
   
  # Print the resultant count
  print(int(a))
 
# Driver Code
 
N = 5
countXorPartition(N)

                    
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count number of ways
// to represent N as the Bitwise
// XOR of distinct integers
static void countXorPartition(int N)
{
     
    // Count number of subsets using
    // above-mentioned formula
    double a = Math.Pow(2, (int)(N - Math.Log(N + 1) /
                                Math.Log(2)));
     
    // Print the resultant count
    Console.Write(a);
}
   
// Driver Code
public static void Main()
{
    int N = 5;  
    countXorPartition(N);
}
}
 
// This code is contributed by ipg2016107.

                    
<script>
 
// JavaScript program for the above approach
 
// Function to count number of ways
// to represent N as the Bitwise
// XOR of distinct integers
function countXorPartition(N)
{
     
    // Count number of subsets using
    // above-mentioned formula
    let a = Math.pow(2, Math.floor(N - Math.log(N + 1) /
                                Math.log(2)));
     
    // Print the resultant count
    document.write(a);
}
   
// Driver Code
    let N = 5;
     
    countXorPartition(N);
 
</script>

                    

Output: 
4

 

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :