📌  相关文章
📜  设置了第K位的,超过N的最小数字

📅  最后修改于: 2021-05-25 09:23:44             🧑  作者: Mango

给定两个整数NK ,任务是找到大于N的最小数字,其二进制表示形式中的K位已设置。

例子:

简单方法:最简单的方法是遍历从N + 1和每个编号开始的所有数字,检查是否它的K位被设置或没有。如果找到这样的号码,请打印该号码。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the number greater
// than n whose Kth bit is set
int find_next(int n, int k)
{
    // Iterate from N + 1
    int M = n + 1;
 
    while (1) {
 
        // Check if Kth bit is
        // set or not
        if (M & (1ll << k))
            break;
 
        // Increment M for next number
        M++;
    }
 
    // Return the minimum value
    return M;
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 15, K = 2;
 
    // Function Call
    cout << find_next(N, K);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to find the number greater
// than n whose Kth bit is set
static int find_next(int n, int k)
{
  // Iterate from N + 1
  int M = n + 1;
 
  while (true)
  {
    // Check if Kth bit is
    // set or not
    if ((M & (1L << k)) > 0)
      break;
 
    // Increment M for
    // next number
    M++;
  }
 
  // Return the minimum value
  return M;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given N and K
  int N = 15, K = 2;
 
  // Function Call
  System.out.print(find_next(N, K));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for
# the above approach
 
# Function to find the number
# greater than n whose Kth
# bit is set
def find_next(n, k):
   
    # Iterate from N + 1
    M = n + 1;
 
    while (True):
       
        # Check if Kth bit is
        # set or not
        if ((M & (1 << k)) > 0):
            break;
 
        # Increment M for
        # next number
        M += 1;
 
    # Return the
    # minimum value
    return M;
 
# Driver Code
if __name__ == '__main__':
   
    # Given N and K
    N = 15; K = 2;
 
    # Function Call
    print(find_next(N, K));
 
# This code is contributed by Rajput-Ji


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to find the
// number greater than n
// whose Kth bit is set
static int find_next(int n, int k)
{
  // Iterate from N + 1
  int M = n + 1;
 
  while (true)
  {
    // Check if Kth bit is
    // set or not
    if ((M & (1L << k)) > 0)
      break;
 
    // Increment M for
    // next number
    M++;
  }
 
  // Return the minimum value
  return M;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given N and K
  int N = 15, K = 2;
 
  // Function Call
  Console.Write(find_next(N, K));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the number greater
// than n whose Kth bit is set
int find_next(int n, int k)
{
    // Stores the resultant number
    int ans = 0;
 
    // If Kth bit is not set
    if ((n & (1ll << k)) == 0) {
        int cur = 0;
 
        // cur will be the sum of all
        // powers of 2 < k
        for (int i = 0; i < k; i++) {
 
            // If the current bit is set
            if (n & (1ll << i))
                cur += 1ll << i;
        }
 
        // Add Kth power of 2 to n and
        // subtract the all powers of 2
        // less than K that are set
        ans = n - cur + (1ll << k);
    }
 
    // If the kth bit is set
    else {
        int first_unset_bit = -1, cur = 0;
 
        for (int i = 0; i < 64; i++) {
 
            // First unset bit position
            if ((n & (1ll << i)) == 0) {
                first_unset_bit = i;
                break;
            }
 
            // sum of bits that are set
            else
                cur += (1ll << i);
        }
 
        // Add Kth power of 2 to n and
        // subtract the all powers of 2
        // less than K that are set
        ans = n - cur
              + (1ll << first_unset_bit);
 
        // If Kth bit became unset
        // then set it again
        if ((ans & (1ll << k)) == 0)
            ans += (1ll << k);
    }
 
    // Return the resultant number
    return ans;
}
 
// Driver Code
int main()
{
    int N = 15, K = 2;
 
    // Print ans
    cout << find_next(N, K);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to find the number
// greater than n whose Kth
// bit is set
static int find_next(int n,
                     int k)
{
  // Stores the resultant
  // number
  int ans = 0;
 
  // If Kth bit is not set
  if ((n & (1L << k)) == 0)
  {
    int cur = 0;
 
    // cur will be the sum of all
    // powers of 2 < k
    for (int i = 0; i < k; i++)
    {
      // If the current bit is set
      if ((n & (1L << i)) > 0)
        cur += 1L << i;
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur + (1L << k));
  }
 
  // If the kth bit is set
  else
  {
    int first_unset_bit = -1, cur = 0;
 
    for (int i = 0; i < 64; i++)
    {
      // First unset bit position
      if ((n & (1L << i)) == 0)
      {
        first_unset_bit = i;
        break;
      }
 
      // sum of bits that are set
      else
        cur += (1L << i);
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur +
          (1L << first_unset_bit));
 
    // If Kth bit became unset
    // then set it again
    if ((ans & (1L << k)) == 0)
      ans += (1L << k);
  }
 
  // Return the resultant number
  return ans;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 15, K = 2;
 
  // Print ans
  System.out.print(find_next(N, K));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Function to find the number greater
# than n whose Kth bit is set
def find_next(n, k):
 
    # Stores the resultant number
    ans = 0
     
    # If Kth bit is not set
    if ((n & (1 << k)) == 0):
        cur = 0
 
        # cur will be the sum of all
        # powers of 2 < k
        for i in range(k):
 
            # If the current bit is set
            if (n & (1 << i)):
                cur += 1 << i
 
        # Add Kth power of 2 to n and
        # subtract the all powers of 2
        # less than K that are set
        ans = n - cur + (1 << k)
 
    # If the kth bit is set
    else:
        first_unset_bit, cur = -1, 0
 
        for i in range(64):
 
            # First unset bit position
            if ((n & (1 << i)) == 0):
                first_unset_bit = i
                break
 
            # Sum of bits that are set
            else:
                cur += (1 << i)
 
        # Add Kth power of 2 to n and
        # subtract the all powers of 2
        # less than K that are set
        ans = n - cur + (1 << first_unset_bit)
 
        # If Kth bit became unset
        # then set it again
        if ((ans & (1 << k)) == 0):
            ans += (1 << k)
 
    # Return the resultant number
    return ans
     
# Driver code
N, K = 15, 2
 
# Print ans
print(find_next(N, K))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to find the number
// greater than n whose Kth
// bit is set
static int find_next(int n,
                     int k)
{
  // Stores the resultant
  // number
  int ans = 0;
 
  // If Kth bit is not set
  if ((n & (1L << k)) == 0)
  {
    int cur = 0;
 
    // cur will be the sum of all
    // powers of 2 < k
    for (int i = 0; i < k; i++)
    {
      // If the current bit is set
      if ((n & (1L << i)) > 0)
        cur += (int)1L << i;
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur + (1L << k));
  }
 
  // If the kth bit is set
  else
  {
    int first_unset_bit = -1, cur = 0;
 
    for (int i = 0; i < 64; i++)
    {
      // First unset bit position
      if ((n & (1L << i)) == 0)
      {
        first_unset_bit = i;
        break;
      }
 
      // Sum of bits that are set
      else
        cur +=(int)(1L << i);
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur +
          (1L << first_unset_bit));
 
    // If Kth bit became unset
    // then set it again
    if ((ans & (1L << k)) == 0)
      ans += (int)(1L << k);
  }
 
  // Return the resultant number
  return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 15, K = 2;
 
  // Print ans
  Console.Write(find_next(N, K));
}
}
 
// This code is contributed by Rajput-Ji


输出
20

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

高效方法:为了优化上述方法,存在两种可能性:

  1. K位未设置:观察到位置大于K的位不会受到影响。因此,使K位设置,并在其左侧0所有位。
  2. K位被置位:找到第一个最低有效未置位并置位。之后,取消设置其右侧的所有位。

请按照以下步骤解决问题:

  1. 检查的KN位设置。如果发现为真,则找到最低的未设置位并进行设置,然后将其右边的所有位更改为0
  2. 否则,将第K位设置为1,并将所有低于K的位更新为0
  3. 完成上述步骤后,打印答案。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the number greater
// than n whose Kth bit is set
int find_next(int n, int k)
{
    // Stores the resultant number
    int ans = 0;
 
    // If Kth bit is not set
    if ((n & (1ll << k)) == 0) {
        int cur = 0;
 
        // cur will be the sum of all
        // powers of 2 < k
        for (int i = 0; i < k; i++) {
 
            // If the current bit is set
            if (n & (1ll << i))
                cur += 1ll << i;
        }
 
        // Add Kth power of 2 to n and
        // subtract the all powers of 2
        // less than K that are set
        ans = n - cur + (1ll << k);
    }
 
    // If the kth bit is set
    else {
        int first_unset_bit = -1, cur = 0;
 
        for (int i = 0; i < 64; i++) {
 
            // First unset bit position
            if ((n & (1ll << i)) == 0) {
                first_unset_bit = i;
                break;
            }
 
            // sum of bits that are set
            else
                cur += (1ll << i);
        }
 
        // Add Kth power of 2 to n and
        // subtract the all powers of 2
        // less than K that are set
        ans = n - cur
              + (1ll << first_unset_bit);
 
        // If Kth bit became unset
        // then set it again
        if ((ans & (1ll << k)) == 0)
            ans += (1ll << k);
    }
 
    // Return the resultant number
    return ans;
}
 
// Driver Code
int main()
{
    int N = 15, K = 2;
 
    // Print ans
    cout << find_next(N, K);
 
    return 0;
}

Java

// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to find the number
// greater than n whose Kth
// bit is set
static int find_next(int n,
                     int k)
{
  // Stores the resultant
  // number
  int ans = 0;
 
  // If Kth bit is not set
  if ((n & (1L << k)) == 0)
  {
    int cur = 0;
 
    // cur will be the sum of all
    // powers of 2 < k
    for (int i = 0; i < k; i++)
    {
      // If the current bit is set
      if ((n & (1L << i)) > 0)
        cur += 1L << i;
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur + (1L << k));
  }
 
  // If the kth bit is set
  else
  {
    int first_unset_bit = -1, cur = 0;
 
    for (int i = 0; i < 64; i++)
    {
      // First unset bit position
      if ((n & (1L << i)) == 0)
      {
        first_unset_bit = i;
        break;
      }
 
      // sum of bits that are set
      else
        cur += (1L << i);
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur +
          (1L << first_unset_bit));
 
    // If Kth bit became unset
    // then set it again
    if ((ans & (1L << k)) == 0)
      ans += (1L << k);
  }
 
  // Return the resultant number
  return ans;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 15, K = 2;
 
  // Print ans
  System.out.print(find_next(N, K));
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program for the above approach
 
# Function to find the number greater
# than n whose Kth bit is set
def find_next(n, k):
 
    # Stores the resultant number
    ans = 0
     
    # If Kth bit is not set
    if ((n & (1 << k)) == 0):
        cur = 0
 
        # cur will be the sum of all
        # powers of 2 < k
        for i in range(k):
 
            # If the current bit is set
            if (n & (1 << i)):
                cur += 1 << i
 
        # Add Kth power of 2 to n and
        # subtract the all powers of 2
        # less than K that are set
        ans = n - cur + (1 << k)
 
    # If the kth bit is set
    else:
        first_unset_bit, cur = -1, 0
 
        for i in range(64):
 
            # First unset bit position
            if ((n & (1 << i)) == 0):
                first_unset_bit = i
                break
 
            # Sum of bits that are set
            else:
                cur += (1 << i)
 
        # Add Kth power of 2 to n and
        # subtract the all powers of 2
        # less than K that are set
        ans = n - cur + (1 << first_unset_bit)
 
        # If Kth bit became unset
        # then set it again
        if ((ans & (1 << k)) == 0):
            ans += (1 << k)
 
    # Return the resultant number
    return ans
     
# Driver code
N, K = 15, 2
 
# Print ans
print(find_next(N, K))
 
# This code is contributed by divyeshrabadiya07

C#

// C# program for
// the above approach
using System;
class GFG{
 
// Function to find the number
// greater than n whose Kth
// bit is set
static int find_next(int n,
                     int k)
{
  // Stores the resultant
  // number
  int ans = 0;
 
  // If Kth bit is not set
  if ((n & (1L << k)) == 0)
  {
    int cur = 0;
 
    // cur will be the sum of all
    // powers of 2 < k
    for (int i = 0; i < k; i++)
    {
      // If the current bit is set
      if ((n & (1L << i)) > 0)
        cur += (int)1L << i;
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur + (1L << k));
  }
 
  // If the kth bit is set
  else
  {
    int first_unset_bit = -1, cur = 0;
 
    for (int i = 0; i < 64; i++)
    {
      // First unset bit position
      if ((n & (1L << i)) == 0)
      {
        first_unset_bit = i;
        break;
      }
 
      // Sum of bits that are set
      else
        cur +=(int)(1L << i);
    }
 
    // Add Kth power of 2 to n and
    // subtract the all powers of 2
    // less than K that are set
    ans = (int)(n - cur +
          (1L << first_unset_bit));
 
    // If Kth bit became unset
    // then set it again
    if ((ans & (1L << k)) == 0)
      ans += (int)(1L << k);
  }
 
  // Return the resultant number
  return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 15, K = 2;
 
  // Print ans
  Console.Write(find_next(N, K));
}
}
 
// This code is contributed by Rajput-Ji
输出
20

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