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
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.
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Ways of dividing a group into two halves such that two elements are in different groups
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Partition the array into two odd length groups with minimized absolute difference between their median
- Count the number of ways to divide N in k groups incrementally
- Divide a number into two parts such that sum of digits is maximum
- Divide array into two arrays which does not contain any pair with sum K
- Count number of ways to divide an array into two halves with same sum
- Minimum difference between groups of size two
- Split N^2 numbers into N groups of equal sum
- Minimum cuts required to divide the Circle into equal parts
- Divide number into two parts divisible by given numbers
- Divide a big number into two parts that differ by k
- Divide a number into two parts
- Divide cuboid into cubes such that sum of volumes is maximum
- Divide first N natural numbers into 3 equal sum subsets
- Minimize the cost of partitioning an array into K groups
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Minimum sum obtained from groups of four elements from the given array
- Split N powers of 2 into two subsets such that their difference of 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 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