📌  相关文章
📜  最小化通过分解2的前N个幂获得的两个序列之间的差异

📅  最后修改于: 2021-04-17 16:53:10             🧑  作者: Mango

给定正偶数N ,任务是将2的前N个幂分成两个相等的序列,以使它们的和之间的绝对差最小。打印获得的最小差异。

例子:

天真的方法:解决此问题的最简单方法是生成序列的N / 2个元素的所有可能组合并存储它们的和。然后,在所有对的总和中找到最小差。
时间复杂度: O(2 N )
辅助空间: O(N)

方法:上述方法也可以根据以下观察进行优化:

  • 由于2 N大于所有其他元素的总和:
  • 具有最大元素的子数组将始终具有较大的总和。因此,为了使它们的和之间的差异最小,我们的想法是将(N / 2 – 1)个最小的元素放入具有最大元素的子数组中。

请按照以下步骤解决问题:

  • 初始化两个变量sum1 = 0sum2 = 0 ,以分别存储第一个子数组和第二个子数组的和。
  • 加总和\sum_{i = 1}^{i = N/2 - 1}2^{i}          2 N到变量sum1
  • 加总和\sum_{i = N/2}^{i = N - 1}2^{i}          到变量sum2
  • 完成上述步骤后,打印sum1sum2之间的差异。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to partition first N powers
// of 2 into two subsequences with
// minimum difference between their sum
void minimumDifference(int N)
{
    // Largest element in the first part
    int sum1 = (1 << N), sum2 = 0;
 
    // Place N/2 - 1 smallest
    // elements in the first sequence
    for (int i = 1; i < N / 2; i++)
        sum1 += (1 << i);
 
    // Place remaining N / 2 elements
    // in the second sequence
    for (int i = N / 2; i < N; i++)
        sum2 += (1 << i);
 
    // Print the minimum difference
    cout << sum1 - sum2;
}
 
// Driver Code
int main()
{
    int N = 4;
    minimumDifference(N);
 
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG
{
 
 // Function to partition first N powers
  // of 2 into two subsequences with
  // minimum difference between their sum
  static void minimumDifference(int N)
  {
     
    // Largest element in the first part
    int sum1 = (1 << N), sum2 = 0;
  
    // Place N/2 - 1 smallest
    // elements in the first sequence
    for (int i = 1; i < N / 2; i++)
      sum1 += (1 << i);
  
    // Place remaining N / 2 elements
    // in the second sequence
    for (int i = N / 2; i < N; i++)
      sum2 += (1 << i);
  
    // Print the minimum difference
    System.out.println(sum1 - sum2);
  }
 
    // Driver Code
    public static void main(String args[])
    {
        int N = 4;
        minimumDifference(N);
    }
}
 
// This code is contributed by splevel62.


Python3
# Python program for the above approach
 
# Function to partition first N powers
# of 2 into two subsequences with
# minimum difference between their sum
def minimumDifference(N):
   
    # Largest element in the first part
    sum1 = (1 << N)
    sum2 = 0
 
    # Place N/2 - 1 smallest
    # elements in the first sequence
    for i in range(1, N // 2):
        sum1 += (1 << i)
 
    # Place remaining N / 2 elements
    # in the second sequence
    for i in range( N // 2, N):
        sum2 += (1 << i)
 
    # Print the minimum difference
    print(sum1 - sum2)
     
# Driver Code
N = 4
minimumDifference(N)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to partition first N powers
  // of 2 into two subsequences with
  // minimum difference between their sum
  static void minimumDifference(int N)
  {
    // Largest element in the first part
    int sum1 = (1 << N), sum2 = 0;
 
    // Place N/2 - 1 smallest
    // elements in the first sequence
    for (int i = 1; i < N / 2; i++)
      sum1 += (1 << i);
 
    // Place remaining N / 2 elements
    // in the second sequence
    for (int i = N / 2; i < N; i++)
      sum2 += (1 << i);
 
    // Print the minimum difference
    Console.WriteLine(sum1 - sum2);
  }
 
  // Driver Code
  static public void Main ()
  {
    int N = 4;
    minimumDifference(N);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript


输出:
6

时间复杂度: O(N)
辅助空间: O(1)