给定范围[L,R] ,任务是找到一个对(X,Y) ,以使L≤X
例子:
Input: L = 1, R = 9
Output: 8
In all the possible pairs, pair (8, 9) gives the maximum value for bitwise AND.
Input: L = 523641, R = 985624
Output: 985622
天真的方法:从L到R进行迭代,并检查每个可能的对的按位与,并在最后打印最大值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum bitwise AND
// possible among all the possible pairs
int maxAND(int L, int R)
{
int maximum = L & R;
for (int i = L; i < R; i++)
for (int j = i + 1; j <= R; j++)
// Maximum among all (i, j) pairs
maximum = max(maximum, (i & j));
return maximum;
}
// Driver code
int main()
{
int L = 1, R = 632;
cout << maxAND(L, R);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum bitwise AND
// possible among all the possible pairs
static int maxAND(int L, int R)
{
int maximum = L & R;
for (int i = L; i < R; i++)
for (int j = i + 1; j <= R; j++)
// Maximum among all (i, j) pairs
maximum = Math.max(maximum, (i & j));
return maximum;
}
// Driver code
public static void main(String[] args)
{
int L = 1, R = 632;
System.out.println(maxAND(L, R));
}
}
// This code contributed by Rajput-Ji
Python3
# Python 3 implementation of the approach
# Function to return the maximum bitwise AND
# possible among all the possible pairs
def maxAND(L, R):
maximum = L & R
for i in range(L, R, 1):
for j in range(i + 1, R + 1, 1):
# Maximum among all (i, j) pairs
maximum = max(maximum, (i & j))
return maximum
# Driver code
if __name__ == '__main__':
L = 1
R = 632
print(maxAND(L, R))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum bitwise AND
// possible among all the possible pairs
static int maxAND(int L, int R)
{
int maximum = L & R;
for (int i = L; i < R; i++)
for (int j = i + 1; j <= R; j++)
// Maximum among all (i, j) pairs
maximum = Math.Max(maximum, (i & j));
return maximum;
}
// Driver code
public static void Main(String[] args)
{
int L = 1, R = 632;
Console.WriteLine(maxAND(L, R));
}
}
// This code has been contributed by 29AjayKumar
PHP
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum bitwise AND
// possible among all the possible pairs
int maxAND(int L, int R)
{
// If there is only a single value
// in the range [L, R]
if (L == R)
return L;
// If there are only two values
// in the range [L, R]
else if ((R - L) == 1)
return (R & L);
else {
if (((R - 1) & R) > ((R - 2) & (R - 1)))
return ((R - 1) & R);
else
return ((R - 2) & (R - 1));
}
}
// Driver code
int main()
{
int L = 1, R = 632;
cout << maxAND(L, R);
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to return the maximum bitwise AND
// possible among all the possible pairs
static int maxAND(int L, int R)
{
// If there is only a single value
// in the range [L, R]
if (L == R)
return L;
// If there are only two values
// in the range [L, R]
else if ((R - L) == 1)
return (R & L);
else {
if (((R - 1) & R) > ((R - 2) & (R - 1)))
return ((R - 1) & R);
else
return ((R - 2) & (R - 1));
}
}
// Driver code
public static void main(String[] args)
{
int L = 1, R = 632;
System.out.println(maxAND(L, R));
}
}
// This code is contributed by
// Prerna Saini
Python3
# Python3 implementation of the approach
# Function to return the maximum bitwise AND
# possible among all the possible pairs
def maxAND(L, R):
# If there is only a single value
# in the range [L, R]
if (L == R):
return L;
# If there are only two values
# in the range [L, R]
elif ((R - L) == 1):
return (R & L);
else:
if (((R - 1) & R) >
((R - 2) & (R - 1))):
return ((R - 1) & R);
else:
return ((R - 2) & (R - 1));
# Driver code
L = 1;
R = 632;
print(maxAND(L, R));
# This code contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GfG
{
// Function to return the maximum bitwise AND
// possible among all the possible pairs
static int maxAND(int L, int R)
{
// If there is only a single value
// in the range [L, R]
if (L == R)
return L;
// If there are only two values
// in the range [L, R]
else if ((R - L) == 1)
return (R & L);
else
{
if (((R - 1) & R) > ((R - 2) & (R - 1)))
return ((R - 1) & R);
else
return ((R - 2) & (R - 1));
}
}
// Driver code
public static void Main()
{
int L = 1, R = 632;
Console.WriteLine(maxAND(L, R));
}
}
// This code is contributed by Ryuga
PHP
(($R - 2) & ($R - 1)))
return (($R - 1) & $R);
else
return (($R - 2) & ($R - 1));
}
}
// Driver code
$L = 1;
$R = 632;
echo maxAND($L, $R);
// This code is contributed by mits
?>
输出:
630
时间复杂度: O(n 2 )
高效方法:如果我们仔细观察[L,R]范围,则(R)和(R – 1)的AND或(R – 1)和(R – 2)的AND之间可能存在最大AND。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum bitwise AND
// possible among all the possible pairs
int maxAND(int L, int R)
{
// If there is only a single value
// in the range [L, R]
if (L == R)
return L;
// If there are only two values
// in the range [L, R]
else if ((R - L) == 1)
return (R & L);
else {
if (((R - 1) & R) > ((R - 2) & (R - 1)))
return ((R - 1) & R);
else
return ((R - 2) & (R - 1));
}
}
// Driver code
int main()
{
int L = 1, R = 632;
cout << maxAND(L, R);
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to return the maximum bitwise AND
// possible among all the possible pairs
static int maxAND(int L, int R)
{
// If there is only a single value
// in the range [L, R]
if (L == R)
return L;
// If there are only two values
// in the range [L, R]
else if ((R - L) == 1)
return (R & L);
else {
if (((R - 1) & R) > ((R - 2) & (R - 1)))
return ((R - 1) & R);
else
return ((R - 2) & (R - 1));
}
}
// Driver code
public static void main(String[] args)
{
int L = 1, R = 632;
System.out.println(maxAND(L, R));
}
}
// This code is contributed by
// Prerna Saini
Python3
# Python3 implementation of the approach
# Function to return the maximum bitwise AND
# possible among all the possible pairs
def maxAND(L, R):
# If there is only a single value
# in the range [L, R]
if (L == R):
return L;
# If there are only two values
# in the range [L, R]
elif ((R - L) == 1):
return (R & L);
else:
if (((R - 1) & R) >
((R - 2) & (R - 1))):
return ((R - 1) & R);
else:
return ((R - 2) & (R - 1));
# Driver code
L = 1;
R = 632;
print(maxAND(L, R));
# This code contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GfG
{
// Function to return the maximum bitwise AND
// possible among all the possible pairs
static int maxAND(int L, int R)
{
// If there is only a single value
// in the range [L, R]
if (L == R)
return L;
// If there are only two values
// in the range [L, R]
else if ((R - L) == 1)
return (R & L);
else
{
if (((R - 1) & R) > ((R - 2) & (R - 1)))
return ((R - 1) & R);
else
return ((R - 2) & (R - 1));
}
}
// Driver code
public static void Main()
{
int L = 1, R = 632;
Console.WriteLine(maxAND(L, R));
}
}
// This code is contributed by Ryuga
的PHP
(($R - 2) & ($R - 1)))
return (($R - 1) & $R);
else
return (($R - 2) & ($R - 1));
}
}
// Driver code
$L = 1;
$R = 632;
echo maxAND($L, $R);
// This code is contributed by mits
?>
输出:
630
时间复杂度: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。