📜  在数组中打印具有最大AND值的对

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

给定一个由n个正元素组成的数组,请找到最大AND值,并从该数组中生成最大AND值的一对元素。
AND是按位&运算符。
例子:

Input : arr[] = {4, 8, 12, 16}
Output : Pair = 8, 12
         Maximum AND value = 8
         
Input : arr[] = {4, 8, 16, 2}
Output : Pair = Not Possible
         Maximum AND value = 0

方法:
查找最大AND值与数组中的最大AND值相同。我们的任务是找到导致获得AND值的一对元素。为了找到元素,只需遍历整个数组并找到具有最大AND值(结果)的每个元素的AND值,如果arr [i]&result == result ,则意味着arr [i]是将产生最大AND值。同样,如果最大AND值(结果)为零,那么在这种情况下,我们应该打印“ Not不可能”。
下面是上述方法的实现:

C++
// CPP Program to find pair with
// maximum AND value
#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);
    }
     
    // Find the elements
    if (res == 0)
        cout << "Not Possible\n";
 
    else {
 
        // print the pair of elements
        cout << "Pair = ";
     
        count = 0;
     
        for (int i = 0; i < n && count < 2; i++) {
     
            // inc count value after
            // printing element
            if ((arr[i] & res) == res) {
                count++;
                cout << arr[i] << " ";
            }
        }
    }
     
    // return the result value
    return res;
}
 
// Driver function
int main()
{
    int arr[] = { 4, 8, 6, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "\nMaximum AND Value = "
         << maxAND(arr, n);
    return 0;
}


Java
// Java Program to find pair
// with maximum AND value
import java.util.*;
import java.io.*;
 
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);
    }
     
    // Find the elements
    if (res == 0)
        System.out.println("Not Possible");
 
    else {
 
        // print the pair of elements
        System.out.print("Pair = ");
     
        count = 0;
     
        for (int i = 0; i < n && count < 2; i++) {
     
            // inc count value after
            // printing element
            if ((arr[i] & res) == res) {
                count++;
                System.out.print(arr[i] + " ");
            }
        }
        System.out.println();
    }
     
    // return the result value
    return res;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 4, 8, 6, 2 };
    int n = arr.length;
    System.out.println("Maximum AND Value = "
                                + maxAND(arr, n));
 
}
}
 
// This code is contributed by Sahil_Bansall


Python 3
# Python 3 Program to find pair with
# maximum AND value
 
# 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 += 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 |= (1 << bit)
     
    # Find the elements
    if (res == 0):
        print("Not Possible")
 
    else:
        # print the pair of elements
        print("Pair = ", end = "")
     
        count = 0
     
        i = 0
        while(i < n and count < 2):
     
            # inc count value after
            # printing element
            if ((arr[i] & res) == res) :
                count+=1
                print(arr[i] , end = " ")
            i += 1
         
    # return the result value
    return res
 
# Driver function
arr = [4, 8, 6, 2 ]
n = len(arr)
print("\nMaximum AND Value = ",
                    maxAND(arr, n))
 
# This code is contributed by Smitha


C#
// C# Program to find pair
// with maximum AND value
using System;
 
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);
        }
         
        // Find the elements
        if (res == 0)
            Console.Write("Not Possible");
     
        else {
     
            // print the pair of elements
            Console.Write("Pair = ");
         
            count = 0;
         
            for (int i = 0; i < n && count < 2; i++)
            {
         
                // inc count value after
                // printing element
                if ((arr[i] & res) == res) {
                    count++;
                    Console.Write(arr[i] + " ");
                }
            }
            Console.WriteLine();
        }
         
        // return the result value
        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);
    }
     
    // Find the elements
    if ($res == 0)
        echo "Not Possible\n";
 
    else {
 
        // print the pair of elements
        echo "Pair = ";
     
        $count = 0;
     
        for ($i = 0; $i < $n &&
             $count < 2; $i++)
        {
     
            // inc count value after
            // printing element
            if (($arr[$i] & $res) == $res)
            {
                $count++;
                echo $arr[$i]. " ";
            }
        }
    }
     
    // return the result value
    return $res;
}
 
    // Driver code
    $arr = array( 4, 8, 6, 2 );
    $n = sizeof($arr) / sizeof($arr[0]);
    echo "\nMaximum AND Value = " .maxAND($arr, $n);
 
//This code is contributed by mits
?>


Javascript


输出:

Pair = 4 6
Maximum AND value = 4