# 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 the 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `using` `namespace` `std; ` ` `  `// Function to count the pairs ` `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 AND == B) { ` `                ``cnt++; ` `            ``} ` `        ``} ` `    ``} ` `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A = 3, B = 0; ` `    ``cout << countPairs(A, B); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `class` `GFG ` `{ ` ` `  `// Function to count the pairs ` `static` `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 code ` `public` `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 pairs ` `def` `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 approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to count the pairs ` `static` `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 code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `A = 3, B = 0; ` `    ``Console.WriteLine(countPairs(A, B)); ` `} ` `} ` ` `  `// This code is contributed by ihritik  `

## PHP

 `  `

Output:

```2
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : shrikanth13, ihritik