📜  求 K 的最小值,使得 [N, NK] 范围内的数字按位与为 0

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

求 K 的最小值,使得 [N, NK] 范围内的数字按位与为 0

给定一个整数N ,任务是找到最小的数K ,使得 [N, NK] 范围内所有数字的按位与为 0,即N & (N – 1) & (N – 2) &… (N – K) = 0

例子:

朴素的方法:解决问题的最简单方法是从给定的数字开始,然后用比当前数字少一个数字进行按位与,直到累积的按位与不等于0 。请按照以下步骤解决此问题:

  • 声明一个变量cummAnd存储可交换的按位与并用n初始化它。
  • 声明一个变量i n-1 初始化它。
  • cummAnd不等于0 时运行循环:
    • cummAnd = cummAnd & 我。
    • i1。
  • 最后,打印n - i。

以下是以下方法的实现:

C++
// C++ program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
 
#include 
using namespace std;
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
int findSmallestNumK(int n)
{
    int cummAnd = n;
 
    int i = n - 1;
    // Since, we need the largest no,
    // we start from n itself, till 0
    while (cummAnd != 0) {
 
        cummAnd = cummAnd & i;
        if (cummAnd == 0) {
            return i;
        }
 
        i--;
    }
 
    return -1;
}
 
// Driver Code
int main()
{
 
    int N = 17;
    int lastNum = findSmallestNumK(N);
    int K = lastNum == -1 ? lastNum : N - lastNum;
    cout << K << "\n";
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function is to find the largest no which gives the
    // sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
    public static int findSmallestNumK(int n)
    {
        int cummAnd = n;
        int i = n - 1;
       
        // Since, we need the largest no,
        // we start from n itself, till 0
        while (cummAnd != 0) {
 
            cummAnd = cummAnd & i;
            if (cummAnd == 0) {
                return i;
            }
 
            i--;
        }
 
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 17;
        int lastNum = findSmallestNumK(N);
        int K = lastNum == -1 ? lastNum : N - lastNum;
        System.out.println(K);
       
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program to find smallest value of K
# such that bitwise AND of numbers
# in range [N, N-K] is 0
 
# Function is to find the largest no which gives the
# sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
def findSmallestNumK(n):
     
    cummAnd = n
    i = n - 1
     
    # Since, we need the largest no,
    # we start from n itself, till 0
    while (cummAnd != 0):
        cummAnd = cummAnd & i
         
        if (cummAnd == 0):
            return i
 
        i -= 1
 
    return -1
 
# Driver Code
if __name__ == '__main__':
     
    N = 17
    lastNum = findSmallestNumK(N);
    K = lastNum if lastNum == -1 else N - lastNum
     
    print(K)
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
public static int findSmallestNumK(int n)
{
    int cummAnd = n;
    int i = n - 1;
 
    // Since, we need the largest no,
    // we start from n itself, till 0
    while (cummAnd != 0)
    {
        cummAnd = cummAnd & i;
        if (cummAnd == 0)
        {
            return i;
        }
        i--;
    }
    return -1;
}
 
// Driver code
static void Main()
{
    int N = 17;
    int lastNum = findSmallestNumK(N);
    int K = lastNum == -1 ? lastNum : N - lastNum;
     
    Console.WriteLine(K);
}
}
 
// This code is contributed by abhinavjain194


Javascript


C++
// C++ program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
 
#include 
using namespace std;
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
int findSmallestNumK(int n)
{
 
    // find largest power of 2
    // less than or equal to n
    int larPowOfTwo = floor(log2(n));
 
    larPowOfTwo = 1 << larPowOfTwo;
 
    return larPowOfTwo - 1;
}
 
// Driver Code
int main()
{
 
    int N = 17;
    int lastNum = findSmallestNumK(N);
    int K = lastNum == -1 ? lastNum : N - lastNum;
    cout << K << "\n";
    return 0;
}


C
// C program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
 
#include  //for log2
#include 
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
int findSmallestNumK(int n)
{
 
  // find largest power of 2
  // less than or equal to n
  int larPowOfTwo = floor(log2(n));
 
  larPowOfTwo = 1 << larPowOfTwo;
 
  return larPowOfTwo - 1;
}
 
// Driver Code
int main()
{
 
  int N = 17;
  int lastNum = findSmallestNumK(N);
  int K = lastNum == -1 ? lastNum : N - lastNum;
  printf("%d\n", K);
  return 0;
}
 
// This code is contributed by phalashi.


Java
// Java program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
import java.io.*;
 
class GFG {
 
    // Function is to find the largest no which gives the
    // sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
    static int findSmallestNumK(int n)
    {
 
        // find largest power of 2
        // less than or equal to n
        int larPowOfTwo
            = (int)Math.floor(Math.log(n) / Math.log(2));
 
        larPowOfTwo = 1 << larPowOfTwo;
 
        return larPowOfTwo - 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 17;
        int lastNum = findSmallestNumK(N);
        int K = lastNum == -1 ? lastNum : N - lastNum;
        System.out.println(K);
    }
}
 
// This code is contributed by rishavmahato348.


Python3
# Python program to find smallest value of K
# such that bitwise AND of numbers
# in range [N, N-K] is 0
# Function is to find the largest no which gives the
# sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
import math
 
def findSmallestNumK( n):
 
 
    # find largest power of 2
    # less than or equal to n
    larPowOfTwo = math.floor(math.log2(n))
 
    larPowOfTwo = 1 << larPowOfTwo
 
    return larPowOfTwo - 1
 
 
# Driver Code
N = 17
lastNum = findSmallestNumK(N)
K = lastNum if (lastNum == -1 ) else N - lastNum
print(K)
 
# this code is contributed by shivanisinghss2110


C#
// C# program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
using System;
 
class GFG{
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
static int findSmallestNumK(int n)
{
     
    // Find largest power of 2
    // less than or equal to n
    int larPowOfTwo = (int)Math.Floor(Math.Log(n) /
                                      Math.Log(2));
 
    larPowOfTwo = 1 << larPowOfTwo;
 
    return larPowOfTwo - 1;
}
 
// Driver Code
public static void Main()
{
    int N = 17;
    int lastNum = findSmallestNumK(N);
    int K = lastNum == -1 ? lastNum : N - lastNum;
     
    Console.Write(K);
}
}
 
// This code is contributed by subhammahato348


Javascript


输出
2

时间复杂度:O(N)

辅助空间:O(1)

有效的方法:这个问题可以通过使用按位与运算符的属性来解决,即如果两个位都设置了,那么只有结果将是非零的。因此,我们必须找到2的最大幂,它小于或等于N (比如说X )。请按照以下步骤解决此问题:

  • log2(n)函数给出2的幂,它等于n
  • 因为,它的返回类型是双精度的。所以我们使用 floor函数得到2的最大幂,它小于或等于n并将其存储到X中。
  • X = (1 << X) – 1。
  • 最后,打印N - X。

以下是以下方法的实现:

C++

// C++ program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
 
#include 
using namespace std;
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
int findSmallestNumK(int n)
{
 
    // find largest power of 2
    // less than or equal to n
    int larPowOfTwo = floor(log2(n));
 
    larPowOfTwo = 1 << larPowOfTwo;
 
    return larPowOfTwo - 1;
}
 
// Driver Code
int main()
{
 
    int N = 17;
    int lastNum = findSmallestNumK(N);
    int K = lastNum == -1 ? lastNum : N - lastNum;
    cout << K << "\n";
    return 0;
}

C

// C program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
 
#include  //for log2
#include 
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
int findSmallestNumK(int n)
{
 
  // find largest power of 2
  // less than or equal to n
  int larPowOfTwo = floor(log2(n));
 
  larPowOfTwo = 1 << larPowOfTwo;
 
  return larPowOfTwo - 1;
}
 
// Driver Code
int main()
{
 
  int N = 17;
  int lastNum = findSmallestNumK(N);
  int K = lastNum == -1 ? lastNum : N - lastNum;
  printf("%d\n", K);
  return 0;
}
 
// This code is contributed by phalashi.

Java

// Java program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
import java.io.*;
 
class GFG {
 
    // Function is to find the largest no which gives the
    // sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
    static int findSmallestNumK(int n)
    {
 
        // find largest power of 2
        // less than or equal to n
        int larPowOfTwo
            = (int)Math.floor(Math.log(n) / Math.log(2));
 
        larPowOfTwo = 1 << larPowOfTwo;
 
        return larPowOfTwo - 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 17;
        int lastNum = findSmallestNumK(N);
        int K = lastNum == -1 ? lastNum : N - lastNum;
        System.out.println(K);
    }
}
 
// This code is contributed by rishavmahato348.

Python3

# Python program to find smallest value of K
# such that bitwise AND of numbers
# in range [N, N-K] is 0
# Function is to find the largest no which gives the
# sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
import math
 
def findSmallestNumK( n):
 
 
    # find largest power of 2
    # less than or equal to n
    larPowOfTwo = math.floor(math.log2(n))
 
    larPowOfTwo = 1 << larPowOfTwo
 
    return larPowOfTwo - 1
 
 
# Driver Code
N = 17
lastNum = findSmallestNumK(N)
K = lastNum if (lastNum == -1 ) else N - lastNum
print(K)
 
# this code is contributed by shivanisinghss2110

C#

// C# program to find smallest value of K
// such that bitwise AND of numbers
// in range [N, N-K] is 0
using System;
 
class GFG{
 
// Function is to find the largest no which gives the
// sequence n & (n - 1) & (n - 2)&.....&(n - k) = 0.
static int findSmallestNumK(int n)
{
     
    // Find largest power of 2
    // less than or equal to n
    int larPowOfTwo = (int)Math.Floor(Math.Log(n) /
                                      Math.Log(2));
 
    larPowOfTwo = 1 << larPowOfTwo;
 
    return larPowOfTwo - 1;
}
 
// Driver Code
public static void Main()
{
    int N = 17;
    int lastNum = findSmallestNumK(N);
    int K = lastNum == -1 ? lastNum : N - lastNum;
     
    Console.Write(K);
}
}
 
// This code is contributed by subhammahato348

Javascript


输出
2

时间复杂度: O(log N)

空间复杂度: O(1)