Maximum bitwise OR value of subsequence of length K
Last Updated :
04 Jan, 2022
Given an array arr[] of N positive integers and a number K, the task is to find the maximum value of bitwise OR of the subsequence of size K.
Examples:
Input: arr[] = {2, 5, 3, 6, 11, 13}, k = 3
Output: 15
Explanation:
The sub-sequence will maximum OR value is 2, 11, 13.
Input: arr[] = {5, 9, 7, 19}, k = 3
Output: 31
Explanation:
The maximum value of bitwise OR of the subsequence of size K = 3 is 31.
Naive Approach: The naive approach is to generate all the subsequence of length K and find the Bitwise OR value of all subsequences. The maximum among all of them will be the answer.
Time Complexity: O(N2)
Auxiliary Space: O(K)
Efficient Approach: To optimize the above method try to implement the Greedy Approach. Below are the steps:
- Initialize an integer array bit[] of size 32 with all value equal to 0.
- Now iterate for each index of bit[] array from 31 to 0, and check if the ith value of bit array is 0 then iterate in the given array and find an element which contributes maximum 1 to our bit array after taking it.
- Take that element and change the bit array correspondingly, also decrease k each time by 1 if k > 0. Otherwise break out from the loop.
- Now convert the bit[] array into a decimal number to get final answer.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int build_num( int bit[])
{
int ans = 0;
for ( int i = 0; i < 32; i++)
if (bit[i])
ans += (1 << i);
return ans;
}
int maximumOR( int arr[], int n, int k)
{
int bit[32] = { 0 };
for ( int i = 31; i >= 0; i--) {
if (bit[i] == 0 && k > 0) {
int temp = build_num(bit);
int temp1 = temp;
int val = -1;
for ( int j = 0; j < n; j++) {
if (temp1 < (temp | arr[j])) {
temp1 = temp | arr[j];
val = arr[j];
}
}
if (val != -1) {
k--;
for ( int j = 0; j < 32; j++) {
if (val & (1 << j))
bit[j]++;
}
}
}
}
return build_num(bit);
}
int main()
{
int arr[] = { 5, 9, 7, 19 };
int k = 3;
int n = sizeof arr / sizeof arr[0];
cout << maximumOR(arr, n, k);
return 0;
}
|
Java
class GFG{
static int build_num( int []bit)
{
int ans = 0 ;
for ( int i = 0 ; i < 32 ; i++)
if (bit[i] == 1 )
ans += ( 1 << i);
ans += 32 ;
return ans;
}
static int maximumOR( int []arr, int n, int k)
{
int bit[] = new int [ 32 ];
for ( int i = 31 ; i >= 0 ; i--)
{
if (bit[i] == 0 && k > 0 )
{
int temp = build_num(bit);
int temp1 = temp;
int val = - 1 ;
for ( int j = 0 ; j < n; j++)
{
if (temp1 < (temp | arr[j]))
{
temp1 = temp | arr[j];
val = arr[j];
}
}
if (val != - 1 )
{
k--;
for ( int j = 0 ; j < 32 ; j++)
{
bit[j]++;
}
}
}
}
return build_num(bit);
}
public static void main(String[] args)
{
int arr[] = { 5 , 9 , 7 , 19 };
int k = 3 ;
int n = arr.length;
System.out.println(maximumOR(arr, n, k));
}
}
|
Python3
def build_num(bit):
ans = 0
for i in range ( 0 , 32 ):
if (bit[i]):
ans + = ( 1 << i)
return ans;
def maximumOR(arr, n, k):
bit = [ 0 ] * 32
for i in range ( 31 , - 1 , - 1 ):
if (bit[i] = = 0 and k > 0 ):
temp = build_num(bit)
temp1 = temp
val = - 1
for j in range ( 0 , n):
if (temp1 < (temp | arr[j])):
temp1 = temp | arr[j]
val = arr[j]
if (val ! = - 1 ):
k - = 1
for j in range ( 0 , 32 ):
if (val & ( 1 << j)):
bit[j] + = 1
return build_num(bit)
arr = [ 5 , 9 , 7 , 19 ]
k = 3 ;
n = len (arr)
print (maximumOR(arr, n, k))
|
C#
using System;
class GFG{
static int build_num( int []bit)
{
int ans = 0;
for ( int i = 0; i < 32; i++)
if (bit[i] == 1)
ans += (1 << i);
ans += 32;
return ans;
}
static int maximumOR( int []arr, int n, int k)
{
int []bit = new int [32];
for ( int i = 31; i >= 0; i--)
{
if (bit[i] == 0 && k > 0)
{
int temp = build_num(bit);
int temp1 = temp;
int val = -1;
for ( int j = 0; j < n; j++)
{
if (temp1 < (temp | arr[j]))
{
temp1 = temp | arr[j];
val = arr[j];
}
}
if (val != -1)
{
k--;
for ( int j = 0; j < 32; j++)
{
bit[j]++;
}
}
}
}
return build_num(bit);
}
public static void Main()
{
int []arr = { 5, 9, 7, 19 };
int k = 3;
int n = arr.Length;
Console.Write(maximumOR(arr, n, k));
}
}
|
Javascript
<script>
function build_num(bit)
{
let ans = 0;
for (let i = 0; i < 32; i++)
if (bit[i] > 0)
ans += (1 << i);
return ans;
}
function maximumOR(arr, n, k)
{
let bit = new Array(32);
bit.fill(0);
for (let i = 31; i >= 0; i--)
{
if (bit[i] == 0 && k > 0)
{
let temp = build_num(bit);
let temp1 = temp;
let val = -1;
for (let j = 0; j < n; j++)
{
if (temp1 < (temp | arr[j]))
{
temp1 = temp | arr[j];
val = arr[j];
}
}
if (val != -1)
{
k--;
for (let j = 0; j < 32; j++)
{
if ((val & (1 << j)) > 0)
bit[j]++;
}
}
}
}
return build_num(bit);
}
let arr = [ 5, 9, 7, 19 ];
let k = 3;
let n = arr.length;
document.write(maximumOR(arr, n, k));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Similar article: Maximum Bitwise AND value of subsequence of length K
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...