
- 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
Find a triplet such that sum of two equals to third element in C++
Suppose there is an array of n numbers. We have to find three numbers, such that sum of two elements is same as the third one. So if the array is like [5, 32, 1, 7, 10, 50, 19, 21, 2], the output will be 21, 2, 19. If no such element has found, display that message.
To solve this, we will follow some steps as follows −
Sort the given array
Then start fixing the greatest element from the last element and traverse the array to find other two numbers which sum up to the third element.
Take two pointers j and k, j is from first, k is from last to find the smallest of two numbers from i - 1 to find the largest of the two remaining numbers.
If the addition of both the numbers is still less than Arr[i], then we have to increase the value of the summation of two numbers, thereby increase the j pointer, so as to increase the value of Arr[j] + Arr[k]
If the addition of both the numbers is more than Arr[i], then we need to decrease the value of the summation of two numbers, thereby decrease the pointer k, such that decrease the overall value of Arr[j] + Arr[k]
Example
#include<iostream> #include<algorithm> #define N 5 using namespace std; void getValueTriplet(int arr[], int n) { sort(arr, arr + n); for (int i = n - 1; i >= 0; i--) { int j = 0; int k = i - 1; while (j < k) { if (arr[i] == arr[j] + arr[k]) { cout << "The numbers are " << arr[i] << " " << arr[j] << " " << arr[k] << endl; return; } else if (arr[i] > arr[j] + arr[k]) j += 1; else k -= 1; } } cout << "No such triplet exists"; } int main() { int arr[] = { 5, 32, 1, 7, 10, 50, 19, 21, 2 }; int n = sizeof(arr) / sizeof(arr[0]); getValueTriplet(arr, n); }
Output
The numbers are 21 2 19
- Related Articles
- JavaScript Program to Find a triplet such that sum of two equals to third element
- Find a triplet that sum to a given value in C++
- Find three element from different three arrays such that that a + b + c = sum in Python
- The two numbers of a Pythagorean triplet are 100 and 2499. Find the third number of this Pythagorean triplet.
- Find triplet such that number of nodes connecting these triplets is maximum in C++
- Find an element in array such that sum of left array is equal to sum of right array using c++
- JavaScript Program to Find a triplet that sum to a given value
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Maximum sum such that no two elements are adjacent in C++
- Find a point such that sum of the Manhattan distances is minimize in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum triplet sum in array in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Count all Quadruples from four arrays such that their XOR equals to ‘x’ in C++
- Find numbers whose sum of digits equals a value
