Largest number with binary representation is m 1's and m-1 0's
Last Updated :
02 Sep, 2024
Given n, find the greatest number which is strictly not more than 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 than 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;
}
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 than 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
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 than 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.
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 than 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.
JavaScript
<script>
// Javascript 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.
let m = 2;
// initial answer is 1 which
// meets the given condition
let ans = 1;
let r = 1;
// check for all numbers
while (r < n) {
// compute the number
r = (Math.pow(2, m) - 1) *
(Math.pow(2, m - 1));
// if less than N
if (r < n)
ans = r;
// increment m to get
// the next number
m++;
}
return ans;
}
// Driver code
let n = 7;
document.write(answer(n));
</script>
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 than 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.
?>
Time Complexity: O(1)
Auxiliary Space: O(1)
Efficient approach:
This problem can be solved with at most two comparisons, by using the following logic:
1. For a number with n bits, the answer with m set bits followed by m - 1 bits can have a total of n bits, ie, m = (n + 1)/2 (as m + m - 1 = n).
2. The result can then be computed as 2 (m + m - 1) - 2 (m - 1).
3. However, if the n - bit number is in the range [2 (m + m - 2), 2 (m + m - 1) - 2 (m - 1) - 1], then m must be adjusted to m - 1.
Below is the implementation of the above approach:
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)
{
// Calculating the number of bits
int bits_no = 1 + (int)(log(n)/log(2));
// if m + m - 1 = bits_no, then m = (bits_no + 1) / 2
int m = (bits_no + 1) / 2;
// A number with m set bits and m - 1 unset bits can be calculated as
// (2 ^ (m + m - 1)) - (2 ^ (m - 1))
int ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
// If the result is too large, adjust m by applying decrement operation
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
// driver code to check the above condition
int main()
{
long long n = 7;
cout << answer(n);
return 0;
}
Java
// Java program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
import java.util.*;
class GFG
{
// Returns largest number with m set
// bits then m-1 0 bits.
static int answer(int n)
{
// Calculating the number of bits
int bits_no = 1 + (int)(Math.log(n)/Math.log(2));
// if m + m - 1 = bits_no, then m = (bits_no + 1) / 2
int m = (bits_no + 1) / 2;
// A number with m set bits and m - 1
// unset bits can be calculated as
// (2 ^ (m + m - 1)) - (2 ^ (m - 1))
int ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
// If the result is too large, adjust m
// by applying decrement operation
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
// driver code to check the above condition
public static void main(String[] args)
{
int n = 7;
System.out.println(answer(n));
}
}
// This code is contributed by phasing17.
Python
# 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):
# Calculating the number of bits
bits_no = 1 + int(math.log(n)/math.log(2));
# if m + m - 1 = bits_no, then m = (bits_no + 1) / 2
m = int((bits_no + 1) / 2);
# A number with m set bits and m - 1 unset bits can be calculated as
# (2 ^ (m + m - 1)) - (2 ^ (m - 1))
ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n):
return ans;
# If the result is too large, adjust m by applying decrement operation
m -= 1;
return (1 << (m + m - 1)) - (1 << (m - 1));
# driver code to check the above condition
n = 7;
print(answer(n));
# This code is contributed by phasing17.
C#
// C# program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
using System;
using System.Collections.Generic;
class GFG
{
// Returns largest number with m set
// bits then m-1 0 bits.
static int answer(int n)
{
// Calculating the number of bits
int bits_no = 1 + (int)(Math.Log(n)/Math.Log(2));
// if m + m - 1 = bits_no, then m = (bits_no + 1) / 2
int m = (bits_no + 1) / 2;
// A number with m set bits and m - 1
// unset bits can be calculated as
// (2 ^ (m + m - 1)) - (2 ^ (m - 1))
int ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
// If the result is too large, adjust m
// by applying decrement operation
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
// driver code to check the above condition
public static void Main(string[] args)
{
int n = 7;
Console.WriteLine(answer(n));
}
}
// This code is contributed by phasing17.
JavaScript
// JS 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)
{
// Calculating the number of bits
let bits_no = 1 + Math.floor(Math.log(n)/Math.log(2));
// if m + m - 1 = bits_no, then m = (bits_no + 1) / 2
let m = (bits_no + 1) / 2;
// A number with m set bits and m - 1 unset bits can be calculated as
// (2 ^ (m + m - 1)) - (2 ^ (m - 1))
let ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
// If the result is too large, adjust m by applying decrement operation
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
// driver code to check the above condition
let n = 7;
console.log(answer(n));
// This code is contributed by phasing17.
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Binary representation of next greater number with same number of 1's and 0's
Given a binary input that represents binary representation of positive number n, find binary representation of smallest number greater than n with same number of 1's and 0's as in binary representation of n. If no such number can be formed, print "no greater number".The binary input may be and may n
12 min read
1 to n bit numbers with no consecutive 1s in binary representation.
Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation. Examples: Input : n = 4 Output : 1 2 4 5 8 9 10 These are numbers with 1 to 4 bits and no consecutive ones in binary representation. Input : n = 3 Output : 1 2 4 5 Recommended: Please
6 min read
Find longest sequence of 1's in binary representation with one flip
Give an integer n. We can flip exactly one bit. Write code to find the length of the longest sequence of 1 s you could create. Examples: Input : 1775 Output : 8 Binary representation of 1775 is 11011101111.After flipping the highlighted bit, we get consecutive 8 bits. 11011111111.Input : 12 Output :
12 min read
Maximum 0's between two immediate 1's in binary representation
Given a number n, the task is to find the maximum 0's between two immediate 1's in binary representation of given n. Return -1 if binary representation contains less than two 1's. Examples : Input : n = 47 Output: 1 // binary of n = 47 is 101111 Input : n = 549 Output: 3 // binary of n = 549 is 1000
9 min read
Find the largest number with n set and m unset bits
Given two non-negative numbers n and m. The problem is to find the largest number having n number of set bits and m number of unset bits in its binary representation.Note : 0 bits before leading 1 (or leftmost 1) in binary representation are countedConstraints: 1 <= n, 0 <= m, (m+n) <= 31Ex
6 min read
Largest area rectangular sub-matrix with equal number of 1's and 0's
Given a binary matrix. The problem is to find the largest area rectangular sub-matrix with equal number of 1's and 0's. Examples: Input : mat[][] = { {0, 0, 1, 1}, {0, 1, 1, 0}, {1, 1, 1, 0}, {1, 0, 0, 1} } Output : 8 sq. units (Top, left): (0, 0) (Bottom, right): (3, 1) Input : mat[][] = { {0, 0, 1
15+ min read
Check if the binary representation of a number has equal number of 0s and 1s in blocks
Given an integer N, the task is to check if its equivalent binary number has an equal frequency of consecutive blocks of 0 and 1. Note that 0 and a number with all 1s are not considered to have a number of blocks of 0s and 1s. Examples: Input: N = 5 Output: Yes Equivalent binary number of 5 is 101.
10 min read
Binary representation of a given number
Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000
6 min read
Longest common substring in binary representation of two numbers
Given two integers n and m. Find the longest contiguous subset in binary representation of both the numbers and their decimal value. Example 1: Input : n = 10, m = 11 Output : 5 Explanation : Binary representation of 10 -> 1010 11 -> 1011 longest common substring in both is 101 and decimal val
7 min read
Print N-bit binary numbers having more 1âs than 0âs in all prefixes
Given a positive integer n, print all n-bit binary numbers having more 1âs than 0âs for any prefix of the number. Examples: Input : n = 2 Output : 11 10 Input : n = 4 Output : 1111 1110 1101 1100 1011 1010Recommended PracticePrint N-bit binary numbers having more 1s than 0sTry It! A simple but not e
12 min read