我们得到了n个正元素的数组。我们需要找到数组中任何一对元素生成的最大AND值。 AND是按位&运算符。
例子:
Input : arr[] = {4, 8, 12, 16}
Output : Maximum AND value = 8
Input : arr[] = {4, 8, 16, 2}
Output : Maximum AND value = 0
天真的方法:基本方法与最大异或值相同。我们遍历所有可能的对,并计算所有这些对的AND值。选择其中最大的价值。该解决方案的时间复杂度为O(n ^ 2)。
C++
// CPP Program to find maximum XOR value of a pair
#include
using namespace std;
// Function for finding maximum and value pair
int maxAND(int arr[], int n)
{
int res = 0;
for (int i=0; iJava
// Java Program to find maximum
// XOR value of a pair
import java.util.*;
import java.lang.*;
public class GfG{
// Function for finding maximum
// and value pair
static int maxAND(int arr[], int n)
{
int res = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
res = res > ( arr[i] & arr[j]) ?
res : ( arr[i] & arr[j]);
return res;
}
// driver function
public static void main(String argc[])
{
int arr[] = {4, 8, 6, 2};
int n = arr.length;
System.out.println("Maximum AND Value = " +
maxAND(arr,n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 Program to find maximum XOR
# value of a pair
# Function for finding maximum and value pair
def maxAND(arr, n) :
res = 0
for i in range(0, n) :
for j in range(i + 1, n) :
res = max(res, arr[i] & arr[j])
return res
# Driver function
arr = [4, 8, 6, 2]
n = len(arr)
print("Maximum AND Value = ", maxAND(arr,n))
# This code is contributed by Nikita Tiwari.
C#
// C# Program to find maximum
// XOR value of a pair
using System;
public class GfG
{
// Function for finding maximum
// and value pair
static int maxAND(int []arr, int n)
{
int res = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
res = res > ( arr[i] & arr[j]) ?
res : ( arr[i] & arr[j]);
return res;
}
// Driver code
public static void Main()
{
int []arr = {4, 8, 6, 2};
int n = arr.Length;
Console.WriteLine("Maximum AND Value = " +
maxAND(arr, n));
}
}
// This code is contributed by vt_m.
PHP
C++
// CPP Program to find maximum XOR value of a pair
#include
using namespace std;
// Utility function to check number of elements
// having set msb as of pattern
int checkBit(int pattern, int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum and value pair
int maxAND (int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element having set msb
count = checkBit(res | (1 << bit),arr,n);
// if count >= 2 set particular bit in result
if ( count >= 2 )
res |= (1 << bit);
}
return res;
}
// Driver function
int main()
{
int arr[] = {4, 8, 6, 2};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Maximum AND Value = " << maxAND(arr,n);
return 0;
}
Java
// Java Program to find maximum
// XOR value of a pair
import java.util.*;
import java.lang.*;
public class GfG{
// Utility function to check number of elements
// having set msb as of pattern
static int checkBit(int pattern, int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum and value pair
static int maxAND (int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if ( count >= 2 )
res |= (1 << bit);
}
return res;
}
// driver function
public static void main(String argc[])
{
int arr[] = {4, 8, 6, 2};
int n = arr.length;
System.out.println("Maximum AND Value = " +
maxAND(arr, n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 Program to find maximum XOR
# value of a pair
# Utility function to check number of
# elements having set msb as of pattern
def checkBit(pattern,arr, n) :
count = 0
for i in range(0, n) :
if ((pattern & arr[i]) == pattern) :
count = count + 1
return count
# Function for finding maximum and
# value pair
def maxAND (arr, n) :
res = 0
# iterate over total of 30bits
# from msb to lsb
for bit in range(31,-1,-1) :
# find the count of element
# having set msb
count = checkBit(res | (1 << bit), arr, n)
# if count >= 2 set particular
# bit in result
if ( count >= 2 ) :
res =res | (1 << bit)
return res
# Driver function
arr = [4, 8, 6, 2]
n = len(arr)
print("Maximum AND Value = ", maxAND(arr, n))
# This code is contributed by Nikita Tiwari
C#
// C# Program to find maximum
// XOR value of a pair
using System;
public class GfG
{
// Utility function to check
// number of elements having
// set msb as of pattern
static int checkBit(int pattern,
int []arr,
int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum
// and value pair
static int maxAND (int []arr, int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if (count >= 2)
res |= (1 << bit);
}
return res;
}
// Driver Code
public static void Main()
{
int []arr = {4, 8, 6, 2};
int n = arr.Length;
Console.WriteLine("Maximum AND Value = " +
maxAND(arr, n));
}
}
// This code is contributed by vt_m.
PHP
= 0; $bit--)
{
// find the count of element
// having set msb
$count = checkBit($res | (1 << $bit),
$arr, $n);
// if count >= 2 set particular
// bit in result
if ( $count >= 2 )
$res |= (1 << $bit);
}
return $res;
}
// Driver Code
$arr = array(4, 8, 6, 2);
$n = count($arr);
echo "Maximum AND Value = " , maxAND($arr,$n);
// This code is contributed by vt_m.
?>
输出:
Maximum AND Value = 4
更好的方法:想法基于AND运算符的属性。如果两个位都为1,则任何两个位的AND运算结果均为1。我们从MSB开始,检查是否具有设置值的数组的两个元素中的最小值。如果是,则该MSB将成为我们解决方案的一部分并被添加到结果中,否则我们将丢弃该位。同样,从MSB到LSB(32:1)进行位位置迭代,我们可以轻松地检查哪个位将成为解决方案的一部分,并将所有此类位添加到我们的解决方案中。
说明:让我们考虑{4,8,12,16}的第一个示例:
step 1: Write Bit-representation of each element :
4 = 100, 8 = 1000, 12 = 1100, 16 = 10000
step 2: Check for 1st MSB , pattern = 0 + 16 = 16. Now 5th bit in 16 is set but no other element has 5-bit as set bit so this will not add up to our RES, still RES = 0 and pattern = 0
step 3: Check 4th bit, pattern = 0 + 8 = 8. Now 8 and 12 both have set bit on 4th bit position so that will add up in our solution, RES = 8 and pattern = 8
step 4: Check 3rd bit, pattern = 8 + 4 = 12. Now only 12 has both bits set bit (same as pattern) so we will discard 3rd bit, RES = 8 and pattern = 8
step 5: Check 2nd bit, pattern = 8 + 2 = 10. No element has set bit same as pattern so we will discard 2nd bit, RES = 8 and pattern = 8
step 4: Check 1st bit, pattern = 8 + 1 = 9. No element has set bit same as pattern so we will discard 1st bit, RES = 8 and pattern = 8
C++
// CPP Program to find maximum XOR value of a pair
#include
using namespace std;
// Utility function to check number of elements
// having set msb as of pattern
int checkBit(int pattern, int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum and value pair
int maxAND (int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element having set msb
count = checkBit(res | (1 << bit),arr,n);
// if count >= 2 set particular bit in result
if ( count >= 2 )
res |= (1 << bit);
}
return res;
}
// Driver function
int main()
{
int arr[] = {4, 8, 6, 2};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Maximum AND Value = " << maxAND(arr,n);
return 0;
}
Java
// Java Program to find maximum
// XOR value of a pair
import java.util.*;
import java.lang.*;
public class GfG{
// Utility function to check number of elements
// having set msb as of pattern
static int checkBit(int pattern, int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum and value pair
static int maxAND (int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if ( count >= 2 )
res |= (1 << bit);
}
return res;
}
// driver function
public static void main(String argc[])
{
int arr[] = {4, 8, 6, 2};
int n = arr.length;
System.out.println("Maximum AND Value = " +
maxAND(arr, n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 Program to find maximum XOR
# value of a pair
# Utility function to check number of
# elements having set msb as of pattern
def checkBit(pattern,arr, n) :
count = 0
for i in range(0, n) :
if ((pattern & arr[i]) == pattern) :
count = count + 1
return count
# Function for finding maximum and
# value pair
def maxAND (arr, n) :
res = 0
# iterate over total of 30bits
# from msb to lsb
for bit in range(31,-1,-1) :
# find the count of element
# having set msb
count = checkBit(res | (1 << bit), arr, n)
# if count >= 2 set particular
# bit in result
if ( count >= 2 ) :
res =res | (1 << bit)
return res
# Driver function
arr = [4, 8, 6, 2]
n = len(arr)
print("Maximum AND Value = ", maxAND(arr, n))
# This code is contributed by Nikita Tiwari
C#
// C# Program to find maximum
// XOR value of a pair
using System;
public class GfG
{
// Utility function to check
// number of elements having
// set msb as of pattern
static int checkBit(int pattern,
int []arr,
int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum
// and value pair
static int maxAND (int []arr, int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--)
{
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if (count >= 2)
res |= (1 << bit);
}
return res;
}
// Driver Code
public static void Main()
{
int []arr = {4, 8, 6, 2};
int n = arr.Length;
Console.WriteLine("Maximum AND Value = " +
maxAND(arr, n));
}
}
// This code is contributed by vt_m.
的PHP
= 0; $bit--)
{
// find the count of element
// having set msb
$count = checkBit($res | (1 << $bit),
$arr, $n);
// if count >= 2 set particular
// bit in result
if ( $count >= 2 )
$res |= (1 << $bit);
}
return $res;
}
// Driver Code
$arr = array(4, 8, 6, 2);
$n = count($arr);
echo "Maximum AND Value = " , maxAND($arr,$n);
// This code is contributed by vt_m.
?>
输出:
Maximum AND Value = 4
时间复杂度:O(n * log(m))其中m是数组中的最大元素,n是数组的大小。