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 2^{1}, 2^{2}, 2^{3}, 2^{4}. The most optimal way to divide it into two groups with equal element is 2^{4}+ 2^{1}in one group and 2^{2}+ 2^{3}in another group giving a minimum possible difference of 6.

Input:n = 8

Output:30

Explanation:

Here n = 8 which means we have 2^{1}, 2^{2}, 2^{3}, 2^{4}, 2^{5}, 2^{6}, 2^{7}, 2^{8}. The most optimal way to divide it into two groups with equal element is 2^{8}+ 2^{1}+ 2^{2}+ 2^{3}in one group and 2^{4}+ 2^{5}+ 2^{6}+ 2^{7}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
**2**.^{N} - 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 2^{4}to the first group and add**N/2 – 1**, i.e. 1 more element to this group which is the least which means is 2^{1}. - 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 ` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time complexity:*** O(N)*

**Auxiliary Space:*** O(1)*

## Recommended Posts:

- Split array into two subarrays such that difference of their sum is minimum
- Split array into K subsets to maximize their sum of maximums and minimums
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Split numbers from 1 to N into two equal sum subsets
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Split Array into min number of subsets with difference between each pair greater than 1
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Check if it is possible to split given Array into K odd-sum subsets
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Partition first N natural number into two sets such that their sum is not coprime
- Check if an array can be split into subsets of K consecutive elements
- Count pairs from 1 to N such that their Sum is divisible by their XOR

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.