Sum of decimal equivalent of all possible pairs of Binary representation of a Number
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:
- Find the binary equivalent of N and store it in a vector.
- Run two loops to consider each and every pair formed from the bits of binary equivalent stored in the vector.
- Find the decimal equivalent of all the pairs and add them.
- Return the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfPairs( int n)
{
vector< int > v;
int sum = 0;
while (n > 0) {
v.push_back(n % 2);
n = n / 2;
}
reverse(v.begin(), v.end());
for ( int i = 0; i < v.size() - 1; i++) {
for ( int j = i + 1; j < v.size(); j++)
{
if (v[i] == 0 && v[j] == 1)
sum += 1;
if (v[i] == 1 && v[j] == 1)
sum += 3;
if (v[i] == 1 && v[j] == 0)
sum += 2;
}
}
return sum;
}
int main()
{
int N = 5;
cout << sumOfPairs(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int sumOfPairs( int n)
{
ArrayList<Integer> v = new ArrayList<Integer>();
int sum = 0 ;
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++)
{
if (v.get(i) == 0 && v.get(j) == 1 )
sum += 1 ;
if (v.get(i) == 1 && v.get(j) == 1 )
sum += 3 ;
if (v.get(i) == 1 && v.get(j) == 0 )
sum += 2 ;
}
}
return sum;
}
public static void main (String[] args)
{
int N = 5 ;
System.out.print(sumOfPairs(N));
}
}
|
Python3
def sumofPairs(n) :
v = []
sum = 0
while n > 0 :
v.append(n % 2 )
n = n / / 2
v.reverse()
for i in range ( len (v) - 1 ) :
for j in range (i + 1 , len (v)) :
if v[i] = = 0 and v[j] = = 1 :
sum + = 1
if v[i] = = 1 and v[j] = = 1 :
sum + = 3
if v[i] = = 1 and v[j] = = 0 :
sum + = 2
return sum
if __name__ = = "__main__" :
N = 5
print (sumofPairs(N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int sumOfPairs( int n)
{
List< int > v = new List< int >();
int sum = 0;
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++)
{
if (v[i] == 0 && v[j] == 1)
sum += 1;
if (v[i] == 1 && v[j] == 1)
sum += 3;
if (v[i] == 1 && v[j] == 0)
sum += 2;
}
}
return sum;
}
public static void Main (String[] args)
{
int N = 5;
Console.WriteLine(sumOfPairs(N));
}
}
|
Javascript
<script>
function sumOfPairs(n)
{
var v = [];
var sum = 0;
while (n > 0)
{
v.push(n % 2);
n = parseInt(n / 2);
}
v.reverse();
for ( var i = 0; i < v.length - 1; i++)
{
for ( var j = i + 1; j < v.length; j++)
{
if (v[i] == 0 && v[j] == 1)
sum += 1;
if (v[i] == 1 && v[j] == 1)
sum += 3;
if (v[i] == 1 && v[j] == 0)
sum += 2;
}
}
return sum;
}
var N = 5;
document.write(sumOfPairs(N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...