Find next greater element with no consecutive 1 in it’s binary representation
Given Q queries where each query consists of an integer N and the task is to find the smallest integer greater than N such that there are no consecutive 1s in its binary representation.
Examples:
Input: Q[] = {4, 6}
Output:
5
8
Input: Q[] = {50, 23, 456}
Output:
64
32
512
Approach: Store all the numbers in a list whose binary representation does not contain consecutive 1s upto a fixed limit. Now for every given N, find the next greater element in the list generated previously using binary search.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
vector< int > v;
int consecutiveOnes( int x)
{
int p = 0;
while (x > 0) {
if (x % 2 == 1 and p == 1)
return true ;
p = x % 2;
x /= 2;
}
return false ;
}
void preCompute()
{
for ( int i = 0; i <= MAX; i++) {
if (!consecutiveOnes(i))
v.push_back(i);
}
}
int nextValid( int n)
{
int it = upper_bound(v.begin(),
v.end(), n)
- v.begin();
int val = v[it];
return val;
}
void performQueries( int queries[], int q)
{
for ( int i = 0; i < q; i++)
cout << nextValid(queries[i]) << "\n" ;
}
int main()
{
int queries[] = { 4, 6 };
int q = sizeof (queries) / sizeof ( int );
preCompute();
performQueries(queries, q);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int MAX = 100000 ;
static ArrayList<Integer> v = new ArrayList<Integer>();
public static int upper_bound(ArrayList<Integer> ar,
int k)
{
int s = 0 ;
int e = ar.size();
while (s != e)
{
int mid = s + e >> 1 ;
if (ar.get(mid) <= k)
{
s = mid + 1 ;
}
else
{
e = mid;
}
}
if (s == ar.size())
{
return - 1 ;
}
return s;
}
static int consecutiveOnes( int x)
{
int p = 0 ;
while (x > 0 )
{
if (x % 2 == 1 && p == 1 )
{
return 1 ;
}
p = x % 2 ;
x /= 2 ;
}
return 0 ;
}
static void preCompute()
{
for ( int i = 0 ; i <= MAX; i++)
{
if (consecutiveOnes(i) == 0 )
{
v.add(i);
}
}
}
static int nextValid( int n)
{
int it = upper_bound(v,n);
int val = v.get(it);
return val;
}
static void performQueries( int queries[], int q)
{
for ( int i = 0 ; i < q; i++)
{
System.out.println(nextValid(queries[i]));
}
}
public static void main(String[] args)
{
int queries[] = { 4 , 6 };
int q = queries.length;
preCompute();
performQueries(queries, q);
}
}
|
Python3
from bisect import bisect_right as upper_bound
MAX = 100000
v = []
def consecutiveOnes(x):
p = 0
while (x > 0 ):
if (x % 2 = = 1 and p = = 1 ):
return True
p = x % 2
x / / = 2
return False
def preCompute():
for i in range ( MAX + 1 ):
if (consecutiveOnes(i) = = 0 ):
v.append(i)
def nextValid(n):
it = upper_bound(v, n)
val = v[it]
return val
def performQueries(queries, q):
for i in range (q):
print (nextValid(queries[i]))
queries = [ 4 , 6 ]
q = len (queries)
preCompute()
performQueries(queries, q)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX = 100000;
static List< int > v = new List< int >();
static int upper_bound(List< int > ar, int k)
{
int s = 0;
int e = ar.Count;
while (s != e)
{
int mid = s + e >> 1;
if (ar[mid] <= k)
{
s = mid + 1;
}
else
{
e = mid;
}
}
if (s == ar.Count)
{
return -1;
}
return s;
}
static int consecutiveOnes( int x)
{
int p = 0;
while (x > 0)
{
if (x % 2 == 1 && p == 1)
{
return 1;
}
p = x % 2;
x /= 2;
}
return 0;
}
static void preCompute()
{
for ( int i = 0; i <= MAX; i++)
{
if (consecutiveOnes(i) == 0)
{
v.Add(i);
}
}
}
static int nextValid( int n)
{
int it = upper_bound(v, n);
int val = v[it];
return val;
}
static void performQueries( int [] queries, int q)
{
for ( int i = 0; i < q; i++)
{
Console.WriteLine(nextValid(queries[i]));
}
}
static public void Main()
{
int [] queries = { 4, 6 };
int q = queries.Length;
preCompute();
performQueries(queries, q);
}
}
|
Javascript
<script>
const MAX = 100000;
let v = [];
function upper_bound(ar, k)
{
let s = 0;
let e = ar.length;
while (s != e)
{
let mid = s + e >> 1;
if (ar[mid] <= k)
{
s = mid + 1;
}
else
{
e = mid;
}
}
if (s == ar.length)
{
return -1;
}
return s;
}
function consecutiveOnes(x)
{
let p = 0;
while (x > 0) {
if (x % 2 == 1 && p == 1)
return true ;
p = x % 2;
x = parseInt(x / 2);
}
return false ;
}
function preCompute()
{
for (let i = 0; i <= MAX; i++) {
if (!consecutiveOnes(i))
v.push(i);
}
}
function nextValid(n)
{
let it = upper_bound(v, n);
let val = v[it];
return val;
}
function performQueries(queries, q)
{
for (let i = 0; i < q; i++)
document.write(nextValid(queries[i]) + "<br>" );
}
let queries = [ 4, 6 ];
let q = queries.length;
preCompute();
performQueries(queries, q);
</script>
|
Time Complexity: O(MAX * log MAX)
Auxiliary Space: O(MAX)
Last Updated :
19 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...