# Check whether a number can be represented as sum of K distinct positive integers

Given two integers **N** and **K**, the task is to check whether **N** can be represented as sum of **K** distinct positive integers.

**Examples:**

Input:N = 12, K = 4

Output:Yes

N = 1 + 2 + 4 + 5 = 12 (12 as sum of 4 distinct integers)

Input:N = 8, K = 4

Output:No

**Approach:** Consider the series **1 + 2 + 3 + … + K** which has exactly **K** distinct integers with minimum possible sum i.e. **Sum = (K * (K – 1)) / 2**. Now, if **N < Sum** then it is not possible to represent **N** as the sum of **K** distinct positive integers but if **N ≥ Sum** then any integer say **X ≥ 0** can be added to **Sum** to generate the sum equal to **N** i.e. **1 + 2 + 3 + … + (K – 1) + (K + X)** ensuring that there are exactly **K** distinct positive integers.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that returns true if n ` `// can be represented as the sum of ` `// exactly k distinct positive integers ` `bool` `solve(` `int` `n, ` `int` `k) ` `{ ` ` ` `// If n can be represented as ` ` ` `// 1 + 2 + 3 + ... + (k - 1) + (k + x) ` ` ` `if` `(n >= (k * (k + 1)) / 2) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 12, k = 4; ` ` ` ` ` `if` `(solve(n, k)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns true if n ` ` ` `// can be represented as the sum of ` ` ` `// exactly k distinct positive integers ` ` ` `static` `boolean` `solve(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// If n can be represented as ` ` ` `// 1 + 2 + 3 + ... + (k - 1) + (k + x) ` ` ` `if` `(n >= (k * (k + ` `1` `)) / ` `2` `) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `12` `, k = ` `4` `; ` ` ` ` ` `if` `(solve(n, k)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function that returns true if n ` `# can be represented as the sum of ` `# exactly k distinct positive integers ` `def` `solve(n,k): ` ` ` `# If n can be represented as ` ` ` `# 1 + 2 + 3 + ... + (k - 1) + (k + x) ` ` ` `if` `(n >` `=` `(k ` `*` `(k ` `+` `1` `)) ` `/` `/` `2` `): ` ` ` `return` `True` ` ` ` ` `return` `False` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `12` ` ` `k ` `=` `4` ` ` ` ` `if` `(solve(n, k)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true if n ` ` ` `// can be represented as the sum of ` ` ` `// exactly k distinct positive integers ` ` ` `static` `bool` `solve(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// If n can be represented as ` ` ` `// 1 + 2 + 3 + ... + (k - 1) + (k + x) ` ` ` `if` `(n >= (k * (k + 1)) / 2) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 12, k = 4; ` ` ` ` ` `if` `(solve(n, k)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// PHP implementation of the approach ` ` ` `// Function that returns true if n ` `// can be represented as the sum of ` `// exactly k distinct positive integers ` `function` `solve(` `$n` `, ` `$k` `) ` `{ ` ` ` `// If n can be represented as ` ` ` `// 1 + 2 + 3 + ... + (k - 1) + (k + x) ` ` ` `if` `(` `$n` `>= (` `$k` `* (` `$k` `+ 1)) / 2) { ` ` ` `return` `true; ` ` ` `} ` ` ` ` ` `return` `false; ` `} ` ` ` `// Driver code ` ` ` `$n` `= 12; ` `$k` `= 4; ` ` ` `if` `(solve(` `$n` `, ` `$k` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by ihritik ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Check if N can be represented as sum of integers chosen from set {A, B}
- Check whether product of integers from a to b is positive , negative or zero
- Find the smallest positive number which can not be represented by given digits
- Check if the sum of distinct digits of two integers are equal
- Number of arrays of size N whose elements are positive integers and sum is K
- Number of distinct integers obtained by lcm(X, N)/X
- Check whether a number can be represented by sum of two squares
- Check if a number can be represented as sum of non zero powers of 2
- Check if given number can be represented as sum of two great numbers
- Check if a given number can be represented in given a no. of digits in any base
- Check if a number can be represented as a sum of 2 triangular numbers
- C program to Check Whether a Number is Positive or Negative or Zero
- Check whether a number has exactly three distinct factors or not
- Check if a number can be written as sum of three consecutive integers
- Ways to write N as sum of two or more positive integers | Set-2

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.