📌  相关文章
📜  通过最多替换一个元素来最大化数组的按位与

📅  最后修改于: 2022-05-13 01:56:07.079000             🧑  作者: Mango

通过最多替换一个元素来最大化数组的按位与

给定一个包含N个正整数的数组arr[] ,任务是通过选择arr []的最多一个元素并将其递增或递减任意值来最大化 arr[] 的按位

例子:

方法:这个问题的问题陈述说通过替换arr[] 中的几乎一个元素来最大化 arr[]AND。解决这个问题的蛮力方法是一次对arr[]中除一个之外的所有元素进行按位与,并为每次在 arr[] 的整个按位与中未添加贡献的元素找到适当的替换.对arr[]中的所有元素执行此操作并找到最佳结果。

下面是上述方法的实现。

C++
#include 
using namespace std;
 
// Function to find maximum AND by
// Replacing atmost one element by any value
int maxAnd(int n, vector a)
{
    // To Calculate answer
    int ans = 0;
 
    // Iterating through the array
    for (int i = 0; i < n; i++) {
        int max_and = 0XFFFFFFFF;
 
        // Checking and for element and
        // Leaving only jth element
        for (int j = 0; j < n; j++) {
            if (i != j) {
                max_and = max_and & a[j];
            }
        }
 
        // Comparing previous answers
        // and max answers
        ans = max(ans, max_and);
    }
    return ans;
}
 
// Driver Code
int main()
{
    int N = 3;
    vector arr = { 1, 2, 3 };
 
    // Function Call
    cout << maxAnd(N, arr) << "\n";
}


Java
import java.io.*;
 
class GFG{
   
// Function to find maximum AND by
// Replacing atmost one element by any value
static int maxAnd(int n,int[] a)
{
     
    // To Calculate answer
    int ans = 0;
 
    // Iterating through the array
    for(int i = 0; i < n; i++)
    {
        int max_and = 0XFFFFFFFF;
 
        // Checking and for element and
        // Leaving only jth element
        for(int j = 0; j < n; j++)
        {
            if (i != j)
            {
                max_and = max_and & a[j];
            }
        }
 
        // Comparing previous answers
        // and max answers
        ans = Math.max(ans, max_and);
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    int[] arr = { 1, 2, 3 };
     
    // Function Call
    System.out.println( maxAnd(N, arr) );
}
}
 
// This code is contributed by Potta Lokesh


Python
# Function to find maximum AND by
# Replacing atmost one element by any value
def maxAnd(n, a):
     
    # To Calculate answer
    ans = 0
 
    # Iterating through the array
    for i in range(0, n):
        max_and = 0XFFFFFFFF
 
        # Checking and for element and
        # Leaving only jth element
        for j in range(0, n):
            if (i != j):
                max_and = max_and & a[j]
 
        # Comparing previous answers
        # and max answers
        ans = max(ans, max_and)
         
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    N = 3
    arr = [ 1, 2, 3 ]
     
    print(maxAnd(N, arr))
     
    # This code is contributed by Samim Hossain Mondal.


C#
using System;
 
class GFG{
   
// Function to find maximum AND by
// Replacing atmost one element by any value
static int maxAnd(int n,int[] a)
{
     
    // To Calculate answer
    int ans = 0;
 
    // Iterating through the array
    for(int i = 0; i < n; i++)
    {
        uint max_and = 0XFFFFFFFF;
 
        // Checking and for element and
        // Leaving only jth element
        for(int j = 0; j < n; j++)
        {
            if (i != j)
            {
                max_and = Convert.ToUInt32(max_and & a[j]);
            }
        }
 
        // Comparing previous answers
        // and max answers
        ans = Math.Max(ans, (int)max_and);
    }
    return ans;
}
 
// Driver Code
public static void Main()
{
    int N = 3;
    int[] arr = { 1, 2, 3 };
     
    // Function Call
    Console.Write( maxAnd(N, arr) );
}
}
 
// This code is contributed by gfgking


Javascript


输出:
2

时间复杂度: O(N^2)
辅助空间: O(1)