Number of special pairs possible from the given two numbers

Given two numbers A, B. The task is to find the numbers of special pairs of A, B. A special pair of two numbers A, B is a pair of numbers X, Y which satisfies both of the given conditions – A = X | Y, B = X & Y.
Examples:Â
Input: A = 3, B = 0 Output: 2 (0, 3), (1, 2) will satisfy the conditions Input: A = 5, B = 7 Output: 0
Approach: The key observation here is that if we want the OR of two numbers, X, Y to be equal to A. Then both X, Y has to be less than or equal to A. If anyone is greater A then there OR won’t be equal to A. This will give us the limits where our loop will terminate, rest we will try and check if two pairs meet the given condition, then we will increment the counter.
Below is the required implementation:Â
Â
C++
// C++ implementation of above approach#include <iostream>using namespace std;Â
// Function to count the pairsint countPairs(int A, int B){Â
    // Variable to store a number of special pairs    int cnt = 0;Â
    for (int i = 0; i <= A; ++i) {        for (int j = i; j <= A; ++j) {            // Calculating AND of i, j            int AND = i & j;Â
            // Calculating OR of i, j            int OR = i | j;Â
            // If the conditions are met,            // then increment the count of special pairs            if (OR == A and AND == B) {                cnt++;            }        }    }    return cnt;}Â
// Driver codeint main(){Â Â Â Â int A = 3, B = 0;Â Â Â Â cout << countPairs(A, B);Â
    return 0;} |
Java
// Java implementation of above approachclass GFG{Â
// Function to count the pairsstatic int countPairs(int A, int B){Â
    // Variable to store a number     // of special pairs    int cnt = 0;Â
    for (int i = 0; i <= A; ++i)     {        for (int j = i; j <= A; ++j)         {            // Calculating AND of i, j            int AND = i & j;Â
            // Calculating OR of i, j            int OR = i | j;Â
            // If the conditions are met,            // then increment the count            // of special pairs            if (OR == A && AND == B)             {                cnt++;            }        }    }    return cnt;}Â
// Driver codepublic static void main(String [] args){Â Â Â Â int A = 3, B = 0;Â Â Â Â System.out.println(countPairs(A, B));}}Â
// This code is contributed by ihritik |
Python3
# Python3 implementation of above # approachÂ
# Function to count the pairsdef countPairs(A,B):Â
    # Variable to store a number     # of special pairs    cnt=0    for i in range(0,A+1):        for j in range(i,A+1):Â
            # Calculating AND of i, j            AND = i&j            OR = i|jÂ
            # If the conditions are met,            # then increment the count of             # special pairs            if(OR==A and AND==B):                cnt +=1    return cntÂ
if __name__=='__main__':Â Â Â Â A = 3Â Â Â Â B = 0Â Â Â Â print(countPairs(A,B))Â
# This code is contributed by # Shrikant13 |
C#
// C# implementation of above approachusing System;Â
class GFG{Â Â Â Â Â // Function to count the pairsstatic int countPairs(int A, int B){Â
    // Variable to store a number    // of special pairs    int cnt = 0;Â
    for (int i = 0; i <= A; ++i)     {        for (int j = i; j <= A; ++j)         {            // Calculating AND of i, j            int AND = i & j;Â
            // Calculating OR of i, j            int OR = i | j;Â
            // If the conditions are met,            // then increment the count             // of special pairs            if (OR == A && AND == B)            {                cnt++;            }        }    }    return cnt;}Â
// Driver codepublic static void Main(){Â Â Â Â int A = 3, B = 0;Â Â Â Â Console.WriteLine(countPairs(A, B));}}Â
// This code is contributed by ihritik |
PHP
<?php// PHP implementation of above approachÂ
// Function to count the pairsfunction countPairs($A, $B){Â
    // Variable to store a number    // of special pairs    $cnt = 0;Â
    for ($i = 0; $i <= $A; ++$i)    {        for ($j = $i; $j <= $A; ++$j)         {            // Calculating AND of i, j            $AND = $i & $j;Â
            // Calculating OR of i, j            $OR = $i | $j;Â
            // If the conditions are met,            // then increment the count             // of special pairs            if ($OR == $A && $AND == $B)            {                $cnt++;            }        }    }    return $cnt;}         // Driver code$A = 3;$B = 0;echo countPairs($A, $B);Â
// This code is contributed by ihritik?> |
Javascript
<script>// Javascript implementation of above approachÂ
Â
// Function to count the pairsfunction countPairs(A, B) {Â
    // Variable to store a number of special pairs    let cnt = 0;Â
    for (let i = 0; i <= A; ++i) {        for (let j = i; j <= A; ++j) {            // Calculating AND of i, j            let AND = i & j;Â
            // Calculating OR of i, j            let OR = i | j;Â
            // If the conditions are met,            // then increment the count of special pairs            if (OR == A && AND == B) {                cnt++;            }        }    }    return cnt;}Â
// Driver codeÂ
let A = 3, B = 0;document.write(countPairs(A, B));Â
Â
// This code is contributed by _saurabh_jaiswal<script> |
Output
2
Time Complexity: O(A2)
Auxiliary Space: O(1)
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!



