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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxOR( long long arr[], int n, int k, int x)
{
long long preSum[n + 1], suffSum[n + 1];
long long res, pow = 1;
for ( int i = 0; i < k; i++)
pow *= x;
preSum[0] = 0;
for ( int i = 0; i < n; i++)
preSum[i + 1] = preSum[i] | arr[i];
suffSum[n] = 0;
for ( int i = n - 1; i >= 0; i--)
suffSum[i] = suffSum[i + 1] | arr[i];
res = 0;
for ( int i = 0; i < n; i++)
res = max(res, preSum[i] | (arr[i] * pow ) | suffSum[i + 1]);
return res;
}
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
import java.io.*;
class GFG {
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 ;
for ( int i = 0 ; i < k; i++)
pow *= x;
preSum[ 0 ] = 0 ;
for ( int i = 0 ; i < n; i++)
preSum[i + 1 ] = preSum[i] | arr[i];
suffSum[n] = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
suffSum[i] = suffSum[i + 1 ] | arr[i];
res = 0 ;
for ( int i = 0 ; i < n; i++)
res = Math.max(res, preSum[i] |
(arr[i] * pow) | suffSum[i + 1 ]);
return res;
}
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);
}
}
|
Python 3
def maxOR(arr, n, k, x):
preSum = [ 0 ] * (n + 1 )
suffSum = [ 0 ] * (n + 1 )
pow = 1
for i in range ( 0 ,k):
pow * = x
preSum[ 0 ] = 0
for i in range ( 0 , n):
preSum[i + 1 ] = preSum[i] | arr[i]
suffSum[n] = 0
for i in range (n - 1 , - 1 , - 1 ):
suffSum[i] = suffSum[i + 1 ] | arr[i]
res = 0
for i in range ( 0 ,n):
res = max (res, preSum[i] |
(arr[i] * pow ) | suffSum[i + 1 ])
return res
arr = [ 1 , 2 , 4 , 8 ]
n = len (arr)
k = 2
x = 3
print (maxOR(arr, n, k, x))
|
C#
using System;
class GFG {
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;
for ( int i = 0; i < k; i++)
pow *= x;
preSum[0] = 0;
for ( int i = 0; i < n; i++)
preSum[i + 1] = preSum[i] | arr[i];
suffSum[n] = 0;
for ( int i = n - 1; i >= 0; i--)
suffSum[i] = suffSum[i + 1] | arr[i];
res = 0;
for ( int i = 0; i < n; i++)
res = Math.Max(res, preSum[i] |
(arr[i] * pow) | suffSum[i + 1]);
return res;
}
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);
}
}
|
PHP
<?php
function maxOR( $arr , $n , $k , $x )
{
$res ; $pow = 1;
for ( $i = 0; $i < $k ; $i ++)
$pow *= $x ;
$preSum [0] = 0;
for ( $i = 0; $i < $n ; $i ++)
$preSum [ $i + 1] = $preSum [ $i ] |
$arr [ $i ];
$suffSum [ $n ] = 0;
for ( $i = $n - 1; $i >= 0; $i --)
$suffSum [ $i ] = $suffSum [ $i + 1] |
$arr [ $i ];
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
$res = max( $res , $preSum [ $i ] |
( $arr [ $i ] * $pow ) |
$suffSum [ $i + 1]);
return $res ;
}
$arr = array (1, 2, 4, 8);
$n = sizeof( $arr );
$k = 2; $x = 3;
echo maxOR( $arr , $n , $k , $x ), "\n" ;
?>
|
Javascript
<script>
function maxOR(arr, n, k, x)
{
let preSum = new Array(n + 1);
let suffSum = new Array(n + 1);
let res = 0, pow = 1;
for (let i = 0; i < k; i++)
pow *= x;
preSum[0] = 0;
for (let i = 0; i < n; i++)
preSum[i + 1] = preSum[i] | arr[i];
suffSum[n] = 0;
for (let i = n - 1; i >= 0; i--)
suffSum[i] = suffSum[i + 1] | arr[i];
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);
</script>
|
Time Complexity: O(n + k)
Auxiliary Space: O(n)
Last Updated :
10 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...