📌  相关文章
📜  最大化数组的按位或

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

给定一个由N个整数组成的数组。必须通过执行一项任务来最大化阵列中所有元素的按位“或”。任务是将数组的任何元素最多k次与给定的整数x相乘。
例子 :

方法:预计算前缀和后缀OR数组。
在一次迭代中,将一个元素与x ^ k相乘,并对其进行前缀OR(即,所有先前元素的按位OR)和后缀OR(即所有后续元素的按位OR)的按位或运算,并在所有迭代之后返回最大值。

C++
// C++ program to maximize the Bitwise
// OR Sum in given array
#include 
using namespace std;
 
// Function to maximize the bitwise
// OR sum
int maxOR(long long arr[], int n, int k, int x)
{
    long long preSum[n + 1], suffSum[n + 1];
    long long res, pow = 1;
 
    // Compute x^k
    for (int i = 0; i < k; i++)
        pow *= x;
 
    // Find prefix bitwise OR
    preSum[0] = 0;
    for (int i = 0; i < n; i++)
        preSum[i + 1] = preSum[i] | arr[i];
 
    // Find suffix bitwise OR
    suffSum[n] = 0;
    for (int i = n - 1; i >= 0; i--)
        suffSum[i] = suffSum[i + 1] | arr[i];
 
    // Find maximum OR  value
    res = 0;
    for (int i = 0; i < n; i++)
        res = max(res, preSum[i] | (arr[i] * pow) | suffSum[i + 1]);
 
    return res;
}
 
// Drivers code
int main()
{
    long long arr[] = { 1, 2, 4, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2, x = 3;
 
    cout << maxOR(arr, n, k, x) << "\n";
 
    return 0;
}


Java
// Java program to maximize the Bitwise
// OR Sum in given array
import java.io.*;
 
class GFG {
     
    // Function to maximize the bitwise OR sum
    public static long maxOR(long arr[], int n,
                                  int k, int x)
    {
        long preSum[] = new long[n + 1];
        long suffSum[] = new long[n + 1];
        long res = 0, pow = 1;
 
        // Compute x^k
        for (int i = 0; i < k; i++)
            pow *= x;
 
        // Find prefix bitwise OR
        preSum[0] = 0;
        for (int i = 0; i < n; i++)
            preSum[i + 1] = preSum[i] | arr[i];
 
        // Find suffix bitwise OR
        suffSum[n] = 0;
        for (int i = n - 1; i >= 0; i--)
            suffSum[i] = suffSum[i + 1] | arr[i];
 
        // Find maximum OR value
        res = 0;
        for (int i = 0; i < n; i++)
            res = Math.max(res, preSum[i] |
                (arr[i] * pow) | suffSum[i + 1]);
 
        return res;
    }
 
    // Drivers code
    public static void main(String args[])
    {
        long arr[] = { 1, 2, 4, 8 };
        int n = 4;
        int k = 2, x = 3;
         
        long ans = maxOR(arr, n, k, x);
        System.out.println(ans);
    }
}
 
// This code is contributed by Jaideep Pyne


Python 3
# Python3 program to maximize the Bitwise
# OR Sum in given array
 
# Function to maximize the bitwise
# OR sum
def maxOR(arr, n, k, x):
 
    preSum = [0] * (n + 1)
    suffSum = [0] * (n + 1)
    pow = 1
 
    # Compute x^k
    for i in range(0 ,k):
        pow *= x
 
    # Find prefix bitwise OR
    preSum[0] = 0
    for i in range(0, n):
        preSum[i + 1] = preSum[i] | arr[i]
 
    # Find suffix bitwise OR
    suffSum[n] = 0
    for i in range(n-1, -1, -1):
        suffSum[i] = suffSum[i + 1] | arr[i]
 
    # Find maximum OR value
    res = 0
    for i in range(0 ,n):
        res = max(res, preSum[i] |
           (arr[i] * pow) | suffSum[i + 1])
 
    return res
 
# Drivers code
arr = [1, 2, 4, 8 ]
n = len(arr)
k = 2
x = 3
print(maxOR(arr, n, k, x))
 
# This code is contributed by Smitha


C#
// C# program to maximize the Bitwise
// OR Sum in given array
using System;
 
class GFG {
     
    // Function to maximize the bitwise OR sum
    public static long maxOR(long []arr, int n,
                                  int k, int x)
    {
        long []preSum = new long[n + 1];
        long []suffSum = new long[n + 1];
        long res = 0, pow = 1;
 
        // Compute x^k
        for (int i = 0; i < k; i++)
            pow *= x;
 
        // Find prefix bitwise OR
        preSum[0] = 0;
        for (int i = 0; i < n; i++)
            preSum[i + 1] = preSum[i] | arr[i];
 
        // Find suffix bitwise OR
        suffSum[n] = 0;
        for (int i = n - 1; i >= 0; i--)
            suffSum[i] = suffSum[i + 1] | arr[i];
 
        // Find maximum OR value
        res = 0;
        for (int i = 0; i < n; i++)
            res = Math.Max(res, preSum[i] |
                (arr[i] * pow) | suffSum[i + 1]);
 
        return res;
    }
 
    // Drivers code
    public static void Main()
    {
        long []arr = { 1, 2, 4, 8 };
        int n = 4;
        int k = 2, x = 3;
         
        long ans = maxOR(arr, n, k, x);
        Console.Write(ans);
    }
}
 
// This code is contributed by Smitha


PHP
= 0; $i--)
        $suffSum[$i] = $suffSum[$i + 1] |
                                $arr[$i];
 
    // Find maximum OR value
    $res = 0;
    for ($i = 0; $i < $n; $i++)
        $res = max($res, $preSum[$i] |
                   ($arr[$i] * $pow) |
                    $suffSum[$i + 1]);
 
    return $res;
}
 
// Driver Code
$arr = array(1, 2, 4, 8);
$n = sizeof($arr);
$k = 2; $x = 3;
 
echo maxOR($arr, $n, $k, $x),"\n";
 
// This code is contributed by jit_t
?>


Javascript


输出 :
79

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。