Find the absolute difference of set bits at even and odd indices of N
Last Updated :
07 Jul, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int diffEvenOddBit( int n)
{
int count1 = 0;
int count2 = 0;
int m = n;
int len = 0;
while (m) {
len++;
m = m >> 1;
}
for ( int i = 0; i < len; i++) {
if (i % 2 == 0) {
if (n & 1 << i) {
count1++;
}
}
else {
if (n & 1 << i) {
count2++;
}
}
}
int diff = count1 - count2;
return diff;
}
int main()
{
int n = 17;
cout << diffEvenOddBit(n) << " " ;
return 0;
}
|
Java
import java.io.*;
public class Main {
static int diffEvenOddBit( int n)
{
int count1 = 0 ;
int count2 = 0 ;
int m = n;
int len = 0 ;
while (m > 0 ) {
len++;
m = m >> 1 ;
}
for ( int i = 0 ; i < len; i++) {
if (i % 2 == 0 ) {
if ((n & ( 1 << i)) != 0 ) {
count1++;
}
}
else {
if ((n & ( 1 << i)) != 0 ) {
count2++;
}
}
}
int diff = count1 - count2;
return diff;
}
public static void main(String[] args)
{
int n = 17 ;
System.out.print(diffEvenOddBit(n) + " " );
}
}
|
Python3
def diffEvenOddBit(n):
count1 = 0
count2 = 0
m = n
len = 0
while m:
len + = 1
m = m >> 1
for i in range ( len ):
if i % 2 = = 0 :
if n & ( 1 << i):
count1 + = 1
else :
if n & ( 1 << i):
count2 + = 1
diff = count1 - count2
return abs (diff)
n = 17
print (diffEvenOddBit(n))
|
C#
using System;
class GFG
{
static int DiffEvenOddBit( int n)
{
int count1 = 0;
int count2 = 0;
int m = n;
int len = 0;
while (m != 0)
{
len++;
m = m >> 1;
}
for ( int i = 0; i < len; i++)
{
if (i % 2 == 0)
{
if ((n & (1 << i)) != 0)
{
count1++;
}
}
else
{
if ((n & (1 << i)) != 0)
{
count2++;
}
}
}
int diff = count1 - count2;
return diff;
}
static void Main( string [] args)
{
int n = 17;
Console.WriteLine(DiffEvenOddBit(n));
}
}
|
Javascript
function diffEvenOddBit(n) {
let count1 = 0;
let count2 = 0;
let m = n;
let len = 0;
while (m) {
len++;
m = m >> 1;
}
for (let i = 0; i < len; i++) {
if (i % 2 === 0) {
if (n & (1 << i)) {
count1++;
}
}
else {
if (n & (1 << i)) {
count2++;
}
}
}
const diff = count1 - count2;
return diff;
}
const n = 17;
console.log(diffEvenOddBit(n));
|
Time Complexity: O(logn)
Auxiliary Space: O(1)Related Articles:
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...