Given N and D, find if it is possible to make two sets from first N natural numbers such that the difference between the sum of 2 sets(individually) is D.

Examples :

Input : 5 7 Output : yes Explanation: Keeping 1 and 3 in one set, and 2, 4 and 5 are in other set. Sum of set 1 = 4 Sum of set 2 = 11 So, the difference D = 7 Which is the required difference Input : 4 5 Output : no

**Approach :**

Let s1 and s2 be the two sets.

Here we know that

sum(s1) + sum(s2) = N*(N+1)/2 and

sum(s1) – sum(s2) = DAdding above 2 equations, we get

2*sum(s1) = N*(N+1)/2 + D

If sum(S1) and sum(S2) are integers, then only we can split the first N natural numbers into two sets. For that N*(N+1)/2 + D must be an even number.

## C++

`// C++ program for implementing ` `// above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function returns true if it is ` `// possible to split into two ` `// sets otherwise returns false ` `bool` `check(` `int` `N, ` `int` `D) ` `{ ` ` ` `int` `temp = (N * (N + 1)) / 2 + D; ` ` ` `return` `(temp % 2 == 0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `M = 7; ` ` ` `if` `(check(N, M)) ` ` ` `cout << ` `"yes"` `; ` ` ` `else` ` ` `cout << ` `"no"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for implementing ` `// above approach ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function returns true if it is ` ` ` `// possible to split into two ` ` ` `// sets otherwise returns false ` ` ` `static` `boolean` `check(` `int` `N, ` `int` `D) ` ` ` `{ ` ` ` `int` `temp = (N * (N + ` `1` `)) / ` `2` `+ D; ` ` ` `return` `(temp % ` `2` `== ` `0` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `main (String args[]) ` ` ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `int` `M = ` `7` `; ` ` ` `if` `(check(N, M)) ` ` ` `System.out.println(` `"yes"` `); ` ` ` `else` ` ` `System.out.println(` `"no"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program for implementing ` `# above approach ` ` ` `# Function returns true if it is ` `# possible to split into two ` `# sets otherwise returns false ` `def` `check(N, D): ` ` ` `temp ` `=` `N ` `*` `(N ` `+` `1` `) ` `/` `/` `2` `+` `D ` ` ` `return` `(` `bool` `(temp ` `%` `2` `=` `=` `0` `)) ` ` ` `# Driver code ` `N ` `=` `5` `M ` `=` `7` `if` `check(N, M): ` ` ` `print` `(` `"yes"` `) ` `else` `: ` ` ` `print` `(` `"no"` `) ` ` ` `# This code is contributed by Shrikant13. ` |

*chevron_right*

*filter_none*

## C#

`// C# program for implementing ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function returns true if it is ` ` ` `// possible to split into two ` ` ` `// sets otherwise returns false ` ` ` `static` `bool` `check(` `int` `N, ` `int` `D) ` ` ` `{ ` ` ` `int` `temp = (N * (N + 1)) / 2 + D; ` ` ` `return` `(temp % 2 == 0); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `M = 7; ` ` ` `if` `(check(N, M)) ` ` ` `Console.Write(` `"yes"` `); ` ` ` `else` ` ` `Console.Write(` `"no"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for implementing ` `// above approach ` ` ` `// Function returns true if it is ` `// possible to split into two ` `// sets otherwise returns false ` `function` `check(` `$N` `, ` `$D` `) ` `{ ` ` ` `$temp` `= (` `$N` `* (` `$N` `+ 1)) / 2 + ` `$D` `; ` ` ` `return` `(` `$temp` `% 2 == 0); ` `} ` ` ` `// Driver code ` `$N` `= 5; ` `$M` `= 7; ` `if` `(check(` `$N` `, ` `$M` `)) ` ` ` `echo` `(` `"yes"` `); ` `else` ` ` `echo` `(` `"no"` `); ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

**OUTPUT :**

yes

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 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
- Sum of square-sums of first n natural numbers
- Replace all elements by difference of sums of positive and negative numbers after that element
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Minimum number of sets with numbers less than Y
- Difference between sums of odd and even digits
- Maximum and minimum sums from two numbers with digit replacements
- Minimum count of numbers required with unit digit X that sums up to N
- Maximum absolute difference of value and index sums
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Natural Numbers
- LCM of First n Natural Numbers
- Sum of first n natural numbers
- Repeated sum of first N natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of first N natural numbers which are divisible by X or Y

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.