Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C

Given two numbers x, y which denotes the number of set bits. Also given is a number C. The task is to print the number of ways in which we can form two numbers A and B such that A has x number of set bits and B has y number of set bits and A+B = C.
Examples:Â
Â
Input: X = 1, Y = 1, C = 3 Output: 2 So two possible ways are (A = 2 and B = 1) and (A = 1 and B = 2) Input: X = 2, Y = 2, C = 20 Output: 3
Â
Recursive Approach: The above problem can be solved using recursion. Calculate the total count of pairs recursively. There will have 4 possibilities. We start from the rightmost bit position.
- If the bit position at C is 1, then there are four possibilities to get 1 at that index.
- If the carry is 0, then we can use 1 bit out of X and 0 bit out of Y, or the vice-versa which generates no carry for next step.
- If the carry is 1, then we can use 1 set bit’s from each which generates a carry for the next step else we use no set bits from X and Y which generates no carry.
- If the bit position at C is 0, then there are four possibilities to get 0 at that bit position.
- If the carry is 1, then we can use 1 set bit out of X and 0 bit out of Y or the vice versa which generates a carry of 1 for the next step.
- If the carry is 0, then we can use 1 and 1 out of X and Y respectively, which generates a carry of 1 for next step. We can also use no set bits, which generates no carry for the next step.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
// Recursive function to generate// all combinations of bitsint func(int third, int seta, int setb,         int carry, int number){Â
    // find if C has no more set bits on left    int shift = (number >> third);Â
    // if no set bits are left for C    // and there are no set bits for A and B    // and the carry is 0, then    // this combination is possible    if (shift == 0 and seta == 0 and setb == 0 and carry == 0)        return 1;Â
    // if no set bits are left for C and    // requirement of set bits for A and B have exceeded    if (shift == 0 or seta < 0 or setb < 0)        return 0;Â
    // Find if the bit is 1 or 0 at    // third index to the left    int mask = shift & 1;         int ans = 0;         // carry = 1 and bit set = 1    if ((mask) && carry) {Â
        // since carry is 1, and we need 1 at C's bit position        // we can use 0 and 0        // or 1 and 1 at A and B bit position        ans += func(third + 1, seta, setb, 0, number)                + func(third + 1, seta - 1, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 1    else if (mask && !carry) {Â
        // since carry is 0, and we need 1 at C's bit position        // we can use 1 and 0        // or 0 and 1 at A and B bit position        ans += func(third + 1, seta - 1, setb, 0, number)                + func(third + 1, seta, setb - 1, 0, number);    }Â
    // carry = 1 and bit set = 0    else if (!mask && carry) {Â
        // since carry is 1, and we need 0 at C's bit position        // we can use 1 and 0        // or 0 and 1 at A and B bit position        ans += func(third + 1, seta - 1, setb, 1, number)                  + func(third + 1, seta, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 0    else if (!mask && !carry) {Â
        // since carry is 0, and we need 0 at C's bit position        // we can use 0 and 0        // or 1 and 1 at A and B bit position        ans += func(third + 1, seta, setb, 0, number)                  + func(third + 1, seta - 1, setb - 1, 1, number);    }         // Return ans    return ans;}Â
// Function to count waysint possibleSwaps(int a, int b, int c){    // Call func    return func(0, a, b, 0, c);}Â
// Driver Codeint main(){Â
    int x = 2, y = 2, c = 20;Â
    cout << possibleSwaps(x, y, c);    return 0;} |
