
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Print all pairs with given sum in C++
- Find all triplets with zero sum in C++
- Find all triplets in a list with given sum in Python
- 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++
- JavaScript Program to Find all triplets with zero sum
- 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 pairs in an unsorted array with equal sum in C++
- Print all possible sums of consecutive numbers with sum N in C++
- Print all integers that are sum of powers of two given numbers in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- JavaScript Program to Count triplets with sum smaller than a given value
