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++
#include<bits/stdc++.h>
using namespace std;
void MinDiff( int n)
{
int val = pow (2, n);
int sep = n / 2;
int grp1 = 0;
int grp2 = 0;
grp1 = grp1 + val;
for ( int i = 1; i < sep; i++)
grp1 = grp1 + pow (2, i);
for ( int i = sep; i < n; i++)
grp2 = grp2 + pow (2, i);
cout << ( abs (grp1 - grp2));
}
int main()
{
int n = 4;
MinDiff(n);
}
|
Java
import java.lang.Math;
class GFG{
public static void MinDiff( int n)
{
int val = ( int )Math.pow( 2 , n);
int sep = n / 2 ;
int grp1 = 0 ;
int grp2 = 0 ;
grp1 = grp1 + val;
for ( int i = 1 ; i < sep; i++)
grp1 = grp1 + ( int )Math.pow( 2 , i);
for ( int i = sep; i < n; i++)
grp2 = grp2 + ( int )Math.pow( 2 , i);
System.out.println(Math.abs(grp1 - grp2));
}
public static void main(String args[])
{
int n = 4 ;
MinDiff(n);
}
}
|
Python3
def MinDiff(n):
val = 2 * * n
sep = n / / 2
grp1 = 0
grp2 = 0
grp1 = grp1 + val
for i in range ( 1 , sep):
grp1 = grp1 + 2 * * i
for i in range (sep, n):
grp2 = grp2 + 2 * * i
print ( abs (grp1 - grp2))
if __name__ = = '__main__' :
n = 4
MinDiff(n)
|
C#
using System;
class GFG{
public static void MinDiff( int n)
{
int val = ( int )Math.Pow(2, n);
int sep = n / 2;
int grp1 = 0;
int grp2 = 0;
grp1 = grp1 + val;
for ( int i = 1; i < sep; i++)
grp1 = grp1 + ( int )Math.Pow(2, i);
for ( int i = sep; i < n; i++)
grp2 = grp2 + ( int )Math.Pow(2, i);
Console.Write(Math.Abs(grp1 - grp2));
}
public static void Main()
{
int n = 4;
MinDiff(n);
}
}
|
Javascript
<script>
function MinDiff(n)
{
var val = parseInt( Math.pow(2, n));
var sep = n / 2;
var grp1 = 0;
var grp2 = 0;
grp1 = grp1 + val;
for (i = 1; i < sep; i++)
grp1 = grp1 + parseInt( Math.pow(2, i));
for (i = sep; i < n; i++)
grp2 = grp2 + parseInt( Math.pow(2, i));
document.write(Math.abs(grp1 - grp2));
}
var n = 4;
MinDiff(n);
</script>
|
Time complexity: O(N*logN) because it is using a pow(2,i) function inside a for loop
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...