Find longest range from numbers in range [1, N] having positive bitwise AND - GeeksforGeeks

25 Jan, 2022
Given a number N, the task is to find the longest range of integers [L, R] such that 1 ≤ L ≤ R ≤ N and the bitwise AND of all the numbers in that range is positive.
Examples:
Input: N = 7Output: 4 7Explanation: Check and from 1 to 7Bitwise AND operations:from 1 to 7 is 0 from 2 to 7 is 0from 3 to 7 is 0from 4 to 7 is 4Therefore, maximum range comes out from L = 4 to R = 7. 
Input: K = 16Output: 8 15
Approach: The problem can be solved based on the following mathematical observation. If 2K is the closest exponent of 2 greater than N then the maximum range will be either of the two:
From 2(K – 2) to (2(K – 1) – 1) [both value inclusive] or,
From 2(K – 1) to N
Because these ranges confirm that all the numbers in the range will have the most significant bit set for all of them. If the ranges vary for powers of 2 then the bitwise AND of the range will become 0.
Below is the implementation of the above approach.
C++
C
Java
Python3
C#
Javascript
// C++ code to implement above approach#include <bits/stdc++.h>using namespace std; // Function to find the closest exponent of 2// which is greater than Kint minpoweroftwo(int K){    int count = 0;    while (K > 0) {        count++;        K = K >> 1;    }    return count;} // Function to find the longest rangevoid findlongestrange(int N){     int K = minpoweroftwo(N);    int y = N + 1 - pow(2, K - 1);    int z = (pow(2, K - 1) - pow(2, K - 2));     if (y >= z) {        cout << pow(2, K - 1) << " " << N;    }    else {        cout << pow(2, K - 2) << " "            << pow(2, K - 1) - 1;    }} // Driver codeint main(){    int N = 16;    findlongestrange(N);    return 0;}
// C code to implement above approach#include <math.h>#include <stdio.h> // Function to find the closest exponent of 2// which is greater than Kint minpoweroftwo(int K){    int count = 0;    while (K > 0) {        count++;        K = K >> 1;    }    return count;} // Function to find the longest rangevoid findlongestrange(int N){     int K = minpoweroftwo(N);    int y = N + 1 - pow(2, K - 1);    int z = (pow(2, K - 1) - pow(2, K - 2));     if (y >= z) {        printf("%d %d", (int)pow(2, K - 1), N);    }    else {        printf("%d %d", (int)pow(2, K - 2),               (int)pow(2, K - 1)-1);    }} // Driver codeint main(){    int N = 16;    findlongestrange(N);    return 0;}
// Java code to implement above approach class GFG {     // Function to find the closest exponent of 2    // which is greater than K    static int minpoweroftwo(int K) {        int count = 0;        while (K > 0) {            count++;            K = K >> 1;        }        return count;    }     // Function to find the longest range    static void findlongestrange(int N) {         int K = minpoweroftwo(N);        int y = (int) (N + 1 - Math.pow(2, K - 1));        int z = (int) (Math.pow(2, K - 1) - Math.pow(2, K - 2));         if (y >= z) {            System.out.println(Math.pow(2, K - 1) + " " + N);        } else {            System.out.print((int) Math.pow(2, K - 2));            System.out.print(" ");            System.out.print((int) Math.pow(2, K - 1) - 1);        }    }     // Driver code    public static void main(String args[]) {        int N = 16;        findlongestrange(N);    }} // This code is contributed by gfgking.
# Python code to implement above approach # Function to find the closest exponent of 2# which is greater than Kdef minpoweroftwo(K):    count = 0;    while (K > 0):        count += 1;        K = K >> 1;     return count; # Function to find the longest rangedef findlongestrange(N):    K = minpoweroftwo(N);    y = int(N + 1 - pow(2, K - 1));    z = int(pow(2, K - 1) - pow(2, K - 2));     if (y >= z):        print(pow(2, K - 1) , " " , N);    else:        print(pow(2, K - 2));        print(" ");        print(pow(2, K - 1) - 1); # Driver codeif __name__ == '__main__':    N = 16;    findlongestrange(N); # This code is contributed by 29AjayKumar
// C# code to implement above approachusing System;class GFG {   // Function to find the closest exponent of 2  // which is greater than K  static int minpoweroftwo(int K)  {    int count = 0;    while (K > 0) {      count++;      K = K >> 1;    }    return count;  }   // Function to find the longest range  static void findlongestrange(int N)  {     int K = minpoweroftwo(N);    int y = (int)(N + 1 - Math.Pow(2, K - 1));    int z = (int)(Math.Pow(2, K - 1)                  - Math.Pow(2, K - 2));     if (y >= z) {      Console.Write(Math.Pow(2, K - 1) + " " + N);    }    else {      Console.Write((int)Math.Pow(2, K - 2));      Console.Write(" ");      Console.Write((int)Math.Pow(2, K - 1) - 1);    }  }   // Driver code  public static void Main()  {    int N = 16;    findlongestrange(N);  }} // This code is contributed by ukasp.
<script>        // JavaScript code for the above approach         // Function to find the closest exponent of 2        // which is greater than K        function minpoweroftwo(K) {            let count = 0;            while (K > 0) {                count++;                K = K >> 1;            }            return count;        }         // Function to find the longest range        function findlongestrange(N)        {            let K = minpoweroftwo(N);            let y = N + 1 - Math.pow(2, K - 1);            let z = (Math.pow(2, K - 1) - Math.pow(2, K - 2));             if (y >= z) {                document.write(Math.pow(2, K - 1) + " " + N);            }            else {                document.write(Math.pow(2, K - 2) + " "                    + (Math.pow(2, K - 1) - 1));            }        }         // Driver code        let N = 16;        findlongestrange(N);   // This code is contributed by Potta Lokesh    </script>
  
8 15
 
Time Complexity: O(logN)Auxiliary Space: O(1)
 
lokeshpotta20
gfgking
ukasp
29AjayKumar
array-range-queries
Bitwise-AND
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Add two numbers without using arithmetic operators
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7