PHP Program to Print all triplets in sorted array that form AP Last Updated : 22 Jul, 2024 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 122 12 2212 17 222 17 3212 22 329 22 352 22 4222 32 42Input : arr[] = { 3, 5, 6, 7, 8, 10, 12};Output :3 5 75 6 76 7 86 8 108 10 12A 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. PHP // PHP program to pr$all // triplets in given array // that form Arithmetic // Progression // Function to print // all triplets in // given sorted array // that forms AP function printAllAPTriplets($arr, $n) { $s = array(); for ($i = 0; $i < $n - 1; $i++) { for ($j = $i + 1; $j < $n; $j++) { // Use hash to find if // there is a previous // element with difference // equal to arr[j] - arr[i] $diff = $arr[$j] - $arr[$i]; if (in_array($arr[$i] - $diff, $arr)) echo(($arr[$i] - $diff) . " " . $arr[$i] . " " . $arr[$j] . " "); } array_push($s, $arr[$i]); } } // Driver code $arr = array(2, 6, 9, 12, 17, 22, 31, 32, 35, 42); $n = count($arr); printAllAPTriplets($arr, $n); // This code is contributed by // Manish Shaw(manishshaw1) ?> Output 6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42Complexity Analysis: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. PHP // PHP implementation to print // all the triplets in given array // that form Arithmetic Progression // Function to print all triplets in // given sorted array that forms AP function findAllTriplets($arr, $n) { for ($i = 1; $i < $n - 1; $i++) { // Search other two elements // of AP with arr[i] as middle. for ($j = $i - 1, $k = $i + 1; $j >= 0 && $k < $n;) { // if a triplet is found if ($arr[$j] + $arr[$k] == 2 * $arr[$i]) { echo $arr[$j] ." " . $arr[$i]. " " . $arr[$k] . " "; // 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 $arr = array(2, 6, 9, 12, 17, 22, 31, 32, 35, 42); $n = count($arr); findAllTriplets($arr, $n); // This code is contributed by Sam007 ?> Output6 9 12 2 12 22 12 17 22 2 17 32 12 22 32 9 22 35 2 22 42 22 32 42 Complexity Analysis: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 PHP Program to Print all triplets in sorted array that form AP kartik Follow Improve Article Tags : Searching Hash Web Technologies PHP PHP Programs DSA Arrays cpp-unordered_set arithmetic progression +5 More Practice Tags : ArraysHashSearching Similar Reads How to print all the values of an array in PHP ? We have given an array containing some array elements and the task is to print all the values of an array arr in PHP. In order to do this task, we have the following approaches in PHP:Table of ContentUsing for-each loop: Using count() function and for loop: Using implode():Using print_r() FunctionUs 3 min read PHP Program to Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are p 3 min read PHP Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe above arra 3 min read PHP Program to Sort an Array of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: [0, 1, 2, 0, 1, 2] Output: [0, 0, 1, 1, 2, 2] Input: [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1] Output: [0, 0, 0, 0 4 min read How to Sort an Array containing 1 to n values in PHP ? In this article, we will see how to sort an array that contains 1 to n values in PHP. There are various approaches through which we can sort an array with custom values. Below is an example for a better understanding of the problem statement.Example:Input: arr = [3, 1, 4, 5, 2, 8, 9, 5]Output: Sorte 6 min read PHP Program to Sort an Array in Ascending Order Sorting array elements is a common operation in programming. PHP provides several methods to achieve this operation. In this article, we will explore various approaches to sort array elements of an array in ascending order.Table of ContentUsing sort() FunctionUsing asort() FunctionUsing array_multis 3 min read PHP Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array. For example.A = {2, 3, 1, 2}, we can shift {1, 2} from the end 3 min read PHP Program to Find a triplet such that sum of two equals to third element Given an 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, 19Input: {5, 32, 1, 7, 10, 50, 19, 21, 0}Output: no such triplet existQuestion source: Arcesium Interview Experience | 3 min read PHP Program to Count Inversions of size three in a given array Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.Example : Input: {8, 4, 2, 1}Output: 4The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1).Input: {9 4 min read PHP Program For Counting Inversions In An Array - Set 1 (Using Merge Sort) Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers 2 min read Like