Given an integer **N**, split the first **N** natural numbers into two sets such that the absolute difference between their sum is minimum. The task is to print the minimum absolute difference that can be obtained.

**Examples**:

Input:N = 5Output:1Explanation:

Split the first N (= 5) natural numbers into sets {1, 2, 5} (sum = 8) and {3, 4} (sum = 7).

Therefore, the required output is 1.

Input:N = 6Output:1

**Naive Approach**: This problem can be solved using the Greedy technique. Follow the steps below to solve the problem:

- Initialize two variables, say
**sumSet1**and**sumSet2**to store the sum of the elements from the two sets. - Traverse first
**N**natural numbers from**N**to**1**. For every number, check if the current sum of elements in set1 is less than or equal to the sum of elements in set2. If found to be true, add the currently traversed number into**set1**and update**sumSet1**. - Otherwise, add the value of the current natural number to
**set2**and update**sumSet2**. - Finally, print
**abs(sumSet1 – sumSet2)**as the required answer.

Below is the implementation of the above approach:

## C++14

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to split the first N` `// natural numbers into two sets` `// having minimum absolute` `// difference of their sums` `int` `minAbsDiff(` `int` `N)` `{` ` ` `// Stores the sum of` ` ` `// elements of set1` ` ` `int` `sumSet1 = 0;` ` ` `// Stores the sum of` ` ` `// elements of set2` ` ` `int` `sumSet2 = 0;` ` ` `// Traverse first N` ` ` `// natural numbers` ` ` `for` `(` `int` `i = N; i > 0; i--) {` ` ` `// Check if sum of elements of` ` ` `// set1 is less than or equal` ` ` `// to sum of elements of set2` ` ` `if` `(sumSet1 <= sumSet2) {` ` ` `sumSet1 += i;` ` ` `}` ` ` `else` `{` ` ` `sumSet2 += i;` ` ` `}` ` ` `}` ` ` `return` `abs` `(sumSet1 - sumSet2);` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 6;` ` ` `cout << minAbsDiff(N);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `import` `java.io.*;` ` ` `class` `GFG{` ` ` `// Function to split the first N` `// natural numbers into two sets` `// having minimum absolute` `// difference of their sums` `static` `int` `minAbsDiff(` `int` `N)` `{` ` ` ` ` `// Stores the sum of` ` ` `// elements of set1` ` ` `int` `sumSet1 = ` `0` `;` ` ` ` ` `// Stores the sum of` ` ` `// elements of set2` ` ` `int` `sumSet2 = ` `0` `;` ` ` ` ` `// Traverse first N` ` ` `// natural numbers` ` ` `for` `(` `int` `i = N; i > ` `0` `; i--) ` ` ` `{` ` ` ` ` `// Check if sum of elements of` ` ` `// set1 is less than or equal` ` ` `// to sum of elements of set2` ` ` `if` `(sumSet1 <= sumSet2) ` ` ` `{` ` ` `sumSet1 += i;` ` ` `}` ` ` `else` ` ` `{` ` ` `sumSet2 += i;` ` ` `}` ` ` `}` ` ` `return` `Math.abs(sumSet1 - sumSet2);` `}` `// Driver code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `N = ` `6` `;` ` ` ` ` `System.out.println(minAbsDiff(N));` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to split the first N` `# natural numbers into two sets` `# having minimum absolute` `# difference of their sums` `def` `minAbsDiff(N):` ` ` ` ` `# Stores the sum of` ` ` `# elements of set1` ` ` `sumSet1 ` `=` `0` ` ` `# Stores the sum of` ` ` `# elements of set2` ` ` `sumSet2 ` `=` `0` ` ` `# Traverse first N` ` ` `# natural numbers` ` ` `for` `i ` `in` `reversed` `(` `range` `(N ` `+` `1` `)):` ` ` ` ` `# Check if sum of elements of` ` ` `# set1 is less than or equal` ` ` `# to sum of elements of set2` ` ` `if` `sumSet1 <` `=` `sumSet2:` ` ` `sumSet1 ` `=` `sumSet1 ` `+` `i` ` ` `else` `:` ` ` `sumSet2 ` `=` `sumSet2 ` `+` `i` ` ` ` ` `return` `abs` `(sumSet1 ` `-` `sumSet2)` `# Driver Code` `N ` `=` `6` `print` `(minAbsDiff(N))` `# This code is contributed by sallagondaavinashreddy7` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function to split the first N` `// natural numbers into two sets` `// having minimum absolute` `// difference of their sums` `static` `int` `minAbsDiff(` `int` `N)` `{` ` ` ` ` `// Stores the sum of` ` ` `// elements of set1` ` ` `int` `sumSet1 = 0;` ` ` ` ` `// Stores the sum of` ` ` `// elements of set2` ` ` `int` `sumSet2 = 0;` ` ` ` ` `// Traverse first N` ` ` `// natural numbers` ` ` `for` `(` `int` `i = N; i > 0; i--)` ` ` `{` ` ` ` ` `// Check if sum of elements of` ` ` `// set1 is less than or equal` ` ` `// to sum of elements of set2` ` ` `if` `(sumSet1 <= sumSet2) ` ` ` `{` ` ` `sumSet1 += i;` ` ` `}` ` ` `else` ` ` `{` ` ` `sumSet2 += i;` ` ` `}` ` ` `}` ` ` `return` `Math.Abs(sumSet1 - sumSet2);` `}` `// Driver code` `static` `void` `Main() ` `{` ` ` `int` `N = 6;` ` ` ` ` `Console.Write(minAbsDiff(N));` `}` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

