Maximize the bitwise OR of an array - GeeksforGeeks

05 May, 2021
Given an array of N integers. The bitwise OR of all the elements of the array has to be maximized by performing one task. The task is to multiply any element of the array at-most k times with a given integer x.Examples :  
Input: a = {1, 1, 1}, k = 1, x = 2 Output: 3 Explanation: Any possible choice of doing one element of the array will result the same three numbers 1, 1, 2. So, the result is 1 | 1 | 2 = 3.Input: a = {1, 2, 4, 8}, k = 2, x = 3 Output: 79 
 
Approach: Precompute the prefix and suffix OR arrays. In one iteration, multiply an element with x^k and do Bitwise OR it with prefix OR i.e. Bitwise OR of all previous elements and suffix OR i.e. Bitwise OR of all next elements and return the maximum value after all iterations. 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to maximize the Bitwise// OR Sum in given array#include <bits/stdc++.h>using namespace std; // Function to maximize the bitwise// OR sumint 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 codeint 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 program to maximize the Bitwise// OR Sum in given arrayimport 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
# Python3 program to maximize the Bitwise# OR Sum in given array # Function to maximize the bitwise# OR sumdef 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 codearr = [1, 2, 4, 8 ]n = len(arr)k = 2x = 3print(maxOR(arr, n, k, x)) # This code is contributed by Smitha
// C# program to maximize the Bitwise// OR Sum in given arrayusing 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// PHP program to maximize the// Bitwise OR Sum in given array // Function to maximize// the bitwise OR sum function maxOR($arr, $n, $k, $x){    $res; $pow = 1;     // Compute x^k    for ($i = 0; $i < $k; $i++)        $pow *= $x;     // Find prefix bitwise OR    $preSum[0] = 0;    for ($i = 0; $i < $n; $i++)        $preSum[$i + 1] = $preSum[$i] |                                  $arr[$i];     // Find suffix bitwise OR    $suffSum[$n] = 0;    for ($i = $n - 1; $i >= 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?>
<script>    // Javascript program to maximize the Bitwise OR Sum in given array         // Function to maximize the bitwise OR sum    function maxOR(arr, n, k, x)    {        let preSum = new Array(n + 1);        let suffSum = new Array(n + 1);        let res = 0, pow = 1;           // Compute x^k        for (let i = 0; i < k; i++)            pow *= x;           // Find prefix bitwise OR        preSum[0] = 0;        for (let i = 0; i < n; i++)            preSum[i + 1] = preSum[i] | arr[i];           // Find suffix bitwise OR        suffSum[n] = 0;        for (let i = n - 1; i >= 0; i--)            suffSum[i] = suffSum[i + 1] | arr[i];           // Find maximum OR value        res = 0;        for (let i = 0; i < n; i++)            res = Math.max(res, preSum[i] | (arr[i] * pow) | suffSum[i + 1]);           return res;    }         let arr = [ 1, 2, 4, 8 ];    let n = 4;    let k = 2, x = 3;     let ans = maxOR(arr, n, k, x);    document.write(ans); // This code is contributed by suresh07.</script>
79
 
jaideeppyne1997
jit_t
Smitha Dinesh Semwal
nidhi_biet
suresh07
Bitwise-OR
Arrays
Bit Magic
Competitive Programming
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?