📜  数组中一对的最大AND值

📅  最后修改于: 2021-04-29 06:22:43             🧑  作者: Mango

我们得到了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}的第一个示例:

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是数组的大小。