# Largest number with binary representation is m 1’s and m-1 0’s

Given n, find the greatest number which is strictly not more then n and whose binary representation consists of m consecutive ones, then m-1 consecutive zeros and nothing else

**Examples:**

Input : n = 7 Output : 6 Explanation: 6's binary representation is 110, and 7's is 111, so 6 consists of 2 consecutive 1's and then 1 consecutive 0. Input : 130 Output : 120 Explanation: 28 and 120 are the only numbers <=120, 28 is 11100 consists of 3 consecutive 1's and then 2 consecutive 0's. 120 is 1111000 consists of 4 consecutive 1's and then 3 consecutive 0's. So 120 is the greatest of number<=120 which meets the given condition.

A **naive approach** will be to traverse from 1 to N and check for every binary representation which consists of m consecutive 1’s and m-1 consecutive 0’s and store the largest of them which meets the given condition.

An **efficient approach** is to observe a pattern of numbers,

[**1(1), 6(110), 28(11100), 120(1111000), 496(111110000), ….] **

To get the formula for the numbers which satisfies the conditions we take 120 as an example-

120 is represented as 1111000 which has m = 4 1’s and m = 3 0’s. Converting 1111000 to decimal we get:

2^3+2^4+2^5+2^6 which can be represented as (2^m-1 + 2^m+ 2^m+1 + … 2^m+2, 2^2*m)

