# Check if a number can be represented as a sum of 2 triangular numbers

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 = 24
Output: YES
24 can be represented as 3+21.

Input: N = 15
Output: NO
```

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

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 ` `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; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to check if it is possible or not  ` `    ``static` `boolean` `checkTriangularSumRepresentation(``int` `n)  ` `    ``{ ` `        ``HashSet tri = ``new` `HashSet<>(); ` `        ``int` `i = ``1``; ` ` `  `        ``// Store all triangular numbers up to N in a Set  ` `        ``while` `(``true``)  ` `        ``{ ` `            ``int` `x = i * (i + ``1``) / ``2``; ` `            ``if` `(x >= n)  ` `            ``{ ` `                ``break``; ` `            ``} ` `            ``tri.add(x); ` `            ``i++; ` `        ``} ` ` `  `        ``// Check if a pair exists  ` `        ``for` `(Integer tm : tri)  ` `        ``{ ` `            ``if` `(tri.contains(n - tm) && (n - tm) !=  ` `                ``(``int``) tri.toArray()[tri.size() - ``1``]) ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `        ``} ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `n = ``24``; ` `        ``if` `(checkTriangularSumRepresentation(n))  ` `        ``{ ` `            ``System.out.println(``"Yes"``); ` `        ``} ` `        ``else` `        ``{ ` `            ``System.out.println(``"No"``); ` `        ``} ` `    ``} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## 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         `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to check if it is possible or not  ` `    ``static` `bool` `checkTriangularSumRepresentation(``int` `n)  ` `    ``{ ` `        ``HashSet<``int``> tri = ``new` `HashSet<``int``>(); ` `        ``int` `i = 1; ` ` `  `        ``// Store all triangular numbers up to N in a Set  ` `        ``while` `(``true``)  ` `        ``{ ` `            ``int` `x = i * (i + 1) / 2; ` `            ``if` `(x >= n)  ` `            ``{ ` `                ``break``; ` `            ``} ` `            ``tri.Add(x); ` `            ``i++; ` `        ``} ` ` `  `        ``// Check if a pair exists  ` `        ``foreach` `(``int` `tm ``in` `tri)  ` `        ``{ ` `            ``if` `(tri.Contains(n - tm)) ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `        ``} ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `n = 24; ` `        ``if` `(checkTriangularSumRepresentation(n))  ` `        ``{ ` `            ``Console.WriteLine(``"Yes"``); ` `        ``} ` `        ``else` `        ``{ ` `            ``Console.WriteLine(``"No"``); ` `        ``} ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## PHP

 `= ``\$n``) ` `            ``break``;  ` `             `  `        ``array_push``(``\$tri``, ``\$x``);  ` `        ``\$i` `+= 1;  ` `    ``} ` `     `  `    ``// Check if a pair exists  ` `    ``foreach``(``\$tri` `as` `\$tm``) ` `        ``if` `(in_array(``\$n` `- ``\$tm``, ``\$tri``)) ` `            ``return` `true;  ` `             `  `    ``return` `false;  ` `} ` ` `  `// Driver Code  ` `\$n` `= 24;  ` ` `  `if` `(checkTriangularSumRepresentation(``\$n``)) ` `    ``print``(``"Yes"``); ` `else` `    ``print``(``"No"``); ` ` `  `// This code is contributed by mits ` `?> `

Output:

```Yes
```

Time Complexity: O(Sqrt(N))

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

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.