给定正偶数N ,任务是将2的前N个幂分成两个相等的序列,以使它们的和之间的绝对差最小。打印获得的最小差异。
例子:
Input: N = 2
Output: 2
Explanation:
The sequence is {2, 4}.
Only possible way to split the sequence is {2}, {4}. Therefore, difference = 4 − 2 = 2.
Input: N = 4
Output: 6
Explanation:
The sequence is {2, 4, 8, 16}.
The most optimal way is to split the sequence as {2, 16}, {4, 8}. The difference is (2 + 16) − (4 + 8) = 6.
天真的方法:解决此问题的最简单方法是生成序列的N / 2个元素的所有可能组合并存储它们的和。然后,在所有对的总和中找到最小差。
时间复杂度: O(2 N )
辅助空间: O(N)
方法:上述方法也可以根据以下观察进行优化:
- 由于2 N大于所有其他元素的总和:
- 具有最大元素的子数组将始终具有较大的总和。因此,为了使它们的和之间的差异最小,我们的想法是将(N / 2 – 1)个最小的元素放入具有最大元素的子数组中。
请按照以下步骤解决问题:
- 初始化两个变量sum1 = 0和sum2 = 0 ,以分别存储第一个子数组和第二个子数组的和。
- 加总和和2 N到变量sum1 。
- 加总和到变量sum2 。
- 完成上述步骤后,打印sum1和sum2之间的差异。
下面是上述方法的实现:
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)