Given an integer **N**, the task is to find out whether it can be written as a sum of 2 triangular numbers (which may or may not be distinct).

**Examples:**

Input:N = 24Output:YES 24 can be represented as 3+21.Input:N = 15Output:NO

**Approach:**

Consider all triangular numbers less than **N** i.e. sqrt(N) numbers. Let’s add them to a set, and for each triangular number **X** we check if **N-X** is present in the set. If it is true with any triangular number, then the answer is YES, otherwise the answer is NO.

Below is the implemnetation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if it is possible or not ` `bool` `checkTriangularSumRepresentation(` `int` `n) ` `{ ` ` ` `unordered_set<` `int` `> tri; ` ` ` `int` `i = 1; ` ` ` ` ` `// Store all triangular numbers up to N in a Set ` ` ` `while` `(1) { ` ` ` `int` `x = i * (i + 1) / 2; ` ` ` `if` `(x >= n) ` ` ` `break` `; ` ` ` `tri.insert(x); ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Check if a pair exists ` ` ` `for` `(` `auto` `tm` `: tri) ` ` ` `if` `(tri.find(n - ` `tm` `) != tri.end()) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 24; ` ` ` `checkTriangularSumRepresentation(n) ? cout << ` `"Yes"` ` ` `: cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 implementation of the above approach ` ` ` `# Function to check if it is possible or not ` `def` `checkTriangularSumRepresentation(n) : ` ` ` ` ` `tri ` `=` `list` `(); ` ` ` `i ` `=` `1` `; ` ` ` ` ` `# Store all triangular numbers ` ` ` `# up to N in a Set ` ` ` `while` `(` `1` `) : ` ` ` `x ` `=` `i ` `*` `(i ` `+` `1` `) ` `/` `/` `2` `; ` ` ` `if` `(x >` `=` `n) : ` ` ` `break` `; ` ` ` ` ` `tri.append(x); ` ` ` `i ` `+` `=` `1` `; ` ` ` ` ` `# Check if a pair exists ` ` ` `for` `tm ` `in` `tri : ` ` ` `if` `n ` `-` `tm ` `in` `tri: ` ` ` `return` `True` `; ` ` ` ` ` `return` `False` `; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `24` `; ` ` ` ` ` `if` `checkTriangularSumRepresentation(n) : ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(Sqrt(N))

## Recommended Posts:

- Check if given number can be represented as sum of two great numbers
- Check whether a number can be represented by sum of two squares
- Check if a given number can be represented in given a no. of digits in any base
- Triangular Numbers
- Sum of the series 1, 3, 6, 10... (Triangular Numbers)
- Check if N can be represented as sum of integers chosen from set {A, B}
- Multiply Large Numbers represented as Strings
- Count numbers which can be represented as sum of same parity primes
- Modulo power for large numbers represented as strings
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a number can be written as a sum of 'k' prime numbers
- Check if a number can be expressed as sum two abundant numbers
- Centered triangular number
- Squared triangular number (Sum of cubes)
- Smallest triangular number larger than p