Given a positive integer n such that n > 2. Divide numbers from 1 to n in two groups such that absolute difference of sum of each group is minimum. Print any two groups with their size in first line and in next line print elements of that group.
Input : 5 Output : 2 5 2 3 4 3 1 Here sum of group 1 is 7 and sum of group 2 is 8. Their absolute difference is 1 which is minimum. We can have multiple correct answers. (1, 2, 5) and (3, 4) is another such group. Input : 6 Output : 2 6 4 4 5 3 2 1
We can always divide sum of n integers in two groups such that their absolute difference of their sum is 0 or 1. So sum of group at most differ by 1. We define sum of group1 as half of n elements sum.
Now run a loop from n to 1 and insert i into group1 if inserting an element doesn’t exceed group1 sum otherwise insert that i into group2.
2 5 2 3 4 3 1
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum difference between groups of size two
- Count the number of ways to divide N in k groups incrementally
- Minimum Bipartite Groups
- Minimum sum obtained from groups of four elements from the given array
- Partition the array into two odd length groups with minimized absolute difference between their median
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Minimum positive integer to divide a number such that the result is an odd
- Minimum cuts required to divide the Circle into equal parts
- Minimum absolute difference between N and a power of 2
- Find two numbers with the given LCM and minimum possible difference
- Minimum absolute difference between N and any power of 2
- Minimum difference between any two primes from the given range
- Co-prime pair with given sum minimum difference
- Sum of minimum difference between consecutive elements of an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Mithun Kumar