# Sum of decimal equivalent of all possible pairs of Binary representation of a Number

Given a number N. The task is to find the sum of the decimal equivalent of all the pairs formed from the binary representation of the given number.

Examples:

Input: N = 4
Output: 4
Binary equivalent of 4 is 100.
All possible pairs are 10, 10, 00 and their decimal equivalent are 2, 2, 0 respectively.
So, 2 + 2+ 0 = 4

Input: N = 11
Output: 13
All possible pairs are: 10, 11, 11, 01, 01, 11
Sum = 2 + 3 + 3 + 1 + 1 + 3 = 13

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

Approach:

1. Find the binary equivalent of N and store it in a vector.
2. Run two loops to consider each and every pair formed from the bits of binary equivalent stored in the vector.
3. Find the decimal equivalent of all the pairs and add them.
4. Return the sum.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the sum ` `int` `sumOfPairs(``int` `n) ` `{ ` ` `  `    ``// Store the Binary equivalent of decimal ` `    ``// number in reverse order ` `    ``vector<``int``> v; ` `    ``int` `sum = 0; ` ` `  `    ``// Calculate binary equivalent of decimal number ` `    ``while` `(n > 0) { ` `        ``v.push_back(n % 2); ` `        ``n = n / 2; ` `    ``} ` ` `  `    ``// for correct binary representation ` `    ``reverse(v.begin(), v.end()); ` ` `  `    ``// Consider every pair ` `    ``for` `(``int` `i = 0; i < v.size() - 1; i++) { ` `        ``for` `(``int` `j = i + 1; j < v.size(); j++) ` ` `  `        ``{ ` `            ``// handles all combinations of 01 ` `            ``if` `(v[i] == 0 && v[j] == 1) ` `                ``sum += 1; ` ` `  `            ``// handles all combinations of 11 ` `            ``if` `(v[i] == 1 && v[j] == 1) ` `                ``sum += 3; ` ` `  `            ``// handles all combinations of 10 ` `            ``if` `(v[i] == 1 && v[j] == 0) ` `                ``sum += 2; ` `        ``} ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 5; ` ` `  `    ``cout << sumOfPairs(N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `public` `static` `int` `sumOfPairs(``int` `n) ` `{ ` `    ``// Store the Binary equivalent   ` `    ``// of decimal number in reverse order  ` `    ``ArrayList v = ``new` `ArrayList();  ` `    ``int` `sum = ``0``; ` `     `  `    ``// Calculate binary equivalent  ` `    ``// of decimal number  ` `    ``while` `(n > ``0``) ` `    ``{  ` `        ``v.add(n % ``2``);  ` `        ``n = n / ``2``;  ` `} ` ` `  `Collections.reverse(v);  ` `     `  `for` `(``int` `i = ``0``; i < v.size() - ``1``; i++)  ` `{  ` `    ``for` `(``int` `j = i + ``1``; j < v.size(); j++)  ` ` `  `    ``{  ` `        ``// handles all combinations of 01  ` `        ``if` `(v.get(i) == ``0` `&& v.get(j) == ``1``)  ` `            ``sum += ``1``;  ` ` `  `        ``// handles all combinations of 11  ` `        ``if` `(v.get(i) == ``1` `&& v.get(j) == ``1``)  ` `            ``sum += ``3``;  ` ` `  `        ``// handles all combinations of 10  ` `        ``if` `(v.get(i) == ``1` `&& v.get(j) == ``0``)  ` `            ``sum += ``2``;  ` `    ``}  ` `} ` ` `  `return` `sum;  ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `N = ``5``;  ` ` `  `    ``System.out.print(sumOfPairs(N)); ` `} ` `} ` ` `  `// This code is contributed by Kirti_Mangal `

## Python 3

 `# Python3 program to find the sum ` ` `  `# Function to find the sum  ` `def` `sumofPairs(n) : ` ` `  `    ``# Store the Binary equivalent of decimal  ` `    ``# number in reverse order  ` `    ``v ``=` `[] ` `    ``sum` `=` `0` ` `  `    ``# Calculate binary equivalent of decimal number ` `    ``while` `n > ``0` `: ` `        ``v.append(n ``%` `2``) ` `        ``n ``=` `n ``/``/` `2` ` `  `    ``# for correct binary representation  ` `    ``v.reverse() ` ` `  `    ``# Consider every pair  ` `    ``for` `i ``in` `range``(``len``(v) ``-` `1``) : ` ` `  `        ``for` `j ``in` `range``(i ``+` `1``, ``len``(v)) : ` ` `  `            ``# handles all combinations of 01  ` `            ``if` `v[i] ``=``=` `0` `and` `v[j] ``=``=` `1` `: ` `                ``sum` `+``=` `1` ` `  `            ``#  handles all combinations of 11 ` `            ``if` `v[i] ``=``=` `1` `and` `v[j] ``=``=` `1` `: ` `                ``sum` `+``=` `3` ` `  `            ``# handles all combinations of 10  ` `            ``if` `v[i] ``=``=` `1` `and` `v[j] ``=``=` `0` `: ` `                ``sum` `+``=` `2` ` `  `    ``return` `sum` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``N ``=` `5` ` `  `    ``# function calling ` `    ``print``(sumofPairs(N)) ` ` `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# implementation of above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{  ` `    ``public` `static` `int` `sumOfPairs(``int` `n)  ` `    ``{  ` `        ``// Store the Binary equivalent  ` `        ``// of decimal number in reverse order  ` `        ``List<``int``> v = ``new` `List<``int``>();  ` `        ``int` `sum = 0;  ` ` `  `        ``// Calculate binary equivalent  ` `        ``// of decimal number  ` `        ``while` `(n > 0)  ` `        ``{  ` `            ``v.Add(n % 2);  ` `            ``n = n / 2;  ` `        ``}  ` ` `  `    ``v.Reverse();  ` ` `  `    ``for` `(``int` `i = 0; i < v.Count - 1; i++)  ` `    ``{  ` `        ``for` `(``int` `j = i + 1; j < v.Count; j++)  ` ` `  `        ``{  ` `            ``// handles all combinations of 01  ` `            ``if` `(v[i] == 0 && v[j] == 1)  ` `                ``sum += 1;  ` ` `  `            ``// handles all combinations of 11  ` `            ``if` `(v[i] == 1 && v[j] == 1)  ` `                ``sum += 3;  ` ` `  `            ``// handles all combinations of 10  ` `            ``if` `(v[i] == 1 && v[j] == 0)  ` `                ``sum += 2;  ` `        ``}  ` `    ``}  ` ` `  `    ``return` `sum;  ` `    ``}  ` ` `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main (String[] args)  ` `    ``{  ` `        ``int` `N = 5;  ` ` `  `        ``Console.WriteLine(sumOfPairs(N));  ` `    ``}  ` `}  ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```6
```

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.

Article Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.