Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Difficulty Level : Easy
  • Last Updated : 13 Sep, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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:

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 :