Count numbers have all 1s together in binary representation

24 Jun, 2022
Given an integer n, the task is to count the total lucky numbers smaller than or equal to n. A number is said to be lucky if it has all contagious number of 1’s in binary representation from the beginning. For example 1, 3, 7, 15 are lucky numbers, and 2, 5 and 9 are not lucky numbers.Examples:  
Input :n = 7 
Output :3
1, 3 and 7 are lucky numbers

Input :n = 17
Output :4
 
Approach:one approach is that first we find out the binary representation of each number and than check for contagious number of 1’s for each number, but this approach is time consuming and can give tle if the constraints are two large, Efficient approach can be find out by observing the numbers, we can say that every ith lucky number can be found by the formula 2i-1, and by iterating a loop upto number less than equal to n we can find out the total lucky numbers.Below is the implementation of above approach  
CPP
Java
Python
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std; int countLuckyNum(int n){    int count = 0, i = 1;     while (1) {        if (n >= ((1 << i) - 1))            count++;        else            break;        i++;    }     return count;} // Driver codeint main(){    int n = 7;    cout << countLuckyNum(n);    return 0;}
import java.util.*;import java.lang.*;import java.io.*; public class GFG {     // Function to return the count of lucky number    static int countLuckyNum(int n)    {         int count = 0, i = 1;        while (true) {            if (n >= ((1 << i) - 1))                count++;            else                break;            i++;        }        return count;    }     // Driver code    public static void main(String[] args)    {        int n = 7;        System.out.println(countLuckyNum(n));    }}
# python3 code of above problem # function to count the lucky number def countLuckyNum(n):         count, i = 0, 1         while True:        if n>= 2**i-1:            count+= 1        else:            break        i+= 1;    return count     # driver coden = 7 print(countLuckyNum(n))
// C# implementation of the approachusing System; public class GFG {     // Function to return the count of lucky number    static int countLuckyNum(int n)    {         int count = 0, i = 1;        while (true) {            if (n >= ((1 << i) - 1))                count++;            else                break;            i++;        }        return count;    }     // Driver code    public static void Main()    {        int n = 7;        Console.WriteLine(countLuckyNum(n));    }}
<?php// PHP implementation of the approach   // Function to count the lucky numberfunction countLuckyNum($n)  {       $count = 0;  $i = 1;     while(1)  {      if ($n >= ((1 << $i) - 1))        $count += 1;      else        break;      $i += 1;  }  return $count;                    }       // Driver code  $n = 7;echo countLuckyNum($n) ;  ?>
<script>     // Function to return the count of lucky number    function countLuckyNum(n) {         var count = 0, i = 1;        while (true) {            if (n >= ((1 << i) - 1))                count++;            else                break;            i++;        }        return count;    }     // Driver code    var n = 7;    document.write(countLuckyNum(n)); // This code is contributed by aashish1995</script>
output:3
Time Complexity: O(logn)
Auxiliary Space: O(1)
aashish1995
souravmahato348
binary-representation
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
Find total no of collisions taking place between the balls in which initial direction of each ball is given
Count all possible paths from top left to bottom right of a mXn matrix
Program to multiply two matrices
Segment Tree | Set 1 (Sum of given range)
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Merge two sorted arrays with O(1) extra space
Median of two sorted arrays of same size