Java
// Java implementation of above approachimport java.util.*;Â
public class GFG{Â
// Recursive function to generate// all combinations of bitsstatic int func(int third, int seta, int setb,         int carry, int number){Â
    // find if C has no more set bits on left    int shift = (number >> third);Â
    // if no set bits are left for C    // and there are no set bits for A and B    // and the carry is 0, then    // this combination is possible    if (shift == 0 && seta == 0 && setb == 0 && carry == 0)        return 1;Â
    // if no set bits are left for C and    // requirement of set bits for A and B have exceeded    if (shift == 0 || seta < 0 || setb < 0)        return 0;Â
    // Find if the bit is 1 or 0 at    // third index to the left    int mask = (shift & 1);         int ans = 0;         // carry = 1 and bit set = 1    if ((mask) == 1 && carry == 1) {Â
        // since carry is 1, and we need 1 at C's bit position        // we can use 0 and 0        // or 1 and 1 at A and B bit position        ans += func(third + 1, seta, setb, 0, number)                + func(third + 1, seta - 1, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 1    else if (mask == 1 && carry == 0) {Â
        // since carry is 0, and we need 1 at C's bit position        // we can use 1 and 0        // or 0 and 1 at A and B bit position        ans += func(third + 1, seta - 1, setb, 0, number)                + func(third + 1, seta, setb - 1, 0, number);    }Â
    // carry = 1 and bit set = 0    else if (mask == 0 && carry == 1) {Â
        // since carry is 1, and we need 0 at C's bit position        // we can use 1 and 0        // or 0 and 1 at A and B bit position        ans += func(third + 1, seta - 1, setb, 1, number)                  + func(third + 1, seta, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 0    else if (mask == 0 && carry == 0) {Â
        // since carry is 0, and we need 0 at C's bit position        // we can use 0 and 0        // or 1 and 1 at A and B bit position        ans += func(third + 1, seta, setb, 0, number)                  + func(third + 1, seta - 1, setb - 1, 1, number);    }         // Return ans    return ans;}Â
// Function to count waysstatic int possibleSwaps(int a, int b, int c){    // Call func    return func(0, a, b, 0, c);}Â
// Driver Codepublic static void main(String args[]){Â
    int x = 2, y = 2, c = 20;Â
    System.out.println(possibleSwaps(x, y, c));}}// This code is contributed by Samim Hossain Mondal. |
Python3
# Python implementation of above approachÂ
# Recursive function to generate# all combinations of bitsdef func(third, seta, setb, carry, number):Â
    # find if C has no more set bits on left    shift = (number >> third);Â
    # if no set bits are left for C    # and there are no set bits for A and B    # and the carry is 0, then    # this combination is possible    if (shift == 0 and seta == 0 and setb == 0 and carry == 0):        return 1;Â
    # if no set bits are left for C and    # requirement of set bits for A and B have exceeded    if (shift == 0 or seta < 0 or setb < 0):        return 0;Â
    # Find if the bit is 1 or 0 at    # third index to the left    mask = (shift & 1);Â
    ans = 0;Â
    # carry = 1 and bit set = 1    if ((mask) == 1 and carry == 1):Â
        # since carry is 1, and we need 1 at C's bit position        # we can use 0 and 0        # or 1 and 1 at A and B bit position        ans += func(third + 1, seta, setb, 0, number) + func(third + 1, seta - 1, setb - 1, 1, number);         # carry = 0 and bit set = 1    elif(mask == 1 and carry == 0):Â
        # since carry is 0, and we need 1 at C's bit position        # we can use 1 and 0        # or 0 and 1 at A and B bit position        ans += func(third + 1, seta - 1, setb, 0, number) + func(third + 1, seta, setb - 1, 0, number);         # carry = 1 and bit set = 0    elif(mask == 0 and carry == 1):Â
        # since carry is 1, and we need 0 at C's bit position        # we can use 1 and 0        # or 0 and 1 at A and B bit position        ans += func(third + 1, seta - 1, setb, 1, number) + func(third + 1, seta, setb - 1, 1, number);         # carry = 0 and bit set = 0    elif(mask == 0 and carry == 0):Â
        # since carry is 0, and we need 0 at C's bit position        # we can use 0 and 0        # or 1 and 1 at A and B bit position        ans += func(third + 1, seta, setb, 0, number) + func(third + 1, seta - 1, setb - 1, 1, number);         # Return ans    return ans;Â
# Function to count waysdef possibleSwaps(a, b, c):    # Call func    return func(0, a, b, 0, c);Â
# Driver Codeif __name__ == '__main__':Â
    x = 2;    y = 2;    c = 20;Â
    print(possibleSwaps(x, y, c));Â
