Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count non-negative triplets with sum equal to N

  • Last Updated : 15 Mar, 2021

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 = 2 
Output :
Triplets are : (0, 0, 2), (1, 0, 1), (0, 1, 1), (2, 0, 0), (0, 2, 0), (1, 1, 0)
Input : N = 50 
Output : 1326 
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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 : 
 

C++




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

Java




// 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

Python3




# 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

C#




// 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..

Javascript




<script>
 
    // Javascript program to find triplets count   
     
    // Function to find triplets count
    function triplets(N)
    {
        // Sum of first n+1 natural numbers
        return ((N + 1) * (N + 2)) / 2;
    }
     
    let N = 50;
    document.write(triplets(N));
     
</script>
Output: 
1326

 

Time Complexity : O(1)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!