Maximize the bitwise OR of an array

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to maximize the Bitwise
// OR Sum in given array
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Pyhton 3 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output :

79


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.