# This code is contributed by Rajput-Ji |
C#
// C# implementation of above approachusing System;public class GFG {Â
  // Recursive function to generate  // all combinations of bits  static int func(int third, int seta, int setb,                   int carry, int number)   {Â
    // find if C has no more set bits on left    int shift = (number >> third);Â
    // if no set bits are left for C    // and there are no set bits for A and B    // and the carry is 0, then    // this combination is possible    if (shift == 0 && seta == 0 && setb == 0 && carry == 0)      return 1;Â
    // if no set bits are left for C and    // requirement of set bits for A and B have exceeded    if (shift == 0 || seta < 0 || setb < 0)      return 0;Â
    // Find if the bit is 1 or 0 at    // third index to the left    int mask = (shift & 1);Â
    int ans = 0;Â
    // carry = 1 and bit set = 1    if ((mask) == 1 && carry == 1) {Â
      // since carry is 1, and we need 1 at C's bit position      // we can use 0 and 0      // or 1 and 1 at A and B bit position      ans += func(third + 1, seta, setb, 0, number) +         func(third + 1, seta - 1, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 1    else if (mask == 1 && carry == 0) {Â
      // since carry is 0, and we need 1 at C's bit position      // we can use 1 and 0      // or 0 and 1 at A and B bit position      ans += func(third + 1, seta - 1, setb, 0, number) +         func(third + 1, seta, setb - 1, 0, number);    }Â
    // carry = 1 and bit set = 0    else if (mask == 0 && carry == 1) {Â
      // since carry is 1, and we need 0 at C's bit position      // we can use 1 and 0      // or 0 and 1 at A and B bit position      ans += func(third + 1, seta - 1, setb, 1, number) +         func(third + 1, seta, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 0    else if (mask == 0 && carry == 0) {Â
      // since carry is 0, and we need 0 at C's bit position      // we can use 0 and 0      // or 1 and 1 at A and B bit position      ans += func(third + 1, seta, setb, 0, number) +         func(third + 1, seta - 1, setb - 1, 1, number);    }Â
    // Return ans    return ans;  }Â
  // Function to count ways  static int possibleSwaps(int a, int b, int c) {    // Call func    return func(0, a, b, 0, c);  }Â
  // Driver Code  public static void Main(String []args) {Â
    int x = 2, y = 2, c = 20;Â
    Console.WriteLine(possibleSwaps(x, y, c));  }}Â
// This code is contributed by umadevi9616 |
Javascript
<script>// javascript implementation of above approach Â
    // Recursive function to generate    // all combinations of bits    function func(third , seta , setb , carry , number) {Â
        // find if C has no more set bits on left        var shift = (number >> third);Â
        // if no set bits are left for C        // and there are no set bits for A and B        // and the carry is 0, then        // this combination is possible        if (shift == 0 && seta == 0 && setb == 0 && carry == 0)            return 1;Â
        // if no set bits are left for C and        // requirement of set bits for A and B have exceeded        if (shift == 0 || seta < 0 || setb < 0)            return 0;Â
        // Find if the bit is 1 or 0 at        // third index to the left        var mask = (shift & 1);Â
        var ans = 0;Â
        // carry = 1 and bit set = 1        if ((mask) == 1 && carry == 1) {Â
            // since carry is 1, and we need 1 at C's bit position            // we can use 0 and 0            // or 1 and 1 at A and B bit position            ans += func(third + 1, seta, setb, 0, number) +             func(third + 1, seta - 1, setb - 1, 1, number);        }Â
        // carry = 0 and bit set = 1        else if (mask == 1 && carry == 0) {Â
            // since carry is 0, and we need 1 at C's bit position            // we can use 1 and 0            // or 0 and 1 at A and B bit position            ans += func(third + 1, seta - 1, setb, 0, number) +             func(third + 1, seta, setb - 1, 0, number);        }Â
        // carry = 1 and bit set = 0        else if (mask == 0 && carry == 1) {Â
            // since carry is 1, and we need 0 at C's bit position            // we can use 1 and 0            // or 0 and 1 at A and B bit position            ans += func(third + 1, seta - 1, setb, 1, number) +             func(third + 1, seta, setb - 1, 1, number);        }Â
        // carry = 0 and bit set = 0        else if (mask == 0 && carry == 0) {Â
            // since carry is 0, and we need 0 at C's bit position            // we can use 0 and 0            // or 1 and 1 at A and B bit position            ans += func(third + 1, seta, setb, 0, number) +             func(third + 1, seta - 1, setb - 1, 1, number);        }Â
        // Return ans        return ans;    }Â
    // Function to count ways    function possibleSwaps(a , b , c)    {             // Call func        return func(0, a, b, 0, c);    }Â
    // Driver Code        var x = 2, y = 2, c = 20;        document.write(possibleSwaps(x, y, c));Â
