Maximize Bitwise OR of Array by incrementing elements by at most K
Last Updated :
08 Apr, 2023
Given an array arr[], and an integer K, the task is to maximize the bitwise OR of the array arr[], where each element of arr[] can be incremented by almost K.
Examples:
Input: arr[]= {1, 3, 7, 0, 6, 1}, K = 2
Output: [1 3 8 0 6 1]
Explanation: Increase the number 7 by 1 ie 8 after that or of the array is maximum that is 15
Input: arr[] ={2, 4, 7, 9}, K = 2
Output: [2, 4, 7, 9]
Explanation: The bitwise OR of arr[] is 15 already so no change is required. 15 is the maximum possible OR.
Approach: This problem can be solved by using Bitwise Operations. Follow the steps below to solve the given problem.
- Find the bitwise or of all the elements, that would tell the status of each bit.
- Start from MSB (Most significant bit), because MSB makes a lot of change in the final bitwise OR.
- If the particular bit of bitwise or is not set. Find the minimum steps to set that bit.
- Minimum Steps to set the ith bit is (1<<i)-((1<<i)-1) & arr[i].
- Count the minimum steps and update the array if the minimum steps are smaller than equal to K.
Below is the implementation of the above approach.
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
void MaximizeBitwiseOR(long a[], int k, int n)
{
// For storing initial
// bitwise OR of array arr[]
long oris = 0;
long bitwiseOr = 0;
for (int i = 0; i < n; ++i)
{
bitwiseOr |= a[i];
}
for (int i = 60; i >= 0; i--)
{
if (((1L << i) & bitwiseOr) == 0)
{
long minSteps = LONG_MAX;
int mini = -1;
for (int j = 0; j < n; j++)
{
long y = ((1L << i) - 1) & a[j];
long steps = (1L << i) - y;
if (steps <= k && steps < minSteps)
{
minSteps = steps;
mini = j;
}
}
if (mini != -1)
{
k -= minSteps;
a[mini] += minSteps;
long orr = 0;
for (int j = 0; j < n; j++)
{
orr |= a[j];
}
bitwiseOr = orr;
}
}
}
// Print the required result
for (long elements = 0; elements < n; elements++)
{
if(a[elements]>0)
cout << a[elements] << " ";
else
cout<<0<<" ";
}
}
// Driver code
int main()
{
int N = 6;
int K = 2;
long arr[] = {1, 3, 7, 0, 6, 1};
MaximizeBitwiseOR(arr, K, N);
}
// This code is contributed by Potta Lokesh
Java
import java.io.*;
class Main {
static void MaximizeBitwiseOR(long[] a, int k, int n)
{
// For storing initial
// bitwise OR of array arr[]
long or = 0;
long bitwiseOr = 0;
for (int i = 0; i < a.length; ++i) {
bitwiseOr |= a[i];
}
for (int i = 60; i >= 0; i--) {
if (((1L << i) & bitwiseOr) == 0) {
long minSteps = Long.MAX_VALUE;
int mini = -1;
for (int j = 0; j < n; j++) {
long y = ((1L << i) - 1) & a[j];
long steps = (1L << i) - y;
if (steps <= k
&& steps < minSteps) {
minSteps = steps;
mini = j;
}
}
if (mini != -1) {
k -= minSteps;
a[mini] += minSteps;
long orr = 0;
for (int j = 0; j < n; j++) {
orr |= a[j];
}
bitwiseOr = orr;
}
}
}
// Print the required result
for (long elements : a) {
System.out.print(elements + " ");
}
}
// Driver code
public static void main(String[] args)
{
int N = 6;
int K = 2;
long arr[] = { 1, 3, 7, 0, 6, 1 };
MaximizeBitwiseOR(arr, K, N);
}
}
Python3
# Python3 code for the above approach
import sys
def MaximizeBitwiseOR(a, k, n):
# For storing initial
# bitwise OR of array arr[]
oris = 0
bitwiseOr = 0
for i in range(n):
bitwiseOr |= a[i]
for i in range(60, -1, -1):
if (((1 << i) & bitwiseOr) == 0):
minSteps = sys.maxsize
mini = -1
for j in range(n):
y = ((1 << i) - 1) & a[j]
steps = (1 << i) - y
if (steps <= k and steps < minSteps):
minSteps = steps
mini = j
if (mini != -1):
k -= minSteps
a[mini] += minSteps
orr = 0
for j in range(n):
orr |= a[j]
bitwiseOr = orr
# Print the required result
for elements in range(n):
if(a[elements] > 0):
print(a[elements], end=" ")
else:
print(0, end=" ")
# Driver code
if __name__ == "__main__":
N = 6
K = 2
arr = [1, 3, 7, 0, 6, 1]
MaximizeBitwiseOR(arr, K, N)
# This code is contributed by ukasp.
C#
using System;
class GFG {
static void MaximizeBitwiseOR(long[] a, int k, int n)
{
// For storing initial
// bitwise OR of array arr[]
long or = 0;
long bitwiseOr = 0;
for (int i = 0; i < a.Length; ++i) {
bitwiseOr |= a[i];
}
for (int i = 60; i >= 0; i--) {
if (((1L << i) & bitwiseOr) == 0) {
long minSteps = Int32.MaxValue;
int mini = -1;
for (int j = 0; j < n; j++) {
long y = ((1L << i) - 1) & a[j];
long steps = (1L << i) - y;
if (steps <= k
&& steps < minSteps) {
minSteps = steps;
mini = j;
}
}
if (mini != -1) {
k -= (int)minSteps;
a[mini] += minSteps;
long orr = 0;
for (int j = 0; j < n; j++) {
orr |= a[j];
}
bitwiseOr = orr;
}
}
}
// Print the required result
foreach (long elements in a) {
Console.Write(elements + " ");
}
}
// Driver code
public static void Main()
{
int N = 6;
int K = 2;
long []arr = { 1, 3, 7, 0, 6, 1 };
MaximizeBitwiseOR(arr, K, N);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
function MaximizeBitwiseOR(a, k, n) {
// For storing initial
// bitwise OR of array arr[]
let orValue = 0n;
let bitwiseOr = 0n;
for (let i = 0; i < a.length; ++i) {
bitwiseOr |= BigInt(a[i]);
}
for (let i = 60; i >= 0; i--) {
if (((1n << BigInt(i)) & bitwiseOr) == 0n) {
let minSteps = Number.MAX_SAFE_INTEGER;
let mini = -1;
for (let j = 0; j < n; j++) {
let y = ((1n << BigInt(i)) - 1n) & BigInt(a[j]);
let steps = (1n << BigInt(i)) - y;
if (steps <= k && steps < minSteps) {
minSteps = Number(steps);
mini = j;
}
}
if (mini != -1) {
k -= minSteps;
a[mini] += minSteps;
let orr = 0n;
for (let j = 0; j < n; j++) {
orr |= BigInt(a[j]);
}
bitwiseOr = orr;
}
}
}
// Print the required result
console.log(a.join(" "));
}
// Driver code
const N = 6;
const K = 2;
const arr = [1, 3, 7, 0, 6, 1];
MaximizeBitwiseOR(arr, K, N);
// This code is contributed by shivhack999
Time Complexity: O(N*60)
Auxiliary Space: O(1)
Similar Reads
Maximize bitwise AND of Array by changing at most K bits of elements Given an array arr[] of length N. You can perform at most K operations on the array of the following type: Choose an index i (0 ? i ? N-1) and set the j-th bit of arr[i] to 1 (0 ? j ? 30). The task is to find the maximum possible value of bitwise AND of all array elements after performing at most K
8 min read
Minimize the max of Array by doing at most K increment and decrement Given a positive array arr[] of size N (2 ⤠N ⤠105) and a positive integer K, the task is to minimize the maximum value of arr[] by performing at most K operations where in each operation, you can select any two distinct integers i and j (0 ⤠i, j < N), then increase the value of arr[i] by 1 and
9 min read
Maximize count of unique array elements by incrementing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of unique elements possible by increasing any array element by K only once. Examples: Input: arr[] = {0, 2, 4, 3, 4}, K = 1Output: 5Explanation:Increase arr[2] ( = 4) by K ( = 1). Therefore, new ar
8 min read
Maximum frequency of any array element possible by at most K increments Given an array arr[] of size N and an integer K, the task is to find the maximum possible frequency of any array element by at most K increments. Examples: Input: arr[] = {1, 4, 8, 13}, N = 4, K = 5 Output: 2 Explanation: Incrementing arr[0] twice modifies arr[] to {4, 4, 8, 13}. Maximum frequency =
15+ min read
Maximize equal elements in two Arrays after at most K increments Given two arrays arr1[] and arr2[] of length N each and an integer K, The task is to maximize the number of equal elements at the same index in arr1[] and arr2[] by incrementing any element of arr2[] but the total increment must be at most K. Examples: Input: arr1[] = {4, 5, 6, 7}, arr2[] = {3, 4, 5
6 min read
Maximize number of elements from Array with sum at most K Given an array A[] of N integers and an integer K, the task is to select the maximum number of elements from the array whose sum is at most K. Examples: Input: A[] = {1, 12, 5, 111, 200, 1000, 10}, K = 50 Output: 4 Explanation: Maximum number of selections will be 1, 12, 5, 10 that is 1 + 12 + 5 + 1
6 min read