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

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.

## Recommended Posts:

- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Partition of a set into K subsets with equal sum using BitMask and DP
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Divide first N natural numbers into 3 equal sum subsets
- Partition a number into two divisible parts
- Partition the array into two odd length groups with minimized absolute difference between their median
- Check if it is possible to split given Array into K odd-sum subsets
- Split array into K subsets to maximize their sum of maximums and minimums
- Sum of sum of all subsets of a set formed by first N natural numbers
- Partition N into M parts such that difference between Max and Min part is smallest
- Split Array into min number of subsets with difference between each pair greater than 1
- Minimize the absolute difference of sum of two subsets
- Sum of all subsets of a set formed by first n natural numbers
- Bell Numbers (Number of ways to Partition a Set)
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Find a triplet in an array whose sum is closest to a given number
- Subsets of size K with product equal to difference of two perfect squares
- Count non-adjacent subsets from numbers arranged in Circular fashion

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.