// This code is contributed by Rajput-Ji</script> |
Output
3
Efficient Approach: The above problem can be solved using bitmask DP.Â
Â
- Initialize a 4-D DP array of size 64 * 64 * 64 * 2 as 10^18 has a maximum of 64 set bits with -1
- The first state of the DP array stores the number of bits traversed in C from right. The second state stores the number of set-bits used out of X and third state stores the number of set bits used out of Y. The fourth state is the carry bit which refers to the carry generated when we perform an addition operation.
- The recurrence will have 4 possibilities. We start from the rightmost bit position.
- If the bit position at C is 1, then there are four possibilities to get 1 at that index.Â
- If the carry is 0, then we can use 1 bit out of X and 0 bit out of Y, or the vice-versa which generates no carry for next step.
- If the carry is 1, then we can use 1 set bit’s from each which generates a carry for the next step else we use no set bits from X and Y which generates no carry.
- If the bit position at C is 0, then there are four possibilities to get 0 at that bit position.
- If the carry is 1, then we can use 1 set bit out of X and 0 bit out of Y or the vice versa which generates a carry of 1 for the next step.
- If the carry is 0, then we can use 1 and 1 out of X and Y respectively, which generates a carry of 1 for next step. We can also use no set bits, which generates no carry for the next step.
- Summation of all possibilities is stored in dp[third][seta][setb][carry] to avoid re-visiting same states.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
// Initial DP arrayint dp[64][64][64][2];Â
// Recursive function to generate// all combinations of bitsint func(int third, int seta, int setb,         int carry, int number){Â
    // if the state has already been visited    if (dp[third][seta][setb][carry] != -1)        return dp[third][seta][setb][carry];Â
    // find if C has no more set bits on left    int shift = (number >> third);Â
    // if no set bits are left for C    // and there are no set bits for A and B    // and the carry is 0, then    // this combination is possible    if (shift == 0 and seta == 0 and setb == 0 and carry == 0)        return 1;Â
    // if no set bits are left for C and    // requirement of set bits for A and B have exceeded    if (shift == 0 or seta < 0 or setb < 0)        return 0;Â
    // Find if the bit is 1 or 0 at    // third index to the left    int mask = shift & 1;Â
    dp[third][seta][setb][carry] = 0;Â
    // carry = 1 and bit set = 1    if ((mask) && carry) {Â
        // since carry is 1, and we need 1 at C's bit position        // we can use 0 and 0        // or 1 and 1 at A and B bit position        dp[third][seta][setb][carry]                += func(third + 1, seta, setb, 0, number)                + func(third + 1, seta - 1, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 1    else if (mask && !carry) {Â
        // since carry is 0, and we need 1 at C's bit position        // we can use 1 and 0        // or 0 and 1 at A and B bit position        dp[third][seta][setb][carry]                 += func(third + 1, seta - 1, setb, 0, number)                + func(third + 1, seta, setb - 1, 0, number);    }Â
    // carry = 1 and bit set = 0    else if (!mask && carry) {Â
        // since carry is 1, and we need 0 at C's bit position        // we can use 1 and 0        // or 0 and 1 at A and B bit position        dp[third][seta][setb][carry]                   += func(third + 1, seta - 1, setb, 1, number)                  + func(third + 1, seta, setb - 1, 1, number);    }Â
    // carry = 0 and bit set = 0    else if (!mask && !carry) {Â
        // since carry is 0, and we need 0 at C's bit position        // we can use 0 and 0        // or 1 and 1 at A and B bit position        dp[third][seta][setb][carry]                   += func(third + 1, seta, setb, 0, number)                  + func(third + 1, seta - 1, setb - 1, 1, number);    }Â
    return dp[third][seta][setb][carry];}Â
