Given an integer **N**. The task is to find the number of different ordered triplets(a, b, c) of non-negative integers such that **a + b + c = N **.

**Examples:**

Input :N = 2Output :6

Triplets are : (0, 0, 2), (1, 0, 1), (0, 1, 1), (2, 0, 0), (0, 2, 0), (1, 1, 0)

Input :N = 50Output :1326

**Approach :**

First, it is easy to see that for each non-negative integer **N**, the equation **a + b = N** can be satisfied by **(N+1)** different ordered pairs of **(a, b).** Now we can assign **c** values from **0** to **N** then the ordered pairs for **a+b** can be found. It will form a series of **N+1** natural numbers and its sum will give the count of triplets.

Below is the implementation of the above approach :

`// CPP program to find triplets count ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to find triplets count ` `int` `triplets(` `int` `N) `
`{ ` ` ` `// Sum of first n+1 natural numbers `
` ` `return` `((N + 1) * (N + 2)) / 2; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `N = 50; `
` ` ` ` `// Function call `
` ` `cout << triplets(N); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java program to find triplets count ` `class` `GFG `
`{ ` ` ` `// Function to find triplets count ` `static` `int` `triplets(` `int` `N) `
`{ ` ` ` `// Sum of first n+1 natural numbers `
` ` `return` `((N + ` `1` `) * (N + ` `2` `)) / ` `2` `; `
`} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) `
`{ ` ` ` `int` `N = ` `50` `; `
` ` ` ` `System.out.println(triplets(N)); `
`} ` `} ` ` ` `// This code is contributed ` `// by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

`# Python3 program to find triplets count ` ` ` `# Function to find triplets count ` `def` `triplets(N): `
` ` ` ` `# Sum of first n+1 natural numbers `
` ` `return` `((N ` `+` `1` `) ` `*` `(N ` `+` `2` `)) ` `/` `/` `2` `; `
` ` `# Driver code ` `N ` `=` `50` `; `
` ` `# Function call ` `print` `(triplets(N)) `
` ` `# This code is contributed by nidhi ` |

*chevron_right*

*filter_none*

`// C# program to find triplets count ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `// Function to find triplets count ` `static` `int` `triplets(` `int` `N) `
`{ ` ` ` `// Sum of first n+1 natural numbers `
` ` `return` `((N + 1) * (N + 2)) / 2; `
`} ` ` ` `// Driver code ` `public` `static` `void` `Main() `
`{ ` ` ` `int` `N = 50; `
` ` ` ` `Console.WriteLine(triplets(N)); `
`} ` `} ` ` ` `// This code is contributed ` `// by anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

1326

**Time Complexity :** O(1)

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:

- Count number of triplets with product equal to given number with duplicates allowed
- Minimize subset addition or multiplication to make two given triplets equal
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Count of ordered triplets (R, G, B) in a given original string
- Count of triplets of numbers 1 to N such that middle element is always largest
- Count of triplets in a given Array having GCD K
- Count of triplets in an Array such that A[i] * A[j] = A[k] and i < j < k
- Count of ways to split N into Triplets forming a Triangle
- Rearrange array to maximize count of triplets (i, j, k) such that arr[i] > arr[j] < arr[k] and i < j < k
- Count triplets having product 0 from a given array
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Find sum of xor of all unordered triplets of the array
- Generate Pythagorean Triplets
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Twin Pythagorean triplets in an array
- Reduce a given Binary Array to a single element by removal of Triplets
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Check if count of even divisors of N is equal to count of odd divisors

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.