

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Triplets such that one of the numbers can be written as sum of the other two in C++
We are given an array Arr[] of integers with length n. The goal is to find the number of triplets (Arr[i],Arr[j],Arr[k]) such that the sum of any two numbers is equal to the third number.
a+b=c, where a,b,c are elements of Arr[] with indexes i,j,k such that 0<=i<j<k<n We will do this by using three for loops. Increment count if arr[x]+arr[y]=arr[z] and x!=y!=z. Let’s understand with examples.
Input
arr[]= { 1,2,2,3,4 }, N=5
Output
Number of triplets: 4
Explanation
Triplets with arr[x]+arr[y]=arr[z].
Arr{}=[ 1,2,2,3,4 ] =(1,2,3) → 1+2=3 Arr{}=[ 1,2,2,3,4 ] =(1,2,3) → 1+2=3 Arr{}=[ 1,2,2,3,4 ] =(1,3,4) → 1+3=4 Arr{}=[ 1,2,2,3,4 ] =(2,2,4) → 2+2=4
Total triplets: 4
Input
arr[]= {2,2,2,2,2}, N=5
Output
Number of triplets: 0
Explanation
Every two numbers have sum=4 which is not equal to third=2.
Total triplets: 0
Approach used in the below program is as follows
We take an integer array Arr[] initialized with random numbers.
Variable N stores the length of Arr[].
Function countTriplets(int arr[],int n) takes an array, its length returns the triplets in which one of the numbers can be written as sum of the other two
Take the initial variable count as 0 for the number of triplets.
Traverse array using three for loops for each element of the triplet.
Outermost loop from 0<=i<n-2, inner loop i<j<n-1, innermost j<k<n.
Check if arr[i]+arr[j]==arr[k] or arr[i]+arr[k]==arr[j] or arr[k]+arr[j]==arr[i] If true then increment count.
At the end of all loops count will have a total number of triplets that meet the condition.
Return the count as result.
Example
#include <bits/stdc++.h> using namespace std; int countTriplets(int arr[], int n){ int count = 0; for (int i = 0; i < n-2; i++){ for (int j = i+1; j < n-1; j++){ for (int k = j+1; k < n; k++){ if(arr[i]+arr[j]==arr[k] || arr[j]+arr[k]==arr[i] || arr[k]+arr[i]==arr[j]){ count++; } } } } return count; } int main(){ int Arr[]={ 1,2,2,3,4 }; int N=5; //length of array cout <<endl<< "Number of triplets : "<<countTriplets(Arr,N); return 0; }
Output
Number of triplets : 4
- Related Questions & Answers
- Find if n can be written as product of k numbers in C++
- Count Triplets That Can Form Two Arrays of Equal XOR in C++
- Count numbers which can be represented as sum of same parity primes in C++
- Check if a number can be written as sum of three consecutive integers in C++
- Count pairs in an array such that frequency of one is at least value of other in C++
- Count array elements that divide the sum of all other elements in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Program to check a number can be written as a sum of distinct factorial numbers or not in Python
- Sum of two numbers where one number is represented as array of digits in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++
- Count numbers which can be constructed using two numbers in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Count the total number of squares that can be visited by Bishop in one move in C++