// Function to count waysint possibleSwaps(int a, int b, int c){Â
    memset(dp, -1, sizeof(dp));Â
    // function call that returns the    // answer    int ans = func(0, a, b, 0, c);Â
    return ans;}Â
// Driver Codeint main(){Â
    int x = 2, y = 2, c = 20;Â
    cout << possibleSwaps(x, y, c);Â
    return 0;} |
Java
// Java implementation of the above approach import java.util.*;class GfG { Â
// Initial DP array static int dp[][][][] = new int[64][64][64][2]; Â
// Recursive function to generate // all combinations of bits static int func(int third, int seta, int setb, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int carry, int number) { Â
    // if the state has already been visited     if (dp[third][seta][setb][carry] != -1)         return dp[third][seta][setb][carry]; Â
    // find if C has no more set bits on left     int shift = (number >> third); Â
    // if no set bits are left for C     // and there are no set bits for A and B     // and the carry is 0, then     // this combination is possible     if (shift == 0 && seta == 0 && setb == 0 && carry == 0)         return 1; Â
    // if no set bits are left for C and     // requirement of set bits for A and B have exceeded     if (shift == 0 || seta < 0 || setb < 0)         return 0; Â
    // Find if the bit is 1 or 0 at     // third index to the left     int mask = shift & 1; Â
    dp[third][seta][setb][carry] = 0; Â
    // carry = 1 and bit set = 1     if ((mask == 1) && carry == 1) { Â
        // since carry is 1, and we need 1 at C's bit position         // we can use 0 and 0         // or 1 and 1 at A and B bit position         dp[third][seta][setb][carry]                             += func(third + 1, seta, setb, 0, number)                             + func(third + 1, seta - 1, setb - 1, 1, number);     } Â
    // carry = 0 and bit set = 1     else if (mask == 1 && carry == 0) { Â
        // since carry is 0, and we need 1 at C's bit position         // we can use 1 and 0         // or 0 and 1 at A and B bit position         dp[third][seta][setb][carry]                                     += func(third + 1, seta - 1, setb, 0, number)                                     + func(third + 1, seta, setb - 1, 0, number);     } Â
    // carry = 1 and bit set = 0     else if (mask == 0 && carry == 1) { Â
        // since carry is 1, and we need 0 at C's bit position         // we can use 1 and 0         // or 0 and 1 at A and B bit position         dp[third][seta][setb][carry] += func(third + 1, seta - 1, setb, 1, number)                                     + func(third + 1, seta, setb - 1, 1, number);     } Â
    // carry = 0 and bit set = 0     else if (mask == 0 && carry == 0) { Â
        // since carry is 0, and we need 0 at C's bit position         // we can use 0 and 0         // or 1 and 1 at A and B bit position         dp[third][seta][setb][carry] += func(third + 1, seta, setb, 0, number)                                     + func(third + 1, seta - 1, setb - 1, 1, number);     } Â
    return dp[third][seta][setb][carry]; } Â
// Function to count ways static int possibleSwaps(int a, int b, int c) { Â Â Â Â for(int q = 0; q < 64; q++)Â Â Â Â {Â Â Â Â Â Â Â Â for(int r = 0; r < 64; r++)Â Â Â Â Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â for(int p = 0; p < 64; p++)Â Â Â Â Â Â Â Â Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â for(int d = 0; d < 2; d++)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dp[q][r][p][d] = -1;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â }Â Â Â Â Â Â
    // function call that returns the     // answer     int ans = func(0, a, b, 0, c); Â
    return ans; } Â
// Driver Code public static void main(String[] args) { Â
    int x = 2, y = 2, c = 20; Â
    System.out.println(possibleSwaps(x, y, c)); }} |
