Divide 1 to n into two groups with minimum sum difference
Last Updated :
20 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void printVector(vector< int > v)
{
cout << v.size() << endl;
for ( int i = 0; i < v.size(); i++)
cout << v[i] << " " ;
cout << endl;
}
void findTwoGroup( int n)
{
int sum = n * (n + 1) / 2;
int group1Sum = sum / 2;
vector< int > group1, group2;
for ( int i = n; i > 0; i--) {
if (group1Sum - i >= 0) {
group1.push_back(i);
group1Sum -= i;
}
else {
group2.push_back(i);
}
}
printVector(group1);
printVector(group2);
}
int main()
{
int n = 5;
findTwoGroup(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void printVector(Vector<Integer> v)
{
System.out.println(v.size());
for ( int i = 0 ; i < v.size(); i++)
System.out.print(v.get(i) + " " );
System.out.println();
}
static void findTwoGroup( int n)
{
int sum = n * (n + 1 ) / 2 ;
int group1Sum = sum / 2 ;
Vector<Integer> group1 = new Vector<Integer>();
Vector<Integer> group2 = new Vector<Integer>();
for ( int i = n; i > 0 ; i--) {
if (group1Sum - i >= 0 ) {
group1.add(i);
group1Sum -= i;
}
else {
group2.add(i);
}
}
printVector(group1);
printVector(group2);
}
public static void main (String[] args)
{
int n = 5 ;
findTwoGroup(n);
}
}
|
Python3
import math
def printVector( v):
print ( len (v))
for i in range ( 0 , len (v)):
print (v[i] , end = " " )
print ()
def findTwoGroup(n):
sum = n * (n + 1 ) / 2
group1Sum = sum / 2
group1 = []
group2 = []
for i in range (n, 0 , - 1 ):
if (group1Sum - i > = 0 ) :
group1.append(i)
group1Sum - = i
else :
group2.append(i)
printVector(group1)
printVector(group2)
n = 5
findTwoGroup(n)
|
C#
using System;
using System.Collections;
class GFG
{
static void printVector(ArrayList v)
{
Console.WriteLine(v.Count);
for ( int i = 0; i < v.Count; i++)
Console.Write(v[i] + " " );
Console.WriteLine();
}
static void findTwoGroup( int n)
{
int sum = n * (n + 1) / 2;
int group1Sum = sum / 2;
ArrayList group1 = new ArrayList();
ArrayList group2 = new ArrayList();
for ( int i = n; i > 0; i--)
{
if (group1Sum - i >= 0)
{
group1.Add(i);
group1Sum -= i;
}
else
{
group2.Add(i);
}
}
printVector(group1);
printVector(group2);
}
public static void Main()
{
int n = 5;
findTwoGroup(n);
}
}
|
PHP
<?php
function printVector( $v )
{
echo count ( $v ) . "\n" ;
for ( $i = 0;
$i < count ( $v ); $i ++)
echo $v [ $i ] . " " ;
echo "\n" ;
}
function findTwoGroup( $n )
{
$sum = $n * ( $n + 1) / 2;
$group1Sum = (int)( $sum / 2);
$group1 ;
$group2 ;
$x = 0;
$y = 0;
for ( $i = $n ; $i > 0; $i --)
{
if ( $group1Sum - $i >= 0)
{
$group1 [ $x ++] = $i ;
$group1Sum -= $i ;
}
else
{
$group2 [ $y ++] = $i ;
}
}
printVector( $group1 );
printVector( $group2 );
}
$n = 5;
findTwoGroup( $n );
?>
|
Javascript
<script>
function printVector(v)
{
document.write( v.length + "<br>" );
for ( var i = 0; i < v.length; i++)
document.write( v[i] + " " );
document.write( "<br>" );
}
function findTwoGroup(n)
{
var sum = n * (n + 1) / 2;
var group1Sum = parseInt(sum / 2);
var group1 = [], group2 = [];
for ( var i = n; i > 0; i--) {
if (group1Sum - i >= 0) {
group1.push(i);
group1Sum -= i;
}
else {
group2.push(i);
}
}
printVector(group1);
printVector(group2);
}
var n = 5;
findTwoGroup(n);
</script>
|
Output:
2
5 2
3
4 3 1
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...