India’s Best Job Seekers and Training Platform › Forums › Algorithm Solutions Discussion › Given a sorted array of distinct positive integers, print all triplets …

Given a sorted array of distinct positive integers, print all triplets …

Given a sorted array of distinct positive integers, print all triplets that forms Arithmetic Progression with integral common difference. A Arithmetic Progression is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with common difference of 2.
For example,
Input: A[] = { 5, 8, 9, 11, 12, 15 }
Output:
5 8 11
9 12 15
Input: A[] = { 1, 3, 5, 6, 8, 9, 15 }
Output:
1 3 5
1 5 9
3 6 9
1 8 15
3 9 15
The idea is to consider every element as middle element of Arithmetic Progression starting from the second element and search other two elements of Arithmetic triplet. We know that for an element A[j] to be middle of Arithmetic progression, there exist two elements A[i] and A[k] such that –(A[j] – A[i] = A[k] – A[j]) or (A[i] + A[k] == 2 * A[j])
Log in to reply.