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++
#include <bits/stdc++.h>
using namespace std;
long long answer( long long n)
{
long m = 2;
long long ans = 1;
long long r = 1;
while (r < n) {
r = ( int )( pow (2, m) - 1) * ( pow (2, m - 1));
if (r < n)
ans = r;
m++;
}
return ans;
}
int main()
{
long long n = 7;
cout << answer(n);
return 0;
}
|
Java
public class GFG {
static long answer( long n)
{
long m = 2 ;
long ans = 1 ;
long r = 1 ;
while (r < n) {
r = (( long )Math.pow( 2 , m) - 1 ) *
(( long )Math.pow( 2 , m - 1 ));
if (r < n)
ans = r;
m++;
}
return ans;
}
public static void main(String args[]) {
long n = 7 ;
System.out.println(answer(n));
}
}
|
Python3
import math
def answer(n):
m = 2 ;
ans = 1 ;
r = 1 ;
while r < n:
r = ( int )(( pow ( 2 , m) - 1 ) *
( pow ( 2 , m - 1 )));
if r < n:
ans = r;
m = m + 1 ;
return ans;
print (answer( 7 ));
|
C#
using System;
class GFG {
static long answer( long n)
{
long m = 2;
long ans = 1;
long r = 1;
while (r < n) {
r = (( long )Math.Pow(2, m) - 1) *
(( long )Math.Pow(2, m - 1));
if (r < n)
ans = r;
m++;
}
return ans;
}
static public void Main ()
{
long n = 7;
Console.WriteLine(answer(n));
}
}
|
PHP
<?php
function answer( $n )
{
$m = 2;
$ans = 1;
$r = 1;
while ( $r < $n )
{
$r = (pow(2, $m ) - 1) *
(pow(2, $m - 1));
if ( $r < $n )
$ans = $r ;
$m ++;
}
return $ans ;
}
$n = 7;
echo answer( $n );
?>
|
Javascript
<script>
function answer(n)
{
let m = 2;
let ans = 1;
let r = 1;
while (r < n) {
r = (Math.pow(2, m) - 1) *
(Math.pow(2, m - 1));
if (r < n)
ans = r;
m++;
}
return ans;
}
let n = 7;
document.write(answer(n));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
long long answer( long long n)
{
int bits_no = 1 + ( int )( log (n)/ log (2));
int m = (bits_no + 1) / 2;
int ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
int main()
{
long long n = 7;
cout << answer(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int answer( int n)
{
int bits_no = 1 + ( int )(Math.log(n)/Math.log( 2 ));
int m = (bits_no + 1 ) / 2 ;
int ans = ( 1 << (m + m - 1 )) - ( 1 << (m - 1 ));
if (ans <= n)
return ans;
m--;
return ( 1 << (m + m - 1 )) - ( 1 << (m - 1 ));
}
public static void main(String[] args)
{
int n = 7 ;
System.out.println(answer(n));
}
}
|
Python3
import math
def answer( n):
bits_no = 1 + int (math.log(n) / math.log( 2 ));
m = int ((bits_no + 1 ) / 2 );
ans = ( 1 << (m + m - 1 )) - ( 1 << (m - 1 ));
if (ans < = n):
return ans;
m - = 1 ;
return ( 1 << (m + m - 1 )) - ( 1 << (m - 1 ));
n = 7 ;
print (answer(n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int answer( int n)
{
int bits_no = 1 + ( int )(Math.Log(n)/Math.Log(2));
int m = (bits_no + 1) / 2;
int ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
public static void Main( string [] args)
{
int n = 7;
Console.WriteLine(answer(n));
}
}
|
Javascript
function answer( n)
{
let bits_no = 1 + Math.floor(Math.log(n)/Math.log(2));
let m = (bits_no + 1) / 2;
let ans = (1 << (m + m - 1)) - (1 << (m - 1));
if (ans <= n)
return ans;
m--;
return (1 << (m + m - 1)) - (1 << (m - 1));
}
let n = 7;
console.log(answer(n));
|
Time Complexity: O(1)
Auxiliary Space: O(1)