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
- Minimum difference between groups of size two
- Minimum sum obtained from groups of four elements from the given array
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Minimum positive integer to divide a number such that the result is an odd
- Minimum cuts required to divide the Circle into equal parts
- Co-prime pair with given sum minimum difference
- Minimum absolute difference between N and a power of 2
- Minimum absolute difference between N and any power of 2
- Minimum sum of absolute difference of pairs of two arrays
- Rectangle with minimum possible difference between the length and the width
- Sum of minimum difference between consecutive elements of an array
- Minimize the difference between minimum and maximum elements
- Pair of prime numbers with a given sum and minimum absolute difference
- Find an index such that difference between product of elements before and after it is minimum
- Find the minimum difference between Shifted tables of two numbers
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.
Improved By : Mithun Kumar