📌  相关文章
📜  通过用 (X, Y) 替换对来最小化数组总和,保持它们的按位或相同

📅  最后修改于: 2022-05-13 01:56:06.330000             🧑  作者: Mango

通过用 (X, Y) 替换对来最小化数组总和,保持它们的按位或相同

给定一个大小为N的数组arr[] 。在执行给定操作任意次数后求出数组的最小和:

  • 选择两个不同的索引i, j (1 ≤ i < j ≤ N),
  • arr[i]arr[j]分别替换为XY (X, Y>0),使得arr[i] | arr[j] = X | Y,在哪里|表示按位或运算

例子:

方法:该问题的解决方案基于以下观察:

插图

请按照以下步骤操作:

  • 从头开始遍历数组。
  • 计算所有数组元素的按位或
  • 将此作为答案返回。

下面是上述方法的实现,

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)