📜  从范围 [1, N] 中具有正位 AND 的数字中查找最长范围

📅  最后修改于: 2022-05-13 01:56:09.860000             🧑  作者: Mango

从范围 [1, N] 中具有正位 AND 的数字中查找最长范围

给定一个数字N ,任务是找到整数[L, R]的最长范围,使得1 ≤ L ≤ R ≤ N并且该范围内所有数字的按位与

例子:

方法:可以根据以下数学观察来解决问题。如果2 K是大于N2的最接近指数,则最大范围将是以下两者之一:

  • 2 (K – 2)到 (2 (K – 1) – 1) [包括两个值] 或,
  • 2 (K – 1)到 N

因为这些范围确认该范围内的所有数字都将为所有数字设置最高有效位。如果范围因 2 的幂而异,则范围的按位与将变为 0。

下面是上述方法的实现。

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to find the closest exponent of 2
// which is greater than K
int minpoweroftwo(int K)
{
    int count = 0;
    while (K > 0) {
        count++;
        K = K >> 1;
    }
    return count;
}
 
// Function to find the longest range
void 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 code
int main()
{
    int N = 16;
    findlongestrange(N);
    return 0;
}


C
// C code to implement above approach
#include 
#include 
 
// Function to find the closest exponent of 2
// which is greater than K
int minpoweroftwo(int K)
{
    int count = 0;
    while (K > 0) {
        count++;
        K = K >> 1;
    }
    return count;
}
 
// Function to find the longest range
void 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 code
int main()
{
    int N = 16;
    findlongestrange(N);
    return 0;
}


Java
// 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.


Python3
# Python code to implement above approach
 
# Function to find the closest exponent of 2
# which is greater than K
def minpoweroftwo(K):
    count = 0;
    while (K > 0):
        count += 1;
        K = K >> 1;
 
    return count;
 
# Function to find the longest range
def 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 code
if __name__ == '__main__':
    N = 16;
    findlongestrange(N);
 
# This code is contributed by 29AjayKumar


C#
// C# code to implement above approach
using 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.


Javascript



输出
8 15

时间复杂度: O(logN)
辅助空间: O(1)