2^3*(1+2+2^2+2^3) which can be represented as (2^(m-1)*(1+2+2^2+2^3+..2^(m-1))

2^3*(2^4-1) which can be represented as **[2^(m-1) * (2^m -1)].**

So all the numbers that meet the given condition can be represented as

[2^(m-1) * (2^m -1)]

We can iterate till the number does not exceeds N and print the largest of all possible elements. A closer observation will shows that at **m = 33 it will exceed the 10^18 mark **, so we are calculating the number in unit’s time as **log(32)** is near to constant which is required in calculating the **pow **.

So, the overall complexity will be O(1).

## C++

`// CPP program to find largest number ` `// smaller than equal to n with m set ` `// bits then m-1 0 bits. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns largest number with m set ` `// bits then m-1 0 bits. ` `long` `long` `answer(` `long` `long` `n) ` `{ ` ` ` `// Start with 2 bits. ` ` ` `long` `m = 2; ` ` ` ` ` `// initial answer is 1 ` ` ` `// which meets the given condition ` ` ` `long` `long` `ans = 1; ` ` ` `long` `long` `r = 1; ` ` ` ` ` `// check for all numbers ` ` ` `while` `(r < n) { ` ` ` ` ` `// compute the number ` ` ` `r = (` `int` `)(` `pow` `(2, m) - 1) * (` `pow` `(2, m - 1)); ` ` ` ` ` `// if less then N ` ` ` `if` `(r < n) ` ` ` `ans = r; ` ` ` ` ` `// increment m to get the next number ` ` ` `m++; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// driver code to check the above condition ` `int` `main() ` `{ ` ` ` `long` `long` `n = 7; ` ` ` `cout << answer(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find largest number ` `// smaller than equal to n with m set ` `// bits then m-1 0 bits. ` `public` `class` `GFG { ` ` ` ` ` `// Returns largest number with ` ` ` `// m set bits then m-1 0 bits. ` ` ` `static` `long` `answer(` `long` `n) ` ` ` `{ ` ` ` ` ` `// Start with 2 bits. ` ` ` `long` `m = ` `2` `; ` ` ` ` ` `// initial answer is 1 which ` ` ` `// meets the given condition ` ` ` `long` `ans = ` `1` `; ` ` ` `long` `r = ` `1` `; ` ` ` ` ` `// check for all numbers ` ` ` `while` `(r < n) { ` ` ` ` ` `// compute the number ` ` ` `r = ((` `long` `)Math.pow(` `2` `, m) - ` `1` `) * ` ` ` `((` `long` `)Math.pow(` `2` `, m - ` `1` `)); ` ` ` ` ` `// if less then N ` ` ` `if` `(r < n) ` ` ` `ans = r; ` ` ` ` ` `// increment m to get ` ` ` `// the next number ` ` ` `m++; ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` ` ` `long` `n = ` `7` `; ` ` ` `System.out.println(answer(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find ` `# largest number smaller ` `# than equal to n with m ` `# set bits then m-1 0 bits. ` `import` `math ` ` ` `# Returns largest number ` `# with m set bits then ` `# m-1 0 bits. ` `def` `answer(n): ` ` ` ` ` `# Start with 2 bits. ` ` ` `m ` `=` `2` `; ` ` ` ` ` `# initial answer is ` ` ` `# 1 which meets the ` ` ` `# given condition ` ` ` `ans ` `=` `1` `; ` ` ` `r ` `=` `1` `; ` ` ` ` ` `# check for all numbers ` ` ` `while` `r < n: ` ` ` ` ` `# compute the number ` ` ` `r ` `=` `(` `int` `)((` `pow` `(` `2` `, m) ` `-` `1` `) ` `*` ` ` `(` `pow` `(` `2` `, m ` `-` `1` `))); ` ` ` ` ` `# if less then N ` ` ` `if` `r < n: ` ` ` `ans ` `=` `r; ` ` ` ` ` `# increment m to get ` ` ` `# the next number ` ` ` `m ` `=` `m ` `+` `1` `; ` ` ` `return` `ans; ` ` ` `# Driver Code ` `print` `(answer(` `7` `)); ` ` ` `# This code is contributed by mits. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find largest number ` `// smaller than equal to n with m set ` `// bits then m-1 0 bits. ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Returns largest number with ` `// m set bits then m-1 0 bits. ` `static` `long` `answer(` `long` `n) ` `{ ` ` ` ` ` `// Start with 2 bits. ` ` ` `long` `m = 2; ` ` ` ` ` `// initial answer is 1 which ` ` ` `// meets the given condition ` ` ` `long` `ans = 1; ` ` ` `long` `r = 1; ` ` ` ` ` `// check for all numbers ` ` ` `while` `(r < n) { ` ` ` ` ` `// compute the number ` ` ` `r = ((` `long` `)Math.Pow(2, m) - 1) * ` ` ` `((` `long` `)Math.Pow(2, m - 1)); ` ` ` ` ` `// if less then N ` ` ` `if` `(r < n) ` ` ` `ans = r; ` ` ` ` ` `// increment m to get ` ` ` `// the next number ` ` ` `m++; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `long` `n = 7; ` ` ` `Console.WriteLine(answer(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find largest number ` `// smaller than equal to n with m set ` `// bits then m-1 0 bits. ` ` ` `// Returns largest number with m set ` `// bits then m-1 0 bits. ` `function` `answer( ` `$n` `) ` `{ ` ` ` ` ` `// Start with 2 bits. ` ` ` `$m` `= 2; ` ` ` ` ` `// initial answer is 1 ` ` ` `// which meets the ` ` ` `// given condition ` ` ` `$ans` `= 1; ` ` ` `$r` `= 1; ` ` ` ` ` `// check for all numbers ` ` ` `while` `(` `$r` `< ` `$n` `) ` ` ` `{ ` ` ` ` ` `// compute the number ` ` ` `$r` `= (pow(2, ` `$m` `) - 1) * ` ` ` `(pow(2, ` `$m` `- 1)); ` ` ` ` ` `// if less then N ` ` ` `if` `(` `$r` `< ` `$n` `) ` ` ` `$ans` `= ` `$r` `; ` ` ` ` ` `// increment m to get ` ` ` `// the next number ` ` ` `$m` `++; ` ` ` `} ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 7; ` ` ` `echo` `answer(` `$n` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Binary representation of next number
- Binary representation of a given number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of previous number
- Number of leading zeros in binary representation of a given number
- Find the n-th number whose binary representation is a palindrome
- Prime Number of Set Bits in Binary Representation | Set 2
- Check if binary representation of a number is palindrome
- Prime Number of Set Bits in Binary Representation | Set 1
- Occurrences of a pattern in binary representation of a number
- Check if binary representation of a given number and its complement are anagram
- Check if actual binary representation of a number is palindrome
- Number of mismatching bits in the binary representation of two integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.