Minimize Array Sum by replacing pairs with (X, Y) keeping their bitwise OR same
Last Updated :
31 May, 2022
Given an array arr[] of size N. Find the minimum sum of the array after performing given operations any number of times:
- Select two different indices i, j (1 ≤ i < j ≤ N),
- Replace arr[i] and arr[j] with X and Y respectively (X, Y>0), such that arr[i] | arr[j] = X | Y, where | denotes the bitwise OR operation
Examples:
Input: arr[] = {1, 3, 2}
Output: 3
Explanation: arr = {1, 3, 2} {bitwise OR of array elements is 3.}, sum=6
Replace 1 and 3 with 3 and 0 as {1|3 = 3 = 3|0}.
arr = {3, 0, 2}, {bitwise OR of array elements is still 3}, sum = 5
Replace 3 and 2 with 3 and 0 as {3|2 = 3 = 3|0}
arr = {3, 0, 0} {or of all array elements is still 3), sum= 3.
This is the minimum sum possible.
Input: arr[] = {3, 5, 6}
Output: 7
Approach: The solution to the problem is based on the following observation:
If (arr[i], arr[j]) is replaced with ((arr[i] | arr[j]), 0), the bitwise OR value will remain the same and the sum will be minimized.
Illustration:
Consider: arr[] = {1, 3, 2}
Initial Array sum = 6
Operation 1: Replace (1, 3) with (1|3 , 0):
-> Bitwise OR of (1, 3) = 3
-> Replacing 1 with bitwise OR value 3 and 3 with 0 respectively
-> New pair will be (3, 0), whose bitwise OR value will be 3|0 = 3 (same as original pair (1, 3)).
-> Updated Array: {3, 0, 2}
-> Updated Array sum = 5
Operation 2: Similarly replace (3, 2) with (3|2 , 0):
-> Bitwise OR of (3, 2) = 3
-> Replacing 3 with bitwise OR value 3 and 2 with 0 respectively
-> New pair will be (3, 0), whose bitwise OR value will be 3|0 = 3 (same as original pair (3, 2)).
-> Updated Array: {3, 0, 0}
-> Updated Array sum = 3
Now no more operations can be done and the Array sum cannot be reduced further from 3.
Therefore final minimized Array sum will be the bitwise OR of all Array elements.
Follow the steps mentioned below:
- Traverse the array from the starting.
- Calculate the bitwise OR of all the array elements.
- Return this as the answer.
Below is the implementation of the above approach,
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll minsum(ll array[], ll n, ll sum)
{
for ( int i = 0; i < n; i++) {
sum |= array[i];
}
return sum;
}
int main()
{
ll array[] = { 1, 3, 2 };
ll n = sizeof (array) / sizeof (array[0]);
ll sum = 0;
cout << minsum(array, n, sum) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static long minsum( long array[], long n,
long sum)
{
for ( int i = 0 ; i < n; i++) {
sum |= array[i];
}
return sum;
}
public static void main(String[] args)
{
long array[] = { 1 , 3 , 2 };
long n = array.length;
long sum = 0 ;
System.out.println(minsum(array, n, sum));
}
}
|
Python3
def minsum(array, n, sum ):
for i in range ( 0 , n):
sum | = array[i]
return sum
if __name__ = = "__main__" :
array = [ 1 , 3 , 2 ]
n = len (array)
sum = 0
print (minsum(array, n, sum ))
|
C#
using System;
public class GFG{
static long minsum( long [] array, long n,
long sum)
{
for ( int i = 0; i < n; i++) {
sum |= array[i];
}
return sum;
}
static public void Main ()
{
long [] array = { 1, 3, 2 };
long n = array.Length;
long sum = 0;
Console.Write(minsum(array, n, sum));
}
}
|
Javascript
<script>
function minsum(array, n, sum) {
for (let i = 0; i < n; i++) {
sum |= array[i];
}
return sum;
}
let array = [1, 3, 2];
let n = array.length;
let sum = 0;
document.write(minsum(array, n, sum) + "<br>" );
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...