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| =2Input: 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
// C++ program for // the above approach #include <bits/stdc++.h> using namespace std; Â
// Function to find the // reverse number of N int reverse( int N) {     // Stores the     // reverse of N     int revn = 0; Â
    // Iterate while N exceeds 0     while (N > 0) {         // Extract last digit of N         int b = N % 10; Â
        // Append the last digit         // of N to revn         revn = (revn * 10) + b; Â
        // Remove the last digit of N         N = N / 10;     } Â
    return revn; } Â
// Function to find the absolute difference // between the set bits in N and its reverse int findAbsoluteDiffernce( int N) {     // Store N as bitset     bitset<64> a(N); Â
    // Stores the reverse of N     int revn = reverse(N); Â
    // Stores revn as bitset     bitset<64> b(revn); Â
    // Count set bits in N     int setBitsInN = a.count(); Â
    // Count set bits in revn     int setBitsInRevN = b.count(); Â
    // Return the absolute difference of     // set bits in N and its reverse     return abs (setBitsInN - setBitsInRevN); } Â
// Driver Code int main() {     // Input     int N = 13; Â
    // Function call to find absolute     // difference between the count     // of set bits in N and its reverse     cout << findAbsoluteDiffernce(N); Â
    return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; Â
class GFG{ Â
// Function to find the // reverse number of N static int reverse( int N) {          // Stores the     // reverse of N     int revn = 0 ; Â
    // Iterate while N exceeds 0     while (N > 0 )     {                  // Extract last digit of N         int b = N % 10 ; Â
        // Append the last digit         // of N to revn         revn = (revn * 10 ) + b; Â
        // Remove the last digit of N         N = N / 10 ;     }     return revn; } Â
// Function to find the absolute difference // between the set bits in N and its reverse static int findAbsoluteDiffernce( int N) { Â Â Â Â Â Â Â Â Â // Count set bits in N Â Â Â Â int setBitsInN = Integer.bitCount(N); Â
    // Stores the reverse of N     int revn = reverse(N); Â
    // Count set bits in revn     int setBitsInRevN = Integer.bitCount(revn); Â
    // Return the absolute difference of     // set bits in N and its reverse     return Math.abs(setBitsInN - setBitsInRevN); } Â
// Driver Code public static void main(String[] args) {          // Input     int N = 13 ; Â
    // Function call to find absolute     // difference between the count     // of set bits in N and its reverse     System.out.println(findAbsoluteDiffernce(N)); } } Â
// This code is contributed by Kingash |
Python3
# Python3 program for # the above approach Â
# Function to find the # reverse number of N def reverse(N):          # Stores the     # reverse of N     revn = 0 Â
    # Iterate while N exceeds 0     while (N > 0 ):                  # Extract last digit of N         b = N % 10 Â
        # Append the last digit         # of N to revn         revn = (revn * 10 ) + b Â
        # Remove the last digit of N         N = N / / 10 Â
    return revn Â
def countSetBits(n):          count = 0          while n:         count + = (n & 1 )         n >> = 1              return count    # Function to find the absolute difference # between the set bits in N and its reverse def findAbsoluteDiffernce(N):          # Count set bits in N     setBitsInN = countSetBits(N) Â
    # Stores the reverse of N     revn = reverse(N) Â
    # Count set bits in revn     setBitsInRevN = countSetBits(revn) Â
    # Return the absolute difference of     # set bits in N and its reverse     return abs (setBitsInN - setBitsInRevN) Â
# Driver Code Â
# Input N = 13 Â
# Function call to find absolute # difference between the count # of set bits in N and its reverse print (findAbsoluteDiffernce(N)) Â
# This code is contributed by rohitsingh07052 |
C#
// C# program for the above approach using System; Â
class GFG{ Â
// Function to find the // reverse number of N static int reverse( int N) {          // Stores the     // reverse of N     int revn = 0; Â
    // Iterate while N exceeds 0     while (N > 0)     {                  // Extract last digit of N         int b = N % 10; Â
        // Append the last digit         // of N to revn         revn = (revn * 10) + b; Â
        // Remove the last digit of N         N = N / 10;     }     return revn; } Â
// Function to get no of set // bits in binary representation // of positive integer n static int countSetBits( int n) { Â Â Â Â int count = 0; Â Â Â Â Â Â Â Â Â while (n > 0) Â Â Â Â { Â Â Â Â Â Â Â Â count += n & 1; Â Â Â Â Â Â Â Â n >>= 1; Â Â Â Â } Â Â Â Â return count; } Â
// Function to find the absolute difference // between the set bits in N and its reverse static int findAbsoluteDiffernce( int N) { Â Â Â Â Â Â Â Â Â // Count set bits in N Â Â Â Â int setBitsInN = countSetBits(N); Â
    // Stores the reverse of N     int revn = reverse(N); Â
    // Count set bits in revn     int setBitsInRevN = countSetBits(revn); Â
    // Return the absolute difference of     // set bits in N and its reverse     return Math.Abs(setBitsInN - setBitsInRevN); } Â
// Driver Code public static void Main( string [] args) {          // Input     int N = 13; Â
    // Function call to find absolute     // difference between the count     // of set bits in N and its reverse     Console.WriteLine(findAbsoluteDiffernce(N)); } } Â
// This code is contributed by AnkThon |
Javascript
<script> Â
        // Javascript program for the         // above approach Â
        // Function to find the         // reverse number of N         function reverse( n ) {         // converting the number to String             n = n + "" ; Â
       // splitting the digits into an array             let arr = n.split( "" ) Â
      // reversing the array             let revn = arr.reverse() Â
            //joining all in a single string             return revn.join( "" );         } Â
        // Function to find         // the absolute difference         // between the set bits in N         // and its reverse         function findAbsoluteDiffernce(N) { Â
            // Count set bits in N             let setBitsInN =             N.toString(2).split( '1' ).length - 1                          // Stores the reverse of N             let revn = reverse(N);                          // Count set bits in revn             let setBitsInRevN =             revn.toString(2).split( '1' ).length - 1                          // Return the absolute difference of             // set bits in N and its reverse             return Math.abs(setBitsInN - setBitsInRevN);         } Â
        // Driver Code Â
        // Input         let N = 13; Â
        // Function call to find absolute         // difference between the count         // of set bits in N and its reverse         document.write(findAbsoluteDiffernce(N)) Â
        // This code is contributed by Hritik              </script> |
2
Â
Time Complexity: O(log N)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!