Open In App

Find the absolute difference of set bits at even and odd indices of N

Last Updated : 07 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to find the absolute difference of set bits at even and odd indices of number N. (0-based Indexing)

Examples: 

Input: N = 15
Output: 0
Explanation: The binary representation of 15 is 1111. So, it contains 1 on the 1st and 3rd position and it contains 1 on the 0th and 2nd position. Therefore, the difference between even and odd bits is 2 – 2 which is 0.

Input: N = 17
Output: 2
Explanation: The binary representation of 17 is 10001. So, it contains 1 on the 0th and 4th positions. Therefore, the difference between even and odd bits is 2-0 which is 2.

Approach: This can be solved with the following idea:

Form a binary string of a given number N and start iterating from the right end, maintaining the sum of numbers present at even and odd indexes separately. 

Below are the steps involved in the implementation of the code:

  • Calculate the length of the binary representation of the number.
  • Iterating through each bit position, checking if it is even or odd
  • At last, Count the number of set bits accordingly using bitwise operations. 
  • Return the absolute difference between even and odd bits.

Below is the implementation of the code:

C++




// C++ implementation of code
#include <bits/stdc++.h>
using namespace std;
 
// Function to find absolute difference
int diffEvenOddBit(int n)
{
 
    int count1 = 0;
    int count2 = 0;
    int m = n;
    int len = 0;
 
    // Count the length of binary string
    while (m) {
        len++;
        m = m >> 1;
    }
 
    for (int i = 0; i < len; i++) {
 
        // Check for 1 at even index
        if (i % 2 == 0) {
            if (n & 1 << i) {
                count1++;
            }
        }
 
        // Check for 1 at odd index
        else {
            if (n & 1 << i) {
                count2++;
            }
        }
    }
 
    // Difference of even and odd bits
    int diff = count1 - count2;
    return diff;
}
 
// Driver code
int main()
{
 
    int n = 17;
 
    // Function call
    cout << diffEvenOddBit(n) << " ";
    return 0;
}


Java




import java.io.*;
 
public class Main {
 
    // Function to find absolute difference
    static int diffEvenOddBit(int n)
    {
 
        int count1 = 0;
        int count2 = 0;
        int m = n;
        int len = 0;
 
        // Count the length of binary string
        while (m > 0) {
            len++;
            m = m >> 1;
        }
 
        for (int i = 0; i < len; i++) {
 
            // Check for 1 at even index
            if (i % 2 == 0) {
                if ((n & (1 << i)) != 0) {
                    count1++;
                }
            }
 
            // Check for 1 at odd index
            else {
                if ((n & (1 << i)) != 0) {
                    count2++;
                }
            }
        }
 
        // Difference of even and odd bits
        int diff = count1 - count2;
        return diff;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int n = 17;
 
        // Function call
        System.out.print(diffEvenOddBit(n) + " ");
    }
}


Python3




# Function to find absolute difference
def diffEvenOddBit(n):
    count1 = 0
    count2 = 0
    m = n
    len = 0
 
    # Count the length of binary string
    while m:
        len += 1
        m = m >> 1
 
    for i in range(len):
        # Check for 1 at even index
        if i % 2 == 0:
            if n & (1 << i):
                count1 += 1
 
        # Check for 1 at odd index
        else:
            if n & (1 << i):
                count2 += 1
 
    # Difference of even and odd bits
    diff = count1 - count2
    return abs(diff)
 
# Driver code
n = 17
 
# Function call
print(diffEvenOddBit(n))


C#




using System;
class GFG
{
    // Function to find absolute difference
    static int DiffEvenOddBit(int n)
    {
        int count1 = 0;
        int count2 = 0;
        int m = n;
        int len = 0;
        // Count the length of the binary string
        while (m != 0)
        {
            len++;
            m = m >> 1;
        }
        for (int i = 0; i < len; i++)
        {
            // Check for 1 at even index
            if (i % 2 == 0)
            {
                if ((n & (1 << i)) != 0)
                {
                    count1++;
                }
            }
            // Check for 1
            else
            {
                if ((n & (1 << i)) != 0)
                {
                    count2++;
                }
            }
        }
        // Difference even and odd bits
        int diff = count1 - count2;
        return diff;
    }
    static void Main(string[] args)
    {
        int n = 17;
        // Function call
        Console.WriteLine(DiffEvenOddBit(n));
    }
}


Javascript




// Function to find absolute difference
function diffEvenOddBit(n) {
  let count1 = 0;
  let count2 = 0;
  let m = n;
  let len = 0;
 
  // Count the length of binary string
  while (m) {
    len++;
    m = m >> 1;
  }
 
  for (let i = 0; i < len; i++) {
    // Check for 1 at even index
    if (i % 2 === 0) {
      if (n & (1 << i)) {
        count1++;
      }
    }
    // Check for 1 at odd index
    else {
      if (n & (1 << i)) {
        count2++;
      }
    }
  }
 
  // Difference of even and odd bits
  const diff = count1 - count2;
  return diff;
}
 
// Driver code
const n = 17;
 
// Function call
console.log(diffEvenOddBit(n));
 
 
//This code is contributed by Tushar Rokade


Output

2 

Time Complexity: O(logn)
Auxiliary Space: O(1)Related Articles:

Related Articles:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads