Split N powers of 2 into two subsets such that their difference of sum is minimum
Last Updated :
30 Aug, 2022
Given an even number N, the task is to split all N powers of 2 into two sets such that the difference of their sum is minimum.
Examples:
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.
Approach: To solve the problem mentioned above we have to follow the steps given below:
- In the first group add the largest element that is 2N.
- After adding the first number in one group, add N/2 - 1 more elements to this group, where the elements has to start from the least power of two or from the beginning of the sequence.
For example: if N = 4 then add 24 to the first group and add N/2 - 1, i.e. 1 more element to this group which is the least which means is 21. - The remaining element of the sequence forms the elements of the second group.
- Calculate the sum for both the groups and then find the absolute difference between the groups which will eventually be the minimum.
Below is the implementation of the above approach:
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<bits/stdc++.h>
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
<script>
// javascript program to find the minimum
// difference possible by splitting
// all powers of 2 up to N into two
// sets of equal size
function MinDiff(n)
{
// Store the largest
var val = parseInt( Math.pow(2, n));
// Form two separate groups
var sep = n / 2;
// Initialize the sum
// for two groups as 0
var grp1 = 0;
var 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 = 1; i < sep; i++)
grp1 = grp1 + parseInt( Math.pow(2, i));
// Sum of remaining
// n / 2 - 1 elements
for (i = sep; i < n; i++)
grp2 = grp2 + parseInt( Math.pow(2, i));
// Min difference between
// the two groups
document.write(Math.abs(grp1 - grp2));
}
// Driver Code
var n = 4;
MinDiff(n);
// This code is contributed by gauravrajput1
</script>
Time complexity: O(N*logN) because it is using a pow(2,i) function inside a for loop
Auxiliary Space: O(1)
Similar Reads
Partition a set into two subsets such that the difference of subset sums is minimum Given an array arr[] of size n, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. If there is a set S with n elements, then if we assume Subset1 has m elements, Subset2 must have n-m elements and the value of abs(sum(Subset1) - sum(Subs
15+ min read
Partition into two subsets of lengths K and (N - k) such that the difference of sums is maximum Given an array of non-negative integers of length N and an integer K. Partition the given array into two subsets of length K and N - K so that the difference between the sum of both subsets is maximum. Examples : Input : arr[] = {8, 4, 5, 2, 10} k = 2 Output : 17 Explanation : Here, we can make firs
7 min read
Partition a set into two non-empty subsets such that the difference of subset sums is maximum Given a set of integers S, the task is to divide the given set into two non-empty sets S1 and S2 such that the absolute difference between their sums is maximum, i.e., abs(sum(S1) â sum(S2)) is maximum. Example: Input: S[] = { 1, 2, 1 } Output: 2Explanation:The subsets are {1} and {2, 1}. Their abso
7 min read
Ways to form n/2 pairs such that difference of pairs is minimum Given an array arr of N integers, the task is to split the elements of the array into N/2 pairs (each pair having 2 elements) such that the absolute difference between two elements of any pair is as minimum as possible. Note: N will always be even. Examples: Input: arr[] = {1, 7, 3, 8} Output: 1 The
15+ min read
Divide array in two Subsets such that sum of square of sum of both subsets is maximum Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pa
6 min read