Given an integer x, count the number of integers a such that 1 âĪ a âĪ x and (a OR x) > x.
Examples:
Input: x = 10
Output:Â 7Â
Explanation: In the range [1, 10] if the OR is taken of 10 and any number in the set [1, 3, 4, 5, 6, 7, 9] the resulting value will be greater than 10.Input: x = 5
Output:Â 2
Explanation: In the range [1, 5] if the OR is taken of 5 and any number in the set [2, 3] the resulting value will be greater than 5.
Table of Content
[Naive Approach] Check Every Number - O(x) Time and O(1) Space
For every number a from 1 to x, directly calculate (a | x). If the result is greater than x, then a satisfies the condition.
- Initialize count = 0.
- Iterate a from 1 to x.
- Calculate (a | x).
- If (a | x) > x, increment count.
- Return count.
#include <bits/stdc++.h>
using namespace std;
int getCount(int x)
{
int count = 0;
// Try every possible value of a from 1 to x
for (int a = 1; a <= x; a++)
{
// If OR of a and x is greater than x,
// then a satisfies the required condition
if ((a | x) > x)
{
count++;
}
}
return count;
}
int main()
{
int x = 10;
cout << getCount(x) << endl;
return 0;
}
import java.util.*;
class GFG {
static int getCount(int x)
{
int count = 0;
// Try every possible value of a from 1 to x
for (int a = 1; a <= x; a++) {
// If OR of a and x is greater than x,
// then a satisfies the required condition
if ((a | x) > x) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int x = 10;
System.out.println(getCount(x));
}
}
def getCount(x):
count = 0
# Try every possible value of a from 1 to x
for a in range(1, x + 1):
# If OR of a and x is greater than x,
# then a satisfies the required condition
if (a | x) > x:
count += 1
return count
# Driver Code
if __name__ == "__main__":
x = 10
print(getCount(x))
using System;
class GFG {
static int getCount(int x)
{
int count = 0;
// Try every possible value of a from 1 to x
for (int a = 1; a <= x; a++) {
// If OR of a and x is greater than x,
// then a satisfies the required condition
if ((a | x) > x) {
count++;
}
}
return count;
}
static void Main()
{
int x = 10;
Console.WriteLine(getCount(x));
}
}
function getCount(x)
{
let count = 0;
// Try every possible value of a from 1 to x
for (let a = 1; a <= x; a++) {
// If OR of a and x is greater than x,
// then a satisfies the required condition
if ((a | x) > x) {
count++;
}
}
return count;
}
// Driver Code
let x = 10;
console.log(getCount(x));
Output
7
[Expected Approach] Count Set Bits - O(log(x)) Time and O(1) Space
We count the numbers for which (a | x) == x. For OR to remain equal to x, every set bit of a must already be set in x.
If x has k set bits, there are 2^k possible subsets of those set bits, including a = 0. Since a must be at least 1, there are 2^k - 1 valid values where the OR is not greater than x. Therefore, the remaining values satisfy the required condition.
- Initialize setBits = 0.
- Count the set bits of x.
- If x has k set bits, calculate 2^k - 1.
- Subtract this from the total x numbers in [1, x].
- Return x - ((1 << k) - 1).
#include <iostream>
using namespace std;
int getCount(int x)
{
int setBits = 0;
int temp = x;
// Count the number of set bits in x
while (temp > 0)
{
setBits++;
temp &= (temp - 1);
}
// Numbers for which (a | x) == x are:
// 2^setBits - 1
return x - ((1 << setBits) - 1);
}
int main()
{
int x = 10;
cout << getCount(x) << endl;
}
import java.util.*;
class GFG {
static int getCount(int x)
{
int setBits = 0;
int temp = x;
// Count the number of set bits in x
while (temp > 0) {
setBits++;
// Remove the rightmost set bit
temp &= (temp - 1);
}
// Numbers for which (a | x) == x are:
// 2^setBits - 1
return x - ((1 << setBits) - 1);
}
public static void main(String[] args)
{
int x = 10;
System.out.println(getCount(x));
}
}
def getCount(x):
setBits = 0
temp = x
# Count the number of set bits in x
while temp > 0:
setBits += 1
# Remove the rightmost set bit
temp &= (temp - 1)
# Numbers for which (a | x) == x are:
# 2^setBits - 1
return x - ((1 << setBits) - 1)
# Driver Code
if __name__ == "__main__":
x = 10
print(getCount(x))
using System;
class GFG {
static int getCount(int x)
{
int setBits = 0;
int temp = x;
// Count the number of set bits in x
while (temp > 0) {
setBits++;
// Remove the rightmost set bit
temp &= (temp - 1);
}
// Numbers for which (a | x) == x are:
// 2^setBits - 1
return x - ((1 << setBits) - 1);
}
static void Main()
{
int x = 10;
Console.WriteLine(getCount(x));
}
}
function getCount(x)
{
let setBits = 0;
let temp = x;
// Count the number of set bits in x
while (temp > 0) {
setBits++;
// Remove the rightmost set bit
temp &= (temp - 1);
}
// Numbers for which (a | x) == x are:
// 2^setBits - 1
return x - ((1 << setBits) - 1);
}
// Driver Code
const x = 10;
console.log(getCount(x));
Output
7