Skip to content
Related Articles

Related Articles

Minimum count of consecutive integers till N whose bitwise AND is 0 with N

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 23 Dec, 2021
View Discussion
Improve Article
Save Article

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: The simplest approach is to iterate until N is greater than 0 and in each iteration check if the, bitwise AND of the numbers so far is equal to 0 or not. If not, then increase the count by 1.

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: The given problem can be solved based on the following observations: 

  1. 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.
  2. 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++




// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
int decimalToBinary(int N)
{
  
    // To store the binary number
    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;
  
        // Count used to store exponent value
        cnt++;
    }
  
    return B_Number;
}
    // Function to count the minimum count of
    // integers such that bitwise AND of that
    // many consecutive elements is equal to 0
    int count(int N)
    {
 
        // Stores the binary
        // representation of N
        string a = to_string(decimalToBinary(N));
 
        // Stores the MSB bit
        int m = a.size() - 1;
       
        // Stores the count
        // of numbers
        int res = (N - (pow(2, m) - 1));
 
        // Return res
        return res;
 
    }
     
 
// Driver Code
int main() {
 
    // Given Input
    int N = 18;
 
    // Function Call
    cout<< count(N);
return 0;
}
 
// This code is contributed by shikhasingrajput

Java




// Java program for the above approach
class GFG
{
   
    // Function to count the minimum count of
    // integers such that bitwise AND of that
    // many consecutive elements is equal to 0
    static int count(int N)
    {
 
        // Stores the binary
        // representation of N
        String a = Integer.toBinaryString(N);
 
        // Stores the MSB bit
        int m = a.length() - 1;
       
        // Stores the count
        // of numbers
        int res = (int) (N - (Math.pow(2, m) - 1));
 
        // Return res
        return res;
 
    }
 
    // Driver Code
    public static void main(String[] args) {
 
        // Given Input
        int N = 18;
 
        // Function Call
        System.out.println(count(N));
    }
}
 
// This code is contributed by shikhasingrajput

Python3




# Python program for the above approach
 
# Function to count the minimum count of
# integers such that bitwise AND of that
# many consecutive elements is equal to 0
 
 
def count(N):
 
    # Stores the binary
    # representation of N
    a = bin(N)
 
    # Excludes first two
    # characters "0b"
    a = a[2:]
 
    # Stores the MSB bit
    m = len(a)-1
 
    # Stores the count
    # of numbers
    res = N - (2**m-1)
 
    # Return res
    return res
 
 
# Driver Code
# Given Input
N = 18
 
# Function Call
print(count(N))

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
   
    // Function to count the minimum count of
    // integers such that bitwise AND of that
    // many consecutive elements is equal to 0
    static int count(int N)
    {
 
        // Stores the binary
        // representation of N
        String a = Convert.ToString(N, 2);
 
        // Stores the MSB bit
        int m = a.Length - 1;
       
        // Stores the count
        // of numbers
        int res = (int) (N - (Math.Pow(2, m) - 1));
 
        // Return res
        return res;
 
    }
 
    // Driver Code
    public static void Main(String[] args) {
 
        // Given Input
        int N = 18;
 
        // Function Call
        Console.WriteLine(count(N));
    }
}
 
// This code is contributed by umadevi9616

Javascript




<script>
// javascript program for the above approach
 
    // Function to count the minimum count of
    // integers such that bitwise AND of that
    // many consecutive elements is equal to 0
    function count(N)
    {
 
        // Stores the binary
        // representation of N
        var a = N.toString(2);
 
        // Stores the MSB bit
        var m = a.length - 1;
       
        // Stores the count
        // of numbers
        var res = N - (Math.pow(2, m) - 1);
 
        // Return res
        return res;
 
    }
 
    // Driver Code
     
        // Given Input
        var N = 18;
 
        // Function Call
        document.write(count(N));
 
 
// This code is contributed by shikhasingrajput
</script>

Output

3

Time Complexity: O(log(N))
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!