Given an integer N, the task is to divide the numbers from 1 to N into two nonempty subsets such that the sum of elements in the set is equal. Print the element in the subset. If we can’t form any subset then print -1.
Input N = 4
Size of subset 1 is: 2
Elements of the subset are: 1 4
Size of subset 2 is: 2
Elements of the subset are: 2 3
The first and the second set have equal sum that is 5.
Input: N = 8
Size of subset 1 is: 4
Elements of the subset are: 1 8 3 6
Size of subset 2 is: 4
Elements of the subset are: 2 7 4 5
The first and the second set have equal sum that is 18.
Approach: To solve the problem mentioned above we have to observe the two cases for integer N, which is even and odd. Below are the observations:
- When N is even: For this case, we can choose the numbers in pair and assign to each of the sets alternatively.
For Example, N = 8 so the numbers are 1, 2, 3, 4, 5, 6, 7, 8, and 1, 3, 6, 8 can be grouped together in a set.
This is because we can take two numbers between 1 and N at once and assign them to same sets, now if in first turn we take 1 and N and assign it to first set then if we increase the first number i.e 1 by one and decrease the second number i.e N by one the total sum remains same (1 + N = 2 + N – 1) and we assign 2 and N-1 to second set, therefore in this order we can divide the numbers into pairs alternatively to each set. Here N = 2 is an exception.
- When N is odd: For this case, then the sum of each subset is half of the sum of the first N natural number. Iterate through the numbers from N to 1 if the number is less than or equal to the required sum then we include this number otherwise if the number is greater than the required sum then we ignore the number and include the number which is equal to the required sum. Also, keep track of the numbers used for one set so that we can find the numbers for another subset.
Below is the implementation of the above approach:
Size of subset 1 is: 4 Elements of the subset are: 1 8 3 6 Size of subset 2 is: 4 Elements of the subset are: 2 7 4 5
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Split an Array A into Subsets having equal Sum and sizes equal to elements of Array B
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- 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 array into K subsets to maximize their sum of maximums and minimums
- Divide first N natural numbers into 3 equal sum subsets
- Split an array into two equal Sum subarrays
- Check if an array can be split into subsets of K consecutive elements
- Split Array into min number of subsets with difference between each pair greater than 1
- Split N into two integers whose addition to A and B makes them equal
- Partition of a set into K subsets with equal sum using BitMask and DP
- Split the array into equal sum parts according to given conditions
- Check if an array can be split into 3 subsequences of equal sum or not
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Split array into two subarrays such that difference of their sum is minimum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.