📌  相关文章
📜  最大化第一个元素的按位与与给定数组的任何排列的剩余元素的补码

📅  最后修改于: 2021-09-06 06:33:59             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到这个数组的任何排列的第一个元素与剩余元素的补码的按位与的最大值,即

例子:

朴素方法:解决问题的最简单方法是生成给定数组的所有可能排列,并为每个排列找到所需的值并打印其中的最大值。
时间复杂度: O(N * N!)
辅助空间: O(N)

高效的方法:上述方法可以通过以下观察进行优化:

  • 表达式A 1 &(~A 2 ) & (~A 3 ) & …… & (~A n )仅取决于 A 1的值
  • 因此,要从表达式中获得最大的值,请选择 A 1使其具有尽可能最高重要性的设置位,该在所有其他数组元素中均未设置。
  • 由于剩余数组元素的顺序无关紧要,因此打印任何以获得的 A 1作为第一个元素的排列。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
#define size_int 32
 
// Function to maximize the value for
// the given function and the array elements
int functionMax(int arr[], int n)
{
    // Vector array to maintain which bit is set
    // for which integer in the given array by
    // saving index of that integer
    vector setBit[32];
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < size_int; j++) {
 
            // Check if j-th bit is set for
            // i-th integer
            if (arr[i] & (1 << j))
 
                // Push the index of that
                // integer in setBit[j]
                setBit[j].push_back(i);
        }
    }
 
    // Find the element having
    // highest significant set bit
    // unset in other elements
    for (int i = size_int; i >= 0; i--) {
        if (setBit[i].size() == 1) {
 
            // Place that integer at 0-th index
            swap(arr[0], arr[setBit[i][0]]);
            break;
        }
    }
 
    // Store the maximum AND value
    int maxAnd = arr[0];
    for (int i = 1; i < n; i++) {
        maxAnd = maxAnd & (~arr[i]);
    }
 
    // Return the answer
    return maxAnd;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 4, 8, 16 };
    int n = sizeof arr / sizeof arr[0];
 
    // Function call
    cout << functionMax(arr, n);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
static final int size_int = 32;
 
// Function to maximize the value for
// the given function and the array elements
static int functionMax(int arr[], int n)
{
    // Vector array to maintain which bit is set
    // for which integer in the given array by
    // saving index of that integer
    Vector []setBit = new Vector[32 + 1];
    for (int i = 0; i < setBit.length; i++)
        setBit[i] = new Vector();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < size_int; j++)
        {
 
            // Check if j-th bit is set for
            // i-th integer
            if ((arr[i] & (1 << j)) > 0)
 
                // Push the index of that
                // integer in setBit[j]
                setBit[j].add(i);
        }
    }
 
    // Find the element having
    // highest significant set bit
    // unset in other elements
    for (int i = size_int; i >= 0; i--)
    {
        if (setBit[i].size() == 1)
        {
 
            // Place that integer at 0-th index
            swap(arr, 0, setBit[i].get(0));
            break;
        }
    }
 
    // Store the maximum AND value
    int maxAnd = arr[0];
    for (int i = 1; i < n; i++)
    {
        maxAnd = maxAnd & (~arr[i]);
    }
 
    // Return the answer
    return maxAnd;
}
   
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
}
   
// Driver Code
public static void main(String[] args)
{
 
    int arr[] = { 1, 2, 4, 8, 16 };
    int n = arr.length;
 
    // Function call
    System.out.print(functionMax(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python 3 Program to
# implement the above approach
 
# Function to maximize the
# value for the given function
# and the array elements
def functionMax(arr, n):
   
    # Vector array to maintain
    # which bit is set for which
    # integer in the given array by
    # saving index of that integer
    setBit = [[] for i in range(32)]
 
    for i in range(n):
        for j in range(32):
           
            # Check if j-th bit is
            # set for i-th integer
            if (arr[i] & (1 << j)):
               
                # Push the index of that
                # integer in setBit[j]
                setBit[j].append(i)
 
    # Find the element having
    # highest significant set bit
    # unset in other elements
    i = 31
     
    while(i >= 0):
        if (len(setBit[i]) == 1):
           
            # Place that integer
            # at 0-th index
            temp = arr[0]
            arr[0] = arr[setBit[i][0]]
            arr[setBit[i][0]] = temp
            break
        i -= 1
 
    # Store the maximum
    # AND value
    maxAnd = arr[0]
    for i in range(1, n, 1):
        maxAnd = (maxAnd & (~arr[i]))
 
    # Return the answer
    return maxAnd
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 4, 8, 16]
    n = len(arr)
 
    # Function call
    print(functionMax(arr, n))
 
# This code is contributed by bgangwar59


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static readonly int size_int = 32;
 
// Function to maximize the value for
// the given function and the array elements
static int functionMax(int []arr, int n)
{
    // List array to maintain which bit is set
    // for which integer in the given array by
    // saving index of that integer
    List []setBit = new List[32 + 1];
    for (int i = 0; i < setBit.Length; i++)
        setBit[i] = new List();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < size_int; j++)
        {
 
            // Check if j-th bit is set for
            // i-th integer
            if ((arr[i] & (1 << j)) > 0)
 
                // Push the index of that
                // integer in setBit[j]
                setBit[j].Add(i);
        }
    }
 
    // Find the element having
    // highest significant set bit
    // unset in other elements
    for (int i = size_int; i >= 0; i--)
    {
        if (setBit[i].Count == 1)
        {
 
            // Place that integer at 0-th index
            swap(arr, 0, setBit[i][0]);
            break;
        }
    }
 
    // Store the maximum AND value
    int maxAnd = arr[0];
    for (int i = 1; i < n; i++)
    {
        maxAnd = maxAnd & (~arr[i]);
    }
 
    // Return the answer
    return maxAnd;
}
   
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
}
   
// Driver Code
public static void Main(String[] args)
{
 
    int []arr = { 1, 2, 4, 8, 16 };
    int n = arr.Length;
 
    // Function call
    Console.Write(functionMax(arr, n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
16

时间复杂度: O(N * sizeof(int)),其中 sizeof(int) 为 32
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live