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 .
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
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 :
# Python3 program to find triplets count
# Function to find triplets count
# Sum of first n+1 natural numbers
return ((N + 1) * (N + 2)) // 2;
# Driver code
N = 50;
# Function call
# This code is contributed by nidhi
Time Complexity : O(1)
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Count numbers whose XOR with N is equal to OR with N
- Count numbers whose difference with N is equal to XOR with N
- Count pairs with set bits sum equal to K
- Count of quadruplets from range [L, R] having GCD equal to K
- Count pairs from two arrays having sum equal to K
- Count the numbers < N which have equal number of divisors as K
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count pairs of natural numbers with GCD equal to given number
- Maximum count of equal numbers in an array after performing given operations
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count number of subsets of a set with GCD equal to a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.