# Closest sum partition (into two subsets) of numbers from 1 to n

Given an integer sequence **1, 2, 3, 4, …, n**. The task is to divide it into two sets **A** and **B** in such a way that each element belongs to exactly one set and **|sum(A) – sum(B)|** is the minimum possible. Print the value of **|sum(A) – sum(B)|**.

**Examples:**

Input:3

Output:0

A = {1, 2} and B = {3} ans |sum(A) – sum(B)| = |3 – 3| = 0.

Input:6

Output:0

A = {1, 3, 4} and B = {2, 5} ans |sum(A) – sum(B)| = |3 – 3| = 0.

Input:5

Output:1

**Approach:** Take **mod = n % 4**,

- If
**mod = 0**or**mod = 3**then print**0**. - If
**mod = 1**or**mod = 2**then print**1**.

This is because the groups will be chosen as A = {N, N – 3, N – 4, N – 7, N – 8, …..}, B = {N – 1, N – 2, N – 5, N – 6, …..}

Starting from N to 1, place 1st element in group A then alternate every 2 elements in B, A, B, A, …..

- When
**n % 4 = 0:**N = 8, A = {1, 4, 5, 8} and B = {2, 3, 6, 7} - When
**n % 4 = 1:**N = 9, A = {1, 4, 5, 8, 9} and B = {2, 3, 6, 7} - When
**n % 4 = 2:**N = 10, A = {1, 4, 5, 8, 9} and B = {2, 3, 6, 7, 10} - When
**n % 4 = 3:**N = 11, A = {1, 4, 5, 8, 9} and B = {2, 3, 6, 7, 10, 11}

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum required ` `// absolute difference ` `int` `minAbsDiff(` `int` `n) ` `{ ` ` ` `int` `mod = n % 4; ` ` ` ` ` `if` `(mod == 0 || mod == 3) ` ` ` `return` `0; ` ` ` ` ` `return` `1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << minAbsDiff(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum required ` `// absolute difference ` ` ` ` ` `static` `int` `minAbsDiff(` `int` `n) ` ` ` `{ ` ` ` `int` `mod = n % ` `4` `; ` ` ` `if` `(mod == ` `0` `|| mod == ` `3` `) ` ` ` `{ ` ` ` `return` `0` `; ` ` ` `} ` ` ` `return` `1` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(minAbsDiff(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-JI ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum required ` `# absolute difference ` `def` `minAbsDiff(n) : ` ` ` `mod ` `=` `n ` `%` `4` `; ` ` ` ` ` `if` `(mod ` `=` `=` `0` `or` `mod ` `=` `=` `3` `) : ` ` ` `return` `0` `; ` ` ` ` ` `return` `1` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; ` ` ` `print` `(minAbsDiff(n)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum ` ` ` `// required absolute difference ` ` ` `static` `int` `minAbsDiff(` `int` `n) ` ` ` `{ ` ` ` `int` `mod = n % 4; ` ` ` `if` `(mod == 0 || mod == 3) ` ` ` `{ ` ` ` `return` `0; ` ` ` `} ` ` ` `return` `1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(minAbsDiff(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by akt_mit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the minimum ` `// required absolute difference ` `function` `minAbsDiff(` `$n` `) ` `{ ` ` ` `$mod` `= ` `$n` `% 4; ` ` ` ` ` `if` `(` `$mod` `== 0 || ` `$mod` `== 3) ` ` ` `return` `0; ` ` ` ` ` `return` `1; ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `echo` `minAbsDiff(` `$n` `); ` ` ` `// This code is contributed by Tushil. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Bell Numbers (Number of ways to Partition a Set)
- Sum of sum of all subsets of a set formed by first N natural numbers
- Sum of all subsets of a set formed by first n natural numbers
- Multiple of x closest to n
- Find the multiple of x which is closest to a^b
- Bitwise AND of sub-array closest to K
- Partition a number into two divisble parts
- Closest perfect square and its distance
- Find K Closest Points to the Origin
- Find the number closest to n and divisible by m
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Find the closest and smaller tidy number
- Find a triplet in an array whose sum is closest to a given number
- Find multiple of x closest to or a ^ b (a raised to power b)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.