给定偶数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)