C++ Program to Print all triplets in sorted array that form AP Last Updated : 18 Aug, 2023 Comments Improve Suggest changes Like Article Like Report Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression)Examples : Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 }; Output : 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Input : arr[] = { 3, 5, 6, 7, 8, 10, 12}; Output : 3 5 7 5 6 7 6 7 8 6 8 10 8 10 12 A simple solution is to run three nested loops to generate all triplets and for every triplet, check if it forms AP or not. Time complexity of this solution is O(n3)A better solution is to use hashing. We traverse array from left to right. We consider every element as middle and all elements after it as next element. To search the previous element, we use a hash table. C++ // C++ program to print all triplets in given // array that form Arithmetic Progression // C++ program to print all triplets in given // array that form Arithmetic Progression #include <bits/stdc++.h> using namespace std; // Function to print all triplets in // given sorted array that forms AP void printAllAPTriplets(int arr[], int n) { unordered_set<int> s; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { // Use hash to find if there is // a previous element with difference // equal to arr[j] - arr[i] int diff = arr[j] - arr[i]; if (s.find(arr[i] - diff) != s.end()) cout << arr[i] - diff << " " << arr[i] << " " << arr[j] << endl; } s.insert(arr[i]); } } // Driver code int main() { int arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 }; int n = sizeof(arr) / sizeof(arr[0]); printAllAPTriplets(arr, n); return 0; } Output : 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Time Complexity : O(n2) Auxiliary Space : O(n)An efficient solution is based on the fact that the array is sorted. We use the same concept as discussed in GP triplet question. The idea is to start from the second element and fix every element as a middle element and search for the other two elements in a triplet (one smaller and one greater). Below is the implementation of the above idea. C++ // C++ program to print all triplets in given // array that form Arithmetic Progression #include <bits/stdc++.h> using namespace std; // Function to print all triplets in // given sorted array that forms AP void printAllAPTriplets(int arr[], int n) { for (int i = 1; i < n - 1; i++) { // Search other two elements of // AP with arr[i] as middle. for (int j = i - 1, k = i + 1; j >= 0 && k < n;) { // if a triplet is found if (arr[j] + arr[k] == 2 * arr[i]) { cout << arr[j] << " " << arr[i] << " " << arr[k] << endl; // Since elements are distinct, // arr[k] and arr[j] cannot form // any more triplets with arr[i] k++; j--; } // If middle element is more move to // higher side, else move lower side. else if (arr[j] + arr[k] < 2 * arr[i]) k++; else j--; } } } // Driver code int main() { int arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 }; int n = sizeof(arr) / sizeof(arr[0]); printAllAPTriplets(arr, n); return 0; } Output : 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Time Complexity : O(n2) Auxiliary Space : O(1) Please refer complete article on Print all triplets in sorted array that form AP for more details! Comment More infoAdvertise with us Next Article C++ Program to Print all triplets in sorted array that form AP kartik Follow Improve Article Tags : Searching Hash C++ Programs C++ DSA Arrays cpp-unordered_set arithmetic progression +4 More Practice Tags : CPPArraysHashSearching Similar Reads C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The 3 min read C++ Program to Count triplets with sum smaller than a given value Given an array of distinct integers and a sum value. Find count of triplets with sum smaller than given sum value. The expected Time Complexity is O(n2).Examples: Input : arr[] = {-2, 0, 1, 3} sum = 2. Output : 2 Explanation : Below are triplets with sum less than 2 (-2, 0, 1) and (-2, 0, 3) Input : 4 min read C++ Program For Finding A Triplet From Three Linked Lists With Sum Equal To A Given Number Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple " 4 min read C++ Program to Find a triplet such that sum of two equals to third element Write a C++ program for a given array of integers, you have to find three numbers such that the sum of two elements equals the third element.Examples: Input: {5, 32, 1, 7, 10, 50, 19, 21, 2}Output: 21, 2, 19 Input: {5, 32, 1, 7, 10, 50, 19, 21, 0}Output: no such triplet exist Question source: Arcesi 6 min read Total number of triplets (A, B, C) in which the points B and C are Equidistant to A Given an array arr containing N points, the task is to find the total number of triplets in which the points are equidistant. A triplet of points (P1, P2, P3) is said to be equidistant when the distance between P1 and P2 is the same as of the distance between P1 and P3. Note: The order of the points 7 min read How to Find the Third Smallest Number in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the third smallest element in an array in C++. If there is no third smallest elemen 2 min read Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j] Given an array arr[] of N integers, the task is to count number of triplets (i, j, k) in the array such that a[k] < a[i] < a[j] and i < j < k.Examples: Input: arr[] = {2, 5, 1, 3, 0} Output: 4 Explanation: Below are the triplets (i, j, k) such that i < j < k and a[k] < a[i] < 8 min read C++ Program for Two Pointers Technique Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Letâs see the naive so 4 min read Count triplets (i, j, k) in an array of distinct elements such that a[i] <a> a[k] and i < j < k Given an array arr[] consisting of N distinct integers, the task is to count the number of triplets (i, j, k) possible from the array arr[] such that i < j < k and arr[i] < arr[j] > arr[k]. Examples: Input: arr[] = {2, 3, 1, -1}Output: 2Explanation: From the given array, all possible tri 8 min read C++ Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1 5 min read Like