Given a non-negative integer n, update its binary representation by setting all bits at even indices to 0 while leaving all bits at odd indices unchanged.
Note: Indices are 0-based, counted from the rightmost bit.
Examples:Â
Input: n = 30
Output: 10
Explanation: The binary representation of 30 is 11110. Considering 0-based indices from the right, the bits at even indices (0, 2, and 4) are set to 0, resulting in 01010. The decimal value of 01010 is 10.
Input: n = 10
Output: 10Â
Explanation: The binary representation of 10 is 1010. Considering 0-based indices from the right, the bits at even indices (0 and 2) are already 0. Therefore, the binary representation remains 1010, and the resulting value is 10.
Table of Content
[Naive Approach] Binary Conversion and Reconstruction - O(log n) Time and O(log n) Space
The idea is to convert the number into its binary representation and examine each bit position from right to left. If a bit is at an even index (0, 2, 4, ...), set it to
0; otherwise, keep it unchanged. Finally, reconstruct the number from the modified binary representation.
#include <iostream>
#include <vector>
using namespace std;
int makeZero(int n)
{
vector<int> bits;
// Store binary representation
while (n > 0)
{
bits.push_back(n % 2);
n /= 2;
}
// Set bits at even indices to 0
for (int i = 0; i < bits.size(); i++)
{
if (i % 2 == 0)
bits[i] = 0;
}
// Reconstruct the number
int res = 0;
int power = 1;
for (int i = 0; i < bits.size(); i++)
{
res += bits[i] * power;
power *= 2;
}
return res;
}
int main()
{
int n = 30;
cout << makeZero(n);
return 0;
}
import java.util.ArrayList;
public class GFG {
public static int makeZero(int n)
{
ArrayList<Integer> bits = new ArrayList<>();
// Store binary representation
while (n > 0) {
bits.add(n % 2);
n /= 2;
}
// Set bits at even indices to 0
for (int i = 0; i < bits.size(); i++) {
if (i % 2 == 0)
bits.set(i, 0);
}
// Reconstruct the number
int res = 0;
int power = 1;
for (int i = 0; i < bits.size(); i++) {
res += bits.get(i) * power;
power *= 2;
}
return res;
}
public static void main(String[] args)
{
int n = 30;
System.out.println(makeZero(n));
}
}
def makeZero(n):
bits = []
# Store binary representation
while n > 0:
bits.append(n % 2)
n //= 2
# Set bits at even indices to 0
for i in range(len(bits)):
if i % 2 == 0:
bits[i] = 0
# Reconstruct the number
res = 0
power = 1
for i in range(len(bits)):
res += bits[i] * power
power *= 2
return res
if __name__ == "__main__":
n = 30
print(makeZero(n))
using System;
using System.Collections.Generic;
public class GFG {
public static int makeZero(int n)
{
List<int> bits = new List<int>();
// Store binary representation
while (n > 0) {
bits.Add(n % 2);
n /= 2;
}
// Set bits at even indices to 0
for (int i = 0; i < bits.Count; i++) {
if (i % 2 == 0)
bits[i] = 0;
}
// Reconstruct the number
int res = 0;
int power = 1;
for (int i = 0; i < bits.Count; i++) {
res += bits[i] * power;
power *= 2;
}
return res;
}
public static void Main()
{
int n = 30;
Console.WriteLine(makeZero(n));
}
}
function makeZero(n)
{
let bits = [];
// Store binary representation
while (n > 0) {
bits.push(n % 2);
n = Math.floor(n / 2);
}
// Set bits at even indices to 0
for (let i = 0; i < bits.length; i++) {
if (i % 2 === 0)
bits[i] = 0;
}
// Reconstruct the number
let res = 0;
let power = 1;
for (let i = 0; i < bits.length; i++) {
res += bits[i] * power;
power *= 2;
}
return res;
}
// Driver code
let n = 30;
console.log(makeZero(n));
Output
10
[Expected Approach] Bit Masking - O(1) Time and O(1) Space
The idea is to use a bit mask that has 1s at all odd indices and 0s at all even indices. Performing a bitwise AND operation of the given number with this mask preserves the bits at odd indices and clears the bits at even indices. Thus, all even-indexed bits become 0 in a single operation.
Let us understand with an example:
Input: n = 30
- Consider n = 30, whose binary representation is 00011110.
- The mask 0xAAAAAAAA has binary pattern 10101010..., with 1s at odd bit positions and 0s at even bit positions.
- Perform the bitwise AND operation: 00011110 & 10101010 = 00001010.
- The resulting binary 00001010 is equal to 10 in decimal.
- Hence, the function returns 10, where all bits at even positions have been cleared.
#include <iostream>
#include <vector>
using namespace std;
int makeZero(int n)
{
// 1s at odd positions, 0s at even positions
int mask = 0xAAAAAAAAu;
return n & mask;
}
// Driver code
int main()
{
int n = 30;
cout << makeZero(n);
return 0;
}
public class GFG {
// 1s at odd positions, 0s at even positions
static int makeZero(int n)
{
int mask = 0xAAAAAAAA;
return n & mask;
}
public static void main(String[] args)
{
int n = 30;
System.out.println(makeZero(n));
}
}
def makeZero(n):
# 1s at odd positions, 0s at even positions
mask = 0xAAAAAAAA
return n & mask
if __name__ == "__main__":
n = 30
print(makeZero(n))
using System;
class GFG {
static int makeZero(int n)
{
// 1s at odd positions, 0s at even positions
int mask = unchecked((int)0xAAAAAAAA);
return n & mask;
}
// Driver code
static void Main(string[] args)
{
int n = 30;
Console.WriteLine(makeZero(n));
}
}
function makeZero(n)
{
// 1s at odd positions, 0s at even positions
const mask = 0xAAAAAAAA;
return n & mask;
}
// Driver Code
const n = 30;
console.log(makeZero(n));
Output
10