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

• Difficulty Level : Medium
• Last Updated : 14 Nov, 2022

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

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 sumint sumOfPairs(int n){     // Store the Binary equivalent of decimal    // number in reverse order    vector 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 codeint main(){    int N = 5;     cout << sumOfPairs(N);     return 0;}

## Java

 // Java implementation of above approachimport 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 Codepublic static void main (String[] args){    int N = 5;     System.out.print(sumOfPairs(N));}} // This code is contributed by Kirti_Mangal

## Python3

 # Python3 program to find the sum # Function to find the sumdef 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 Codeif __name__ == "__main__" :         N = 5     # function calling    print(sumofPairs(N)) # This code is contributed by ANKITRAI1

## C#

 // C# implementation of above approachusing System;using System.Collections.Generic; class GFG{    public static int sumOfPairs(int n)    {        // Store the Binary equivalent        // of decimal number in reverse order        List v = new List();        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 */

## Javascript



Output

6

Time Complexity: O(N2)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up