Maximize sum of LSBs of Bitwise OR of all possible N/2 pairs from given Array
Last Updated :
04 Oct, 2021
Given an array arr[] consisting of N positive integers, where N is even, the task is to form N/2 pairs such that the sum of the Least Significant Bit of Bitwise OR of all these pairs is maximum.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 8
Explanation:
On forming the pairs as (8, 4),(6, 2),(1, 3),(5, 7), the Bitwise OR of the pair is given by:
8 OR 4 = 12 and LSB = 4
6 OR 2 = 6 and LSB = 2
1 OR 3 = 3 and LSB = 1
5 OR 7 = 7 and LSB = 1
The sum of all the LSB is 4 + 2 + 1 + 1 = 8, which is maximum possible sum.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 3
Approach: The given problem can be solved by finding the LSB of each array element arr[i] and store them in another array, say lsb_arr[] and sort this array in descending order. Now, storing just the LSB of each array element is sufficient because in the answer, it is only requires to consider the LSB. So, only the LSB’s can be used for the Bitwise OR operation. Now, consider each pair (i, i + 1) and add the minimum of these two to the result. Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int chk( int n)
{
vector< int > v;
while (n != 0) {
v.push_back(n % 2);
n = n / 2;
}
for ( int i = 0; i < v.size(); i++) {
if (v[i] == 1) {
return pow (2, i);
}
}
return 0;
}
void sumOfLSB( int arr[], int N)
{
vector< int > lsb_arr;
for ( int i = 0; i < N; i++) {
lsb_arr.push_back(chk(arr[i]));
}
sort(lsb_arr.begin(), lsb_arr.end(), greater< int >());
int ans = 0;
for ( int i = 0; i < N - 1; i += 2) {
ans += (lsb_arr[i + 1]);
}
cout << (ans);
}
int main()
{
int N = 5;
int arr[] = { 1, 2, 3, 4, 5 };
sumOfLSB(arr, N);
}
|
Java
import java.util.*;
class GFG
{
static int chk( int n)
{
Vector<Integer> v = new Vector<Integer>();
while (n != 0 ) {
v.add(n % 2 );
n = n / 2 ;
}
for ( int i = 0 ; i < v.size(); i++) {
if (v.get(i) == 1 ) {
return ( int ) Math.pow( 2 , i);
}
}
return 0 ;
}
static void sumOfLSB( int arr[], int N)
{
Vector<Integer> lsb_arr = new Vector<Integer>() ;
for ( int i = 0 ; i < N; i++) {
lsb_arr.add(chk(arr[i]));
}
Collections.sort(lsb_arr);
int ans = 0 ;
for ( int i = 0 ; i < N - 1 ; i += 2 ) {
ans += (lsb_arr.get(i + 1 ));
}
System.out.print(ans);
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 1 , 2 , 3 , 4 , 5 };
sumOfLSB(arr, N);
}
}
|
Python3
def chk(v):
v = list ( bin (v)[ 2 :])
v.reverse()
if ( '1' in v):
v = v.index( '1' )
return ( 2 * * v)
else :
return 0
def sumOfLSB(arr, N):
lsb_arr = []
for i in range (N):
lsb_arr.append(chk(arr[i]))
lsb_arr.sort(reverse = True )
ans = 0
for i in range ( 0 , N - 1 , 2 ):
ans + = (lsb_arr[i + 1 ])
print (ans)
N = 5
arr = [ 1 , 2 , 3 , 4 , 5 ]
sumOfLSB(arr, N)
|
Javascript
<script>
const chk = (n) => {
let v = [];
while (n != 0) {
v.push(n % 2);
n = parseInt(n / 2);
}
for (let i = 0; i < v.length; i++) {
if (v[i] == 1) {
return Math.pow(2, i);
}
}
return 0;
}
const sumOfLSB = (arr, N) => {
let lsb_arr = [];
for (let i = 0; i < N; i++) {
lsb_arr.push(chk(arr[i]));
}
lsb_arr.sort((a, b) => a - b)
let ans = 0;
for (let i = 0; i < N - 1; i += 2) {
ans += (lsb_arr[i + 1]);
}
document.write(ans);
}
let N = 5;
let arr = [1, 2, 3, 4, 5];
sumOfLSB(arr, N);
</script>
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int chk( int n)
{
List< int > v = new List< int >();
while (n != 0) {
v.Add(n % 2);
n = n / 2;
}
int j = 0;
foreach ( int i in v) {
if (i == 1) {
return ( int ) Math.Pow(2.0, ( double )j);
}
j++;
}
return 0;
}
static void sumOfLSB( int [] arr, int N)
{
int [] lsb_arr = new int [N];
for ( int i = 0; i < N; i++) {
lsb_arr[i] = chk(arr[i]);
}
Array.Sort(lsb_arr);
int ans = 0;
for ( int i = 0; i < N - 1; i += 2) {
ans += (lsb_arr[i + 1]);
}
Console.WriteLine(ans);
}
static public void Main (){
int N = 5;
int [] arr = { 1, 2, 3, 4, 5 };
sumOfLSB(arr, N);
}
}
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...