

- 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
Print all triplets with given sum in C++
In this problem, we are given an array of unique integers and a sum. And we have to find the triplets which can form the same sum.
Let's take an example to solve the problem −
Input : array = {0 , 2 , -1 , 1, -2} Sum = 1 Output : 1 2 -2 0 2 -1
To solve this problem, we will be finding all the triplets that provide the sum. A simple approach will be using three loops and finding the sum of the elements and return the adequate triplet.
Example
#include <iostream> using namespace std; void Triplets(int arr[], int n, int sum){ 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] == sum) { cout<<arr[i]<<"\t"<<arr[j]<<"\t"<<arr[k]<<endl; } } } } } // Driver code int main(){ int arr[] = { 0 , 2 , -1 , 1, -2 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The Triplets are : \n"; Triplets(arr, n, 1); return 0; }
Output
The Triplets are −
0 2 -1 2 1 -2
But this approach is not efficient as it will have time complexity of o(n3) for running three loops. So, we will use other techniques to solve this problem in an effective way.
One method is using hashing. In this method, we will find pairs of every element of such that they are a complement each other i.e. for the element with value x, we need an element -x.
This reduces the time complexity of the code.
Example
#include <bits/stdc++.h> using namespace std; void Triplets(int arr[], int n, int sum{ for (int i = 0; i < n - 1; i++) { unordered_set<int> triplet; for (int j = i + 1; j < n; j++) { int third = sum - (arr[i] + arr[j]); if (triplet.find(third) != triplet.end()) cout<<third<<"\t"<<arr[i]<<"\t"<<arr[j]<<endl; else triplet.insert(arr[j]); } } } int main(){ int arr[] = { 0 , 2 , -1 , 1, -2 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The Triplets are : \n"; Triplets(arr, n, 1); return 0; }
Output
The Triplets are −
0 2 -1 2 1 -2
This method can be made more effective by sorting the array which will reduce the space complexity of the code.
- Related Questions & Answers
- Find all triplets in a list with given sum in Python
- Print all pairs with given sum in C++
- Find all triplets with zero sum in C++
- All unique triplets that sum up to a given value in C++
- Print triplets with sum less than or equal to k in C Program
- Print all subarrays with 0 sum in C++
- Print all triplets in sorted array that form AP in C++
- C++ Program to find out the sum of shortest cost paths for all given triplets
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Print all integers that are sum of powers of two given numbers in C++
- Print all pairs in an unsorted array with equal sum in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- Print all possible sums of consecutive numbers with sum N in C++
- Print all permutations of a given string
- Print all sequences of given length in C++