Count non-negative triplets with sum equal to N
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 : 6
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
Naive Approach:
We can use three nested loops to generate all possible ordered triplets of non-negative integers such that their sum is equal to the given number N.
Implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countTriplets( int N)
{
int res = 0;
for ( int i = 0; i <= N; i++) {
for ( int j = 0; j <= N; j++) {
for ( int k = 0; k <= N; k++) {
if (i + j + k == N)
res++;
}
}
}
return res;
}
int main()
{
int N = 50;
cout << countTriplets(N);
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
public static int countTriplets( int N) {
int res = 0 ;
for ( int i = 0 ; i <= N; i++) {
for ( int j = 0 ; j <= N; j++) {
for ( int k = 0 ; k <= N; k++) {
if (i + j + k == N)
res++;
}
}
}
return res;
}
public static void main(String[] args) {
int N = 50 ;
System.out.println(countTriplets(N));
}
}
|
Python3
def countTriplets(N):
res = 0
for i in range (N + 1 ):
for j in range (N + 1 ):
for k in range (N + 1 ):
if i + j + k = = N:
res + = 1
return res
N = 50
print (countTriplets(N))
|
C#
using System;
public class GFG{
public static int CountTriplets( int N) {
int res = 0;
for ( int i = 0; i <= N; i++) {
for ( int j = 0; j <= N; j++) {
for ( int k = 0; k <= N; k++) {
if (i + j + k == N)
res++;
}
}
}
return res;
}
public static void Main( string [] args) {
int N = 50;
Console.WriteLine(CountTriplets(N));
}
}
|
Javascript
function countTriplets(N) {
let res = 0;
for (let i = 0; i <= N; i++) {
for (let j = 0; j <= N; j++) {
for (let k = 0; k <= N; k++) {
if (i + j + k == N)
res++;
}
}
}
return res;
}
let N = 50;
console.log(countTriplets(N));
|
Time Complexity: O(N^3)
Auxiliary Space: O(1)
Efficient 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++
#include <bits/stdc++.h>
using namespace std;
int triplets( int N)
{
return ((N + 1) * (N + 2)) / 2;
}
int main()
{
int N = 50;
cout << triplets(N);
return 0;
}
|
Java
class GFG
{
static int triplets( int N)
{
return ((N + 1 ) * (N + 2 )) / 2 ;
}
public static void main(String[] args)
{
int N = 50 ;
System.out.println(triplets(N));
}
}
|
Python3
def triplets(N):
return ((N + 1 ) * (N + 2 )) / / 2 ;
N = 50 ;
print (triplets(N))
|
C#
using System;
class GFG
{
static int triplets( int N)
{
return ((N + 1) * (N + 2)) / 2;
}
public static void Main()
{
int N = 50;
Console.WriteLine(triplets(N));
}
}
|
Javascript
<script>
function triplets(N)
{
return ((N + 1) * (N + 2)) / 2;
}
let N = 50;
document.write(triplets(N));
</script>
|
Time Complexity : O(1)
Auxiliary Space: O(1)
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...