**Output**

1

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

**Efficient Approach**: To optimize the above approach the idea is based on the following observations:

Splitting any 4 consecutive integers into 2 sets gives the minimum absolute difference of their sum equal to 0.

Mathematical proof:

Considering 4 consecutive integers {a_{1}, a_{2}, a_{3}, a_{4}}

a_{4}= a_{3}+ 1

a_{1}=a_{2}– 1

=> a_{4 }+_{ }a_{1 }= a_{3}+ 1 + a_{2}– 1

=> a_{4 }+_{ }a_{1}= a_{2 }+_{ }a_{3}

Follow the steps below to solve the problem:

- If
**N % 4 == 0**or**N % 4 == 3**, then print**0**. - Otherwise, print
**1**.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to split the first N` `// natural numbers into two sets` `// having minimum absolute` `// difference of their sums` `int` `minAbsDiff(` `int` `N)` `{` ` ` `if` `(N % 4 == 0 || N % 4 == 3) {` ` ` `return` `0;` ` ` `}` ` ` `return` `1;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 6;` ` ` `cout << minAbsDiff(N);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to split the first N` `// natural numbers into two sets` `// having minimum absolute` `// difference of their sums` `static` `int` `minAbsDiff(` `int` `N)` `{` ` ` `if` `(N % ` `4` `== ` `0` `|| N % ` `4` `== ` `3` `)` ` ` `{` ` ` `return` `0` `;` ` ` `}` ` ` `return` `1` `;` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `N = ` `6` `;` ` ` ` ` `System.out.println(minAbsDiff(N));` `}` `}` `// This code is contributed by sallagondaavinashreddy7` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to split the first N` `# natural numbers into two sets` `# having minimum absolute` `# difference of their sums` `def` `minAbsDiff(N):` ` ` ` ` `if` `(N ` `%` `4` `=` `=` `0` `or` `N ` `%` `4` `=` `=` `3` `):` ` ` `return` `0` ` ` ` ` `return` `1` `# Driver Code` `N ` `=` `6` `print` `(minAbsDiff(N))` `# This code is contributed by sallagondaavinashreddy7` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to split the first N` `// natural numbers into two sets` `// having minimum absolute` `// difference of their sums` `static` `int` `minAbsDiff(` `int` `N)` `{` ` ` `if` `(N % 4 == 0 || ` ` ` `N % 4 == 3)` ` ` `{` ` ` `return` `0;` ` ` `}` ` ` `return` `1;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `N = 6;` ` ` `Console.WriteLine(minAbsDiff(N));` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output**

1

**Time Complexity:** O(1)**Auxiliary Space:** O(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:

- Split squares of first N natural numbers into two sets with minimum absolute difference of their sums
- Split N natural numbers into two sets having GCD of their sums greater than 1
- First N natural can be divided into two sets with given difference and co-prime sums
- Possible two sets from first N natural numbers difference of sums as D
- Partition first N natural number into two sets such that their sum is not coprime
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Sum of square-sums of first n natural numbers
- Partition the array into two odd length groups with minimized absolute difference between their median
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Split an array containing N elements into K sets of distinct elements
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Check if product of first N natural numbers is divisible by their sum
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Split array into K subsets to maximize their sum of maximums and minimums
- Divide first N natural numbers into 3 equal sum subsets
- Maximum and minimum sums from two numbers with digit replacements

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.