通过用 (X, Y) 替换对来最小化数组总和,保持它们的按位或相同
给定一个大小为N的数组arr[] 。在执行给定操作任意次数后求出数组的最小和:
- 选择两个不同的索引i, j (1 ≤ i < j ≤ N),
- 将arr[i]和arr[j]分别替换为X和Y (X, Y>0),使得arr[i] | arr[j] = X | Y,在哪里|表示按位或运算
例子:
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
方法:该问题的解决方案基于以下观察:
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.
插图:
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.
请按照以下步骤操作:
- 从头开始遍历数组。
- 计算所有数组元素的按位或。
- 将此作为答案返回。
下面是上述方法的实现,
C++
// C++ code to implement the approach
#include
using namespace std;
typedef long long int ll;
// Function minsum() which will calculate
// the minimum sum of the given array
ll minsum(ll array[], ll n, ll sum)
{
for (int i = 0; i < n; i++) {
// Calculating the or of
// all the elements in the array
sum |= array[i];
}
return sum;
}
// Driver code
int main()
{
ll array[] = { 1, 3, 2 };
// Calculating the size of array
ll n = sizeof(array) / sizeof(array[0]);
// Initialising a variable sum with zero
ll sum = 0;
// Function call
cout << minsum(array, n, sum) << "\n";
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG
{
// Function minsum() which will calculate
// the minimum sum of the given array
public static long minsum(long array[], long n,
long sum)
{
for (int i = 0; i < n; i++) {
// Calculating the or of
// all the elements in the array
sum |= array[i];
}
return sum;
}
// Driver code
public static void main(String[] args)
{
long array[] = { 1, 3, 2 };
// Calculating the size of array
long n = array.length;
// Initialising a variable sum with zero
long sum = 0;
// Function call
System.out.println(minsum(array, n, sum));
}
}
// This code is contributed by Taranpreet
Python3
# python3 code to implement the approach
# Function minsum() which will calculate
# the minimum sum of the given array
def minsum(array, n, sum):
for i in range(0, n):
# Calculating the or of
# all the elements in the array
sum |= array[i]
return sum
# Driver code
if __name__ == "__main__":
array = [1, 3, 2]
# Calculating the size of array
n = len(array)
# Initialising a variable sum with zero
sum = 0
# Function call
print(minsum(array, n, sum))
# This code is contributed by rakeshsahni
C#
// C# code to implement the approach
using System;
public class GFG{
// Function minsum() which will calculate
// the minimum sum of the given array
static long minsum(long[] array, long n,
long sum)
{
for (int i = 0; i < n; i++) {
// Calculating the or of
// all the elements in the array
sum |= array[i];
}
return sum;
}
// Driver code
static public void Main ()
{
long[] array = { 1, 3, 2 };
// Calculating the size of array
long n = array.Length;
// Initialising a variable sum with zero
long sum = 0;
// Function call
Console.Write(minsum(array, n, sum));
}
}
// This code is contributed by hrithikgarg03188.
Javascript
3
时间复杂度: 在)
辅助空间: O(1)