Divide 1 to n into two groups with minimum sum difference - GeeksforGeeks

28 Apr, 2021
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.Examples:  
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.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to divide n integers// in two groups such that absolute// difference of their sum is minimum#include <bits/stdc++.h>using namespace std; // To print vector along sizevoid printVector(vector<int> v){    // Print vector size    cout << v.size() << endl;     // Print vector elements    for (int i = 0; i < v.size(); i++)        cout << v[i] << " ";     cout << endl;} // To divide n in two groups such that// absolute difference of their sum is// minimumvoid findTwoGroup(int n){    // Find sum of all elements upto n    int sum = n * (n + 1) / 2;     // Sum of elements of group1    int group1Sum = sum / 2;     vector<int> group1, group2;     for (int i = n; i > 0; i--) {         // If sum is greater then or equal        // to 0 include i in group 1        // otherwise include in group2        if (group1Sum - i >= 0) {             group1.push_back(i);             // Decrease sum of group1            group1Sum -= i;        }        else {            group2.push_back(i);        }    }     // Print both the groups    printVector(group1);    printVector(group2);} // Driver program to test above functionsint main(){    int n = 5;    findTwoGroup(n);    return 0;}
// Java program to divide n integers// in two groups such that absolute// difference of their sum is minimumimport java.io.*;import java.util.*; class GFG{    // To print vector along size    static void printVector(Vector<Integer> v)    {        // Print vector size        System.out.println(v.size());             // Print vector elements        for (int i = 0; i < v.size(); i++)            System.out.print(v.get(i) + " ");             System.out.println();    }         // To divide n in two groups such that    // absolute difference of their sum is    // minimum    static void findTwoGroup(int n)    {        // Find sum of all elements upto n        int sum = n * (n + 1) / 2;             // Sum of elements of group1        int group1Sum = sum / 2;             Vector<Integer> group1 = new Vector<Integer>();        Vector<Integer> group2 = new Vector<Integer>();             for (int i = n; i > 0; i--) {                 // If sum is greater then or equal            // to 0 include i in group1            // otherwise include in group2            if (group1Sum - i >= 0) {                     group1.add(i);                     // Decrease sum of group1                group1Sum -= i;            }            else {                group2.add(i);            }        }             // Print both the groups        printVector(group1);        printVector(group2);    }         // Driver code    public static void main (String[] args)    {        int n = 5;        findTwoGroup(n);    }} // This code is contributed by Gitanjali.
# Python program to divide n integers# in two groups such that absolute# difference of their sum is minimumimport math # To print vector along sizedef printVector( v):     # Print vector size    print(len(v))     # Print vector elements    for i in range( 0, len(v)):        print(v[i] , end =  " ")     print()  # To divide n in two groups such that# absolute difference of their sum is# minimumdef findTwoGroup(n):     # Find sum of all elements upto n    sum = n * (n + 1) / 2     # Sum of elements of group1    group1Sum = sum / 2     group1=[]    group2=[]    for i in range(n, 0, -1):         # If sum is greater then or equal        # to 0 include i in group 1        # otherwise include in group2        if (group1Sum - i >= 0) :            group1.append(i)             # Decrease sum of group1            group1Sum -= i                 else :            group2.append(i)     # Print both the groups    printVector(group1)    printVector(group2) # driver coden = 5findTwoGroup(n) # This code is contributed by Gitanjali.
// C# program to divide n integers// in two groups such that absolute// difference of their sum is minimumusing System;using System.Collections; class GFG{// To print vector along sizestatic void printVector(ArrayList v){    // Print vector size    Console.WriteLine(v.Count);     // Print vector elements    for (int i = 0; i < v.Count; i++)        Console.Write(v[i] + " ");     Console.WriteLine();} // To divide n in two groups// such that absolute difference// of their sum is minimumstatic void findTwoGroup(int n){    // Find sum of all elements upto n    int sum = n * (n + 1) / 2;     // Sum of elements of group1    int group1Sum = sum / 2;         ArrayList group1 = new ArrayList();    ArrayList group2 = new ArrayList();     for (int i = n; i > 0; i--)    {         // If sum is greater then        // or equal to 0 include i        // in group1 otherwise        // include in group2        if (group1Sum - i >= 0)        {            group1.Add(i);             // Decrease sum of group1            group1Sum -= i;        }        else        {            group2.Add(i);        }    }     // Print both the groups    printVector(group1);    printVector(group2);} // Driver codepublic static void Main(){    int n = 5;    findTwoGroup(n);}} // This code is contributed by mits
<?php// PHP program to divide n// integers in two groups// such that absolute// difference of their// sum is minimum // To print vector// along sizefunction printVector($v){    // Print vector size    echo count($v) . "\n";     // Print vector elements    for ($i = 0;         $i < count($v); $i++)        echo $v[$i] . " ";     echo "\n";} // To divide n in two groups// such that absolute difference// of their sum is minimumfunction findTwoGroup($n){    // Find sum of all    // elements upto n    $sum = $n * ($n + 1) / 2;     // Sum of elements    // of group1    $group1Sum = (int)($sum / 2);     $group1;    $group2;    $x = 0;    $y = 0;     for ($i = $n; $i > 0; $i--)    {         // If sum is greater then        // or equal to 0 include        // i in group 1 otherwise        // include in group2        if ($group1Sum - $i >= 0)        {             $group1[$x++] = $i;             // Decrease sum            // of group1            $group1Sum -= $i;        }        else        {            $group2[$y++] = $i;        }    }     // Print both the groups    printVector($group1);    printVector($group2);} // Driver Code$n = 5;findTwoGroup($n); // This code is contributed by mits.?>
<script>  // Javascript program to divide n integers// in two groups such that absolute// difference of their sum is minimum // To print vector along sizefunction printVector(v){    // Print vector size    document.write( v.length + "<br>");     // Print vector elements    for (var i = 0; i < v.length; i++)        document.write( v[i] + " ");     document.write("<br>");} // To divide n in two groups such that// absolute difference of their sum is// minimumfunction findTwoGroup(n){    // Find sum of all elements upto n    var sum = n * (n + 1) / 2;     // Sum of elements of group1    var group1Sum = parseInt(sum / 2);     var group1 = [], group2 = [];     for (var i = n; i > 0; i--) {         // If sum is greater then or equal        // to 0 include i in group 1        // otherwise include in group2        if (group1Sum - i >= 0) {             group1.push(i);             // Decrease sum of group1            group1Sum -= i;        }        else {            group2.push(i);        }    }     // Print both the groups    printVector(group1);    printVector(group2);} // Driver program to test above functionsvar n = 5;findTwoGroup(n); </script>
Output:  
2
5 2
3
4 3 1
 
Mithun Kumar
itsok
number-theory
Greedy
Mathematical
number-theory
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program for First Fit algorithm in Memory Management
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Max Flow Problem Introduction
Program for Worst Fit algorithm in Memory Management
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find sum of elements in a given array
Modulo Operator (%) in C/C++ with Examples