Absolute difference between the count of set bits in N and its reverse
Given an integer N, the task is to find the absolute difference between the number of set bits present in the number N and in reverse of the number N.
Examples:
Input: N = 13
Output: 2
Explanation:
Binary representation of (13)10 = (1101)2
Count of set bits = 3
Reverse of 13 is 31
Binary representation of (31)10 = (11111)2
Count of set bits of reversed number = 5
Absolute Difference is |3 – 5| =2
Input: N = 135
Output: 0
Explanation:
Binary representation of (135)10 = (10000111)2
Count of set bits =4
Reverse of 135 is 531
Binary representation of (531)10 = (1000010011)2
Count of set bits of reversed number = 4
Absolute Difference is |4 – 4| = 0
Approach: The main idea is to use the bitset function of the STL library.
Follow the steps below to solve the given problem:
- Reverse the digits of the number N and store it in a variable, say revN.
- Use the bitset function to count the number of set bits in N.
- Return the absolute difference of the number of set bits in N and revN.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int reverse( int N)
{
int revn = 0;
while (N > 0) {
int b = N % 10;
revn = (revn * 10) + b;
N = N / 10;
}
return revn;
}
int findAbsoluteDiffernce( int N)
{
bitset<64> a(N);
int revn = reverse(N);
bitset<64> b(revn);
int setBitsInN = a.count();
int setBitsInRevN = b.count();
return abs (setBitsInN - setBitsInRevN);
}
int main()
{
int N = 13;
cout << findAbsoluteDiffernce(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int reverse( int N)
{
int revn = 0 ;
while (N > 0 )
{
int b = N % 10 ;
revn = (revn * 10 ) + b;
N = N / 10 ;
}
return revn;
}
static int findAbsoluteDiffernce( int N)
{
int setBitsInN = Integer.bitCount(N);
int revn = reverse(N);
int setBitsInRevN = Integer.bitCount(revn);
return Math.abs(setBitsInN - setBitsInRevN);
}
public static void main(String[] args)
{
int N = 13 ;
System.out.println(findAbsoluteDiffernce(N));
}
}
|
Python3
def reverse(N):
revn = 0
while (N > 0 ):
b = N % 10
revn = (revn * 10 ) + b
N = N / / 10
return revn
def countSetBits(n):
count = 0
while n:
count + = (n & 1 )
n >> = 1
return count
def findAbsoluteDiffernce(N):
setBitsInN = countSetBits(N)
revn = reverse(N)
setBitsInRevN = countSetBits(revn)
return abs (setBitsInN - setBitsInRevN)
N = 13
print (findAbsoluteDiffernce(N))
|
C#
using System;
class GFG{
static int reverse( int N)
{
int revn = 0;
while (N > 0)
{
int b = N % 10;
revn = (revn * 10) + b;
N = N / 10;
}
return revn;
}
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
static int findAbsoluteDiffernce( int N)
{
int setBitsInN = countSetBits(N);
int revn = reverse(N);
int setBitsInRevN = countSetBits(revn);
return Math.Abs(setBitsInN - setBitsInRevN);
}
public static void Main( string [] args)
{
int N = 13;
Console.WriteLine(findAbsoluteDiffernce(N));
}
}
|
Javascript
<script>
function reverse( n ) {
n = n + "" ;
let arr = n.split( "" )
let revn = arr.reverse()
return revn.join( "" );
}
function findAbsoluteDiffernce(N) {
let setBitsInN =
N.toString(2).split( '1' ).length - 1
let revn = reverse(N);
let setBitsInRevN =
revn.toString(2).split( '1' ).length - 1
return Math.abs(setBitsInN - setBitsInRevN);
}
let N = 13;
document.write(findAbsoluteDiffernce(N))
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
22 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...