Number of integers with odd number of set bits

Given a number n, count number of integers smaller than or equal to n that have odd number of set bits.
Examples:
Input : 5 Output : 3 Explanation : Integers with odd number of set bits in range 1 to 5 : 0 contains 0 set bits 1 contains 1 set bits 2 contains 1 set bits 3 contains 2 set bits 4 contains 1 set bits 5 contains 2 set bits Input : 10 Output : 5 Explanation : Integers with odd set bits are 1, 2, 4, 7 and 8.
Prerequisites: Count number of set bits
The idea is based on below fact.
If n is odd then there are total n+1 integers smaller than or equal to n (0, 1, 2 … n) and half of these integers contain odd number of set bits.
How to handle case when n is even? We know result for n-1. We count set bits in n and add 1 to n/2 if the count is odd. Else we return n/2.
C++
// CPP code to find numbers with// odd number of set bits#include <bits/stdc++.h>using namespace std;// function that returns the number// of integers with odd number of// set bitsint countWithOddSetBits(int n){ // If n is odd, then half of the // integers in (0, 1, .. n) contain // odd number of set bits. if (n % 2 != 0) return (n + 1) / 2; // If n is even, we know result for // n-1. We explicitly compute set bit // count in n. int count = __builtin_popcount(n); int ans = n / 2; if (count % 2 != 0) ans++; return ans;}// Driver codeint main(){ int n = 10; cout << countWithOddSetBits(n); return 0;} |
C
// C code to find numbers with// odd number of set bits#include <stdio.h>// function that returns the number// of integers with odd number of// set bitsint countWithOddSetBits(int n){ // If n is odd, then half of the // integers in (0, 1, .. n) contain // odd number of set bits. if (n % 2 != 0) return (n + 1) / 2; // If n is even, we know result for // n-1. We explicitly compute set bit // count in n. int count = __builtin_popcount(n); int ans = n / 2; if (count % 2 != 0) ans++; return ans;}// Driver codeint main(){ int n = 10; printf("%d",countWithOddSetBits(n)); return 0;}// This code is contributed by kothavvsaaash. |
Java
// Java code to find numbers // with odd number of set bitsimport java.io.*;class GFG { // function that returns the// number of integers with // odd number of set bitsstatic int countWithOddSetBits(int n){ // If n is odd, then half // of the integers in // (0, 1, .. n) contain // odd number of set bits. if (n % 2 != 0) return (n + 1) / 2; // If n is even, we know // result for n-1. We // explicitly compute set // bit count in n. int count = (n); int ans = n / 2; if (count % 2 != 0) ans++; return ans;}// Driver Codepublic static void main (String[] args) { int n = 10; System.out.println( countWithOddSetBits(n));}}// This code is contributed by aj_36 |
Python3
# Python 3 code to find numbers with# odd number of set bits# function that returns the number# of integers with odd number of# set bitsdef countWithOddSetBits(n): # If n is odd, then half of the # integers in (0, 1, .. n) contain # odd number of set bits. if (n % 2 != 0): return (n + 1) / 2 # If n is even, we know result for # n-1. We explicitly compute set # bit count in n. count = bin(n).count('1') ans = n / 2 if (count % 2 != 0): ans += 1 return ans# Driver codeif __name__ == '__main__': n = 10 print(int(countWithOddSetBits(n)))# This code is contributed by# Surendra_Gangwar |
C#
// C# code to find numbers // with odd number of set bitsusing System;class GFG{ // function that returns the// number of integers with // odd number of set bitsstatic int countWithOddSetBits(int n){ // If n is odd, then half // of the integers in // (0, 1, .. n) contain // odd number of set bits. if (n % 2 != 0) return (n + 1) / 2; // If n is even, we know // result for n-1. We // explicitly compute set // bit count in n. int count = (n); int ans = n / 2; if (count % 2 != 0) ans++; return ans;}// Driver Codestatic public void Main (){ int n = 10; Console.WriteLine(countWithOddSetBits(n));}}// This code is contributed by ajit |
PHP
<?php// PHP code to find numbers with// odd number of set bits// function that returns the number// of integers with odd number of// set bitsfunction countWithOddSetBits($n){ // If n is odd, then half of // the integers in (0, 1, .. n) // contain odd number of set bits. if ($n % 2 != 0) return ($n + 1) / 2; // If n is even, we know result // for n-1. We explicitly compute // set bit count in n. $count = ($n); $ans = $n / 2; if ($count % 2 != 0) $ans++; return $ans;}// Driver code$n = 10;echo countWithOddSetBits($n);// This code is contributed by aj_36?> |
Javascript
<script> // Javascript code to find numbers // with odd number of set bits // function that returns the // number of integers with // odd number of set bits function countWithOddSetBits(n) { // If n is odd, then half // of the integers in // (0, 1, .. n) contain // odd number of set bits. if (n % 2 != 0) return parseInt((n + 1) / 2, 10); // If n is even, we know // result for n-1. We // explicitly compute set // bit count in n. let count = (n); let ans = parseInt(n / 2, 10); if (count % 2 != 0) ans++; return ans; } let n = 10; document.write(countWithOddSetBits(n)); </script> |
Output :
5
Time complexity: O(k), where k is the max number of bits in a number.
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



