最大化最大数 K 使得 K 的按位和直到 N 为 0
给定一个整数N,任务是找到K的最大值,使得N & (N-1) & (N-2) & … & (K) = 0。这里&表示按位与运算符。
例子:
Input: N = 5
Output: 3
Explanation: The value of the expression 5 & 4 & 3 = 0. any value greater than 3 (example 4) will not satisfy
our given condition
Input: N =17
Output: 15
天真的方法:蛮力方法是从 N-1 开始并执行按位与运算直到我们得到 0。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find maximum value of k
// which makes bitwise AND zero.
int findMaxK(int N)
{
// Take k = N initially
int K = N;
// Start traversing from N-1 till 0
for (int i = N - 1; i >= 0; i--) {
K &= i;
// Whenever we get AND as 0
// we stop and return
if (K == 0) {
return i;
}
}
return 0;
}
// Driver Code
int main()
{
int N = 5;
cout << findMaxK(N);
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find maximum value of k
// which makes bitwise AND zero.
static int findMaxK(int N)
{
// Take k = N initially
int K = N;
// Start traversing from N-1 till 0
for (int i = N - 1; i >= 0; i--) {
K &= i;
// Whenever we get AND as 0
// we stop and return
if (K == 0) {
return i;
}
}
return 0;
}
// Driver Code
public static void main (String[] args) {
int N = 5;
System.out.println(findMaxK(N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 program for above approach
# Function to find maximum value of k
# which makes bitwise AND zero.
def findMaxK(N):
# Take k = N initially
K = N
# Start traversing from N-1 till 0
i = N-1
while(i >= 0):
K &= i
# Whenever we get AND as 0
# we stop and return
if (K == 0):
return i
i -= 1
return 0
# Driver Code
if __name__ == '__main__':
N = 5
print(findMaxK(N))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find maximum value of k
// which makes bitwise AND zero.
static int findMaxK(int N)
{
// Take k = N initially
int K = N;
// Start traversing from N-1 till 0
for (int i = N - 1; i >= 0; i--) {
K &= i;
// Whenever we get AND as 0
// we stop and return
if (K == 0) {
return i;
}
}
return 0;
}
// Driver Code
public static void Main (String[] args)
{
int N = 5;
Console.Write(findMaxK(N));
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ program for above approach
#include
using namespace std;
// Function to find maximum value of k
// which makes bitwise AND zero.
int findMaxK(int N)
{
// Finding the power less than N
int p = log2(N);
return pow(2, p);
}
// Driver Code
int main()
{
int N = 5;
cout << findMaxK(N) - 1 << endl;
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to find maximum value of k
// which makes bitwise AND zero.
static int findMaxK(int N)
{
// Finding the power less than N
int p = (int)(Math.log(N) / Math.log(2));
return (int)Math.pow(2, p);
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
System.out.println(findMaxK(N) - 1);
}
}
// This code is contributed by maddler.
Python3
import math
# Function to find maximum value of k
# which makes bitwise AND zero.
def findMaxK(N):
# Finding the power less than N
p = math.log(N) // math.log(2);
return int(pow(2, p));
# Driver Code
N = 5;
print(findMaxK(N) - 1);
# This code is contributed by _saurabh_jaiswal
C#
/*package whatever //do not write package name here */
using System;
class GFG
{
// Function to find maximum value of k
// which makes bitwise AND zero.
static int findMaxK(int N)
{
// Finding the power less than N
int p = (int)(Math.Log(N) / Math.Log(2));
return (int)Math.Pow(2, p);
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
Console.Write(findMaxK(N) - 1);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
3
时间复杂度: O(N)
辅助空间:O(1)
高效方法:通过一些观察,可以看出答案总是等于2的最高幂,小于等于(N-1)。所以最后,答案总是等于 2^K -1,其中 K 是某个值。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find maximum value of k
// which makes bitwise AND zero.
int findMaxK(int N)
{
// Finding the power less than N
int p = log2(N);
return pow(2, p);
}
// Driver Code
int main()
{
int N = 5;
cout << findMaxK(N) - 1 << endl;
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to find maximum value of k
// which makes bitwise AND zero.
static int findMaxK(int N)
{
// Finding the power less than N
int p = (int)(Math.log(N) / Math.log(2));
return (int)Math.pow(2, p);
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
System.out.println(findMaxK(N) - 1);
}
}
// This code is contributed by maddler.
Python3
import math
# Function to find maximum value of k
# which makes bitwise AND zero.
def findMaxK(N):
# Finding the power less than N
p = math.log(N) // math.log(2);
return int(pow(2, p));
# Driver Code
N = 5;
print(findMaxK(N) - 1);
# This code is contributed by _saurabh_jaiswal
C#
/*package whatever //do not write package name here */
using System;
class GFG
{
// Function to find maximum value of k
// which makes bitwise AND zero.
static int findMaxK(int N)
{
// Finding the power less than N
int p = (int)(Math.Log(N) / Math.Log(2));
return (int)Math.Pow(2, p);
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
Console.Write(findMaxK(N) - 1);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
3
时间复杂度: O(log N)
辅助空间: O(1)