Count Integers Whose OR with x Is Greater Than x

Last Updated : 17 Aug, 2026

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.

Try It Yourself
redirect icon

[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.
C++
#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;
}
Java
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));
    }
}
Python
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))
C#
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));
    }
}
JavaScript
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).
C++
#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;
}
Java
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));
    }
}
Python
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))
C#
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));
    }
}
JavaScript
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
Comment