Minimum count of consecutive integers till N whose bitwise AND is 0 with N
Last Updated :
29 Mar, 2023
Given a positive integer N, the task is to print the minimum count of consecutive numbers less than N such that the bitwise AND of these consecutive elements, including integer N, is equal to 0.
Examples:
Input: N = 18
Output: 3
Explanation:
One possible way is to form a sequence of {15, 16, 17, and 18}. The bitwise AND of the given numbers is equal to 0.
Therefore, a minimum of 3 numbers are needed to make the bitwise AND of a sequence of 4 consecutive elements, including 18 to 0.
Input: N = 4
Output: 1
Explanation:
One possible way is to form a sequence of {4, 3}. The bitwise AND of the given numbers is equal to 0.
Therefore, a minimum of 1 number is needed to make the bitwise AND of a sequence of 2 consecutive elements including 4 to 0.
Naive Approach:
1) We start iterating from N till 1 and in each iteration, we check if the bitwise AND of all the numbers so far is equal to 0 or not.
2) To check if the bitwise AND is equal to 0 or not, we use the property that a bitwise AND of two numbers is equal to 0 if and only if the binary representations of the two numbers do not have any common 1’s.
3) So, for each number i, we check if i & (i-1) is equal to 0 or not. If it is, then the bitwise AND of all the numbers so far, including i, is equal to 0.
4) If the bitwise AND is equal to 0, then we return the count of numbers so far plus 1, since we need to include i in the sequence as well.
5) If we reach the end of the loop and have not found a sequence with a bitwise AND equal to 0, then we return the count of numbers so far plus 1.
Implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int minConsecutiveNumbers( int N) {
int count = 0;
for ( int i=N; i>0; i--) {
if ((i & (i-1)) == 0) {
return count+1;
}
count++;
}
return count+1;
}
int main() {
int N = 18;
cout << minConsecutiveNumbers(N) << endl;
return 0;
}
|
Java
public class GFG {
public static int minConsecutiveNumbers( int N)
{
int count = 0 ;
for ( int i = N; i > 0 ; i--)
{
if ((i & (i - 1 )) == 0 )
{
return count + 1 ;
}
count++;
}
return count + 1 ;
}
public static void main(String[] args)
{
int N = 18 ;
System.out.println(minConsecutiveNumbers(N));
}
}
|
Python3
def minConsecutiveNumbers(N):
count = 0
for i in range (N, 0 , - 1 ):
if (i & (i - 1 )) = = 0 :
return count + 1
count + = 1
return count + 1
N = 18
print (minConsecutiveNumbers(N))
|
Javascript
function minConsecutiveNumbers(N) {
let count = 0;
for (let i=N; i>0; i--) {
if ((i & (i-1)) == 0) {
return count+1;
}
count++;
}
return count+1;
}
let N = 18;
console.log(minConsecutiveNumbers(N));
|
C#
using System;
public class Program {
public static int MinConsecutiveNumbers( int N)
{
int count = 0;
for ( int i = N; i > 0; i--) {
if ((i & (i - 1)) == 0) {
return count + 1;
}
count++;
}
return count + 1;
}
public static void Main()
{
int N = 18;
Console.WriteLine(MinConsecutiveNumbers(N));
}
}
|
Output: 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The given problem can be solved based on the following observations:
- To make the bitwise AND of sequence including N equal to 0, it is necessary to make the MSB bit of the number N equal to 0.
- Therefore, the idea is to include all the integers greater than or equal to (2MSB -1) and less than N in the sequence, it will give the minimum count.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int decimalToBinary( int N)
{
int B_Number = 0;
int cnt = 0;
while (N != 0)
{
int rem = N % 2;
double c = pow (10, cnt);
B_Number += rem * c;
N /= 2;
cnt++;
}
return B_Number;
}
int count( int N)
{
string a = to_string(decimalToBinary(N));
int m = a.size() - 1;
int res = (N - ( pow (2, m) - 1));
return res;
}
int main() {
int N = 18;
cout<< count(N);
return 0;
}
|
Java
class GFG
{
static int count( int N)
{
String a = Integer.toBinaryString(N);
int m = a.length() - 1 ;
int res = ( int ) (N - (Math.pow( 2 , m) - 1 ));
return res;
}
public static void main(String[] args) {
int N = 18 ;
System.out.println(count(N));
}
}
|
Python3
def count(N):
a = bin (N)
a = a[ 2 :]
m = len (a) - 1
res = N - ( 2 * * m - 1 )
return res
N = 18
print (count(N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int count( int N)
{
String a = Convert.ToString(N, 2);
int m = a.Length - 1;
int res = ( int ) (N - (Math.Pow(2, m) - 1));
return res;
}
public static void Main(String[] args) {
int N = 18;
Console.WriteLine(count(N));
}
}
|
Javascript
<script>
function count(N)
{
var a = N.toString(2);
var m = a.length - 1;
var res = N - (Math.pow(2, m) - 1);
return res;
}
var N = 18;
document.write(count(N));
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...