给定偶数N ,任务是将 2 的所有N 次幂分成两组,使得它们和的差异最小。
例子:
Input: n = 4
Output: 6
Explanation:
Here n = 4 which means we have 21, 22, 23, 24. The most optimal way to divide it into two groups with equal element is 24 + 21 in one group and 22 + 23 in another group giving a minimum possible difference of 6.
Input: n = 8
Output: 30
Explanation:
Here n = 8 which means we have 21, 22, 23, 24, 25, 26, 27, 28. The most optimal way to divide it into two groups with equal element is 28 + 21 + 22 + 23 in one group and 24 + 25 + 26 + 27 in another group giving a minimum possible difference of 30.
方法:要解决上述问题,我们必须按照以下步骤操作:
- 在第一组中添加最大的元素2 N 。
- 添加一组中的第一个数字后,再向该组添加N/2 – 1个元素,其中元素必须从 2 的最小幂或从序列的开头开始。
例如:如果 N = 4,则将 2 4添加到第一组并添加N/2 – 1 ,即该组再添加 1 个元素,即最少的元素,即 2 1 。 - 序列的剩余元素形成第二组的元素。
- 计算两个组的总和,然后找到最终将是最小值的组之间的绝对差异。
下面是上述方法的实现:
C++
// C++ program to find the minimum
// difference possible by splitting
// all powers of 2 up to N into two
// sets of equal size
#include
using namespace std;
void MinDiff(int n)
{
// Store the largest
int val = pow(2, n);
// Form two separate groups
int sep = n / 2;
// Initialize the sum
// for two groups as 0
int grp1 = 0;
int grp2 = 0;
// Insert 2 ^ n in the
// first group
grp1 = grp1 + val;
// Calculate the sum of
// least n / 2 -1 elements
// added to the first set
for(int i = 1; i < sep; i++)
grp1 = grp1 + pow(2, i);
// Sum of remaining
// n / 2 - 1 elements
for(int i = sep; i < n; i++)
grp2 = grp2 + pow(2, i);
// Min Difference between
// the two groups
cout << (abs(grp1 - grp2));
}
// Driver code
int main()
{
int n = 4;
MinDiff(n);
}
// This code is contributed by Bhupendra_Singh
Java
// Java program to find the minimum
// difference possible by splitting
// all powers of 2 up to N into two
// sets of equal size
import java.lang.Math;
class GFG{
public static void MinDiff(int n)
{
// Store the largest
int val = (int)Math.pow(2, n);
// Form two separate groups
int sep = n / 2;
// Initialize the sum
// for two groups as 0
int grp1 = 0;
int grp2 = 0;
// Insert 2 ^ n in the
// first group
grp1 = grp1 + val;
// Calculate the sum of
// least n / 2 -1 elements
// added to the first set
for(int i = 1; i < sep; i++)
grp1 = grp1 + (int)Math.pow(2, i);
// Sum of remaining
// n / 2 - 1 elements
for(int i = sep; i < n; i++)
grp2 = grp2 + (int)Math.pow(2, i);
// Min difference between
// the two groups
System.out.println(Math.abs(grp1 - grp2));
}
// Driver Code
public static void main(String args[])
{
int n = 4;
MinDiff(n);
}
}
// This code is contributed by grand_master
Python3
# Python3 program to find the minimum
# difference possible by splitting
# all powers of 2 up to N into two
# sets of equal size
def MinDiff(n):
# Store the largest
val = 2 ** n
# Form two separate groups
sep = n // 2
# Initialize the sum
# for two groups as 0
grp1 = 0
grp2 = 0
# Insert 2 ^ n in the
# first group
grp1 = grp1 + val
# Calculate the sum of
# least n / 2 -1 elements
# added to the first set
for i in range(1, sep):
grp1 = grp1 + 2 ** i
# sum of remaining
# n / 2 - 1 elements
for i in range(sep, n):
grp2 = grp2 + 2 ** i
# Min Difference between
# the two groups
print(abs(grp1 - grp2))
# Driver code
if __name__=='__main__':
n = 4
MinDiff(n)
C#
// C# program to find the minimum
// difference possible by splitting
// all powers of 2 up to N into two
// sets of equal size
using System;
class GFG{
public static void MinDiff(int n)
{
// Store the largest
int val = (int)Math.Pow(2, n);
// Form two separate groups
int sep = n / 2;
// Initialize the sum
// for two groups as 0
int grp1 = 0;
int grp2 = 0;
// Insert 2 ^ n in the
// first group
grp1 = grp1 + val;
// Calculate the sum of
// least n / 2 -1 elements
// added to the first set
for(int i = 1; i < sep; i++)
grp1 = grp1 + (int)Math.Pow(2, i);
// Sum of remaining
// n / 2 - 1 elements
for(int i = sep; i < n; i++)
grp2 = grp2 + (int)Math.Pow(2, i);
// Min difference between
// the two groups
Console.Write(Math.Abs(grp1 - grp2));
}
// Driver Code
public static void Main()
{
int n = 4;
MinDiff(n);
}
}
// This code is contributed by Akanksha_Rai
Javascript
6
时间复杂度: O(N)
辅助空间: O(1)