Python3
# Python3 implementation of the above approach Â
# Initial DP array dp = [[[[-1, -1] for i in range(64)] Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â for j in range(64)] Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â for k in range(64)] Â
# Recursive function to generate # all combinations of bits def func(third, seta, setb, carry, number): Â
    # if the state has already been visited     if dp[third][seta][setb][carry] != -1:        return dp[third][seta][setb][carry] Â
    # find if C has no more set bits on left     shift = number >> third Â
    # if no set bits are left for C     # and there are no set bits for A and B     # and the carry is 0, then     # this combination is possible     if (shift == 0 and seta == 0 and        setb == 0 and carry == 0):         return 1Â
    # if no set bits are left for C and     # requirement of set bits for A and B have exceeded     if (shift == 0 or seta < 0 or setb < 0):        return 0Â
    # Find if the bit is 1 or 0 at     # third index to the left     mask = shift & 1Â
    dp[third][seta][setb][carry] = 0Â
    # carry = 1 and bit set = 1     if (mask) and carry: Â
        # since carry is 1, and we need 1 at         # C's bit position we can use 0 and 0         # or 1 and 1 at A and B bit position         dp[third][seta][setb][carry] +=\                func(third + 1, seta, setb, 0, number) + \                func(third + 1, seta - 1, setb - 1, 1, number)          # carry = 0 and bit set = 1     elif mask and not carry: Â
        # since carry is 0, and we need 1 at C's         # bit position we can use 1 and 0         # or 0 and 1 at A and B bit position         dp[third][seta][setb][carry] +=\                func(third + 1, seta - 1, setb, 0, number) + \                func(third + 1, seta, setb - 1, 0, number)          # carry = 1 and bit set = 0     elif not mask and carry: Â
        # since carry is 1, and we need 0 at C's         # bit position we can use 1 and 0         # or 0 and 1 at A and B bit position         dp[third][seta][setb][carry] +=\                func(third + 1, seta - 1, setb, 1, number) + \                func(third + 1, seta, setb - 1, 1, number)          # carry = 0 and bit set = 0     elif not mask and not carry: Â
        # since carry is 0, and we need 0 at C's         # bit position we can use 0 and 0         # or 1 and 1 at A and B bit position         dp[third][seta][setb][carry] += \                func(third + 1, seta, setb, 0, number) + \                func(third + 1, seta - 1, setb - 1, 1, number)          return dp[third][seta][setb][carry] Â
# Function to count ways def possibleSwaps(a, b, c): Â
    # function call that returns the answer     ans = func(0, a, b, 0, c)     return ans Â
# Driver Code if __name__ == "__main__": Â
    x, y, c = 2, 2, 20    print(possibleSwaps(x, y, c)) Â
# This code is contributed by Rituraj Jain |
C#
// C# implementation of the above approach using System;Â
class GFG { Â
// Initial DP array static int [,,,]dp = new int[64, 64, 64, 2]; Â
// Recursive function to generate // all combinations of bits static int func(int third, int seta, int setb, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int carry, int number) { Â
    // if the state has already been visited     if (third > -1 && seta > -1 &&         setb > -1 && carry > -1)        if(dp[third, seta, setb, carry] != -1)            return dp[third, seta, setb, carry]; Â
    // find if C has no more set bits on left     int shift = (number >> third); Â
    // if no set bits are left for C     // and there are no set bits for A and B     // and the carry is 0, then     // this combination is possible     if (shift == 0 && seta == 0 &&          setb == 0 && carry == 0)         return 1; Â
    // if no set bits are left for C and     // requirement of set bits for A and    // B have exceeded     if (shift == 0 || seta < 0 || setb < 0)         return 0; Â
    // Find if the bit is 1 or 0 at     // third index to the left     int mask = shift & 1; Â
    dp[third, seta, setb, carry] = 0; Â
    // carry = 1 and bit set = 1     if ((mask == 1) && carry == 1)     { Â
        // since carry is 1, and we need 1 at         // C's bit position, we can use 0 and 0         // or 1 and 1 at A and B bit position         dp[third, seta,            setb, carry] += func(third + 1, seta,                                 setb, 0, number) +                           func(third + 1, seta - 1,                                 setb - 1, 1, number);     } Â
    // carry = 0 and bit set = 1     else if (mask == 1 && carry == 0)    { Â
        // since carry is 0, and we need 1 at         // C's bit position, we can use 1 and 0         // or 0 and 1 at A and B bit position         dp[third, seta,            setb, carry] += func(third + 1, seta - 1,                                   setb, 0, number) +                            func(third + 1, seta,                                 setb - 1, 0, number);     } Â
    // carry = 1 and bit set = 0     else if (mask == 0 && carry == 1)     { Â
        // since carry is 1, and we need 0 at        // C's bit position, we can use 1 and 0         // or 0 and 1 at A and B bit position         dp[third, seta,            setb, carry] += func(third + 1, seta - 1,                                 setb, 1, number) +                            func(third + 1, seta,                                 setb - 1, 1, number);     } Â
    // carry = 0 and bit set = 0     else if (mask == 0 && carry == 0)     { Â
        // since carry is 0, and we need 0 at         // C's bit position, we can use 0 and 0         // or 1 and 1 at A and B bit position         dp[third, seta,            setb, carry] += func(third + 1, seta,                                 setb, 0, number) +                            func(third + 1, seta - 1,                                 setb - 1, 1, number);     }     return dp[third, seta, setb, carry]; } Â
// Function to count ways static int possibleSwaps(int a, int b, int c) {     for(int q = 0; q < 64; q++)    {        for(int r = 0; r < 64; r++)        {            for(int p = 0; p < 64; p++)            {                for(int d = 0; d < 2; d++)                {                    dp[q, r, p, d] = -1;                }            }        }    }         // function call that returns the     // answer     int ans = func(0, a, b, 0, c); Â
    return ans; } Â
// Driver Code public static void Main(String[] args) { Â Â Â Â int x = 2, y = 2, c = 20; Â
    Console.WriteLine(possibleSwaps(x, y, c)); }} Â
// This code is contributed by Rajput-Ji |
Javascript
<script>// javascript implementation of above approach Â
    // Recursive function to generate    // all combinations of bits    function func(third , seta , setb , carry , number) {Â
        // find if C has no more set bits on left        var shift = (number >> third);Â
        // if no set bits are left for C        // and there are no set bits for A and B        // and the carry is 0, then        // this combination is possible        if (shift == 0 && seta == 0 && setb == 0 && carry == 0)            return 1;Â
        // if no set bits are left for C and        // requirement of set bits for A and B have exceeded        if (shift == 0 || seta < 0 || setb < 0)            return 0;Â
        // Find if the bit is 1 or 0 at        // third index to the left        var mask = (shift & 1);Â
        var ans = 0;Â
        // carry = 1 and bit set = 1        if ((mask) == 1 && carry == 1) {Â
            // since carry is 1, and we need 1 at C's bit position            // we can use 0 and 0            // or 1 and 1 at A and B bit position            ans += func(third + 1, seta, setb, 0, number) +             func(third + 1, seta - 1, setb - 1, 1, number);        }Â
        // carry = 0 and bit set = 1        else if (mask == 1 && carry == 0) {Â
            // since carry is 0, and we need 1 at C's bit position            // we can use 1 and 0            // or 0 and 1 at A and B bit position            ans += func(third + 1, seta - 1, setb, 0, number) +             func(third + 1, seta, setb - 1, 0, number);        }Â
        // carry = 1 and bit set = 0        else if (mask == 0 && carry == 1) {Â
            // since carry is 1, and we need 0 at C's bit position            // we can use 1 and 0            // or 0 and 1 at A and B bit position            ans += func(third + 1, seta - 1, setb, 1, number) +             func(third + 1, seta, setb - 1, 1, number);        }Â
        // carry = 0 and bit set = 0        else if (mask == 0 && carry == 0) {Â
            // since carry is 0, and we need 0 at C's bit position            // we can use 0 and 0            // or 1 and 1 at A and B bit position            ans += func(third + 1, seta, setb, 0, number) +             func(third + 1, seta - 1, setb - 1, 1, number);        }Â
        // Return ans        return ans;    }Â
    // Function to count ways    function possibleSwaps(a , b , c)    {             // Call func        return func(0, a, b, 0, c);    }Â
    // Driver Code        var x = 2, y = 2, c = 20;        document.write(possibleSwaps(x, y, c));Â
// This code contributed by Rajput-Ji </script> |
Output:Â
3
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



