# Divide first N natural numbers into 3 equal sum subsets

Given an integer N, the task is to check whether the elements from the range [1, N] can be divided into three non-empty equal sum subsets. If possible then print Yes else print No.

Examples:

Input: N = 5
Output: Yes
The possible subsets are {1, 4}, {2, 3} and {5}.
(1 + 4) = (2 + 3) = (5)

Input: N = 3
Output: No

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: There are two cases:

1. If N ≤ 3: In this case, it is not possible to divide the elements in the subsets that satisfy the given condition. So, print No.
2. If N > 3: In this case, it is only possible when the sum of all the elements of the range [1, N] is divisible by 3 which can be easily calculated as sum = (N * (N + 1)) / 2. Now, if sum % 3 = 0 then print Yes else print No.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true ` `// if the subsets are possible ` `bool` `possible(``int` `n) ` `{ ` ` `  `    ``// If n <= 3 then it is not possible ` `    ``// to divide the elements in three subsets ` `    ``// satisfying the given conditions ` `    ``if` `(n > 3) { ` ` `  `        ``// Sum of all the elements ` `        ``// in the range [1, n] ` `        ``int` `sum = (n * (n + 1)) / 2; ` ` `  `        ``// If the sum is divisible by 3 ` `        ``// then it is possible ` `        ``if` `(sum % 3 == 0) { ` `            ``return` `true``; ` `        ``} ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 5; ` ` `  `    ``if` `(possible(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.math.*; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function that returns true  ` `    ``// if the subsets are possible  ` `    ``public` `static` `boolean` `possible(``int` `n)  ` `    ``{  ` `     `  `        ``// If n <= 3 then it is not possible  ` `        ``// to divide the elements in three subsets  ` `        ``// satisfying the given conditions  ` `        ``if` `(n > ``3``)  ` `        ``{  ` `     `  `            ``// Sum of all the elements  ` `            ``// in the range [1, n]  ` `            ``int` `sum = (n * (n + ``1``)) / ``2``;  ` `     `  `            ``// If the sum is divisible by 3  ` `            ``// then it is possible  ` `            ``if` `(sum % ``3` `== ``0``)  ` `            ``{  ` `                ``return` `true``;  ` `            ``}  ` `        ``}  ` `        ``return` `false``;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{  ` `        ``int` `n = ``5``;  ` ` `  `        ``if` `(possible(n))  ` `            ``System.out.println(``"Yes"``);  ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Naman_Garg `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function that returns true  ` `# if the subsets are possible  ` `def` `possible(n) :  ` ` `  `    ``# If n <= 3 then it is not possible  ` `    ``# to divide the elements in three subsets  ` `    ``# satisfying the given conditions  ` `    ``if` `(n > ``3``) : ` ` `  `        ``# Sum of all the elements  ` `        ``# in the range [1, n]  ` `        ``sum` `=` `(n ``*` `(n ``+` `1``)) ``/``/` `2``;  ` ` `  `        ``# If the sum is divisible by 3  ` `        ``# then it is possible  ` `        ``if` `(``sum` `%` `3` `=``=` `0``) : ` `            ``return` `True``;  ` `     `  `    ``return` `False``;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``n ``=` `5``;  ` ` `  `    ``if` `(possible(n)) : ` `        ``print``(``"Yes"``);  ` `    ``else` `: ` `        ``print``(``"No"``);  ` `         `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function that returns true  ` `// if the subsets are possible  ` `public` `static` `bool` `possible(``int` `n)  ` `{  ` ` `  `    ``// If n <= 3 then it is not possible  ` `    ``// to divide the elements in three subsets  ` `    ``// satisfying the given conditions  ` `    ``if` `(n > 3)  ` `    ``{  ` ` `  `        ``// Sum of all the elements  ` `        ``// in the range [1, n]  ` `        ``int` `sum = (n * (n + 1)) / 2;  ` ` `  `        ``// If the sum is divisible by 3  ` `        ``// then it is possible  ` `        ``if` `(sum % 3 == 0)  ` `        ``{  ` `            ``return` `true``;  ` `        ``}  ` `    ``}  ` `    ``return` `false``;  ` `}  ` ` `  `// Driver code  ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 5;  ` ` `  `    ``if` `(possible(n))  ` `        ``Console.Write(``"Yes"``);  ` `    ``else` `        ``Console.Write(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by ajit `

Output:

```Yes
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : AnkitRai01, Naman_Garg, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.