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++
// 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; } |
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
# 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 = 2x = 3print(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
<?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 ?> |
79
Recommended Posts:
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Maximize the median of the given array after adding K elements to the same array
- Maximize the sum of array by multiplying prefix of array with -1
- Maximize value of (arr[i] - i) - (arr[j] - j) in an array
- Maximize value of (a[i]+i)*(a[j]+j) in an array
- Maximize array sum after K negations | Set 1
- Maximize array sum after K negations | Set 2
- Maximize elements using another array
- Maximize the median of an array
- Given an array and three numbers, maximize (x * a[i]) + (y * a[j]) + (z * a[k])
- Rearrange an array to maximize i*arr[i]
- Remove an element to maximize the GCD of the given array
- Remove array end element to maximize the sum of product
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.



