Given an array A consisting of N positive integers and for any ordered triplet( i, j, k) such that i, j, and k are all distinct and 0 ≤ i, j, k < N, the value of this triplet is (A_{i }− A_{j})⋅A_{k, }the task is to find maximum value among all distinct ordered triplets.
Note: Two triplets (a, b, c) and (d, e, f) are considered to be different if at least one of the conditions is satisfied such that a ≠ d or b ≠ e or c ≠ f. As an example, (1, 2, 3) and (2, 3, 1) are two different ordered triplets.
Examples:
Input: A[] = {1, 1, 3}
Output: 2
Explanation: The desired triplet is (2, 1, 0), which yields the value of (A_{i}−A_{j})⋅A_{k} = (3 − 1)⋅1 = 2.Input: A[] = {3, 4, 4, 1, 2}
Output: 12
Approach: The problem can be solved based on the following observation:
Sort the array A in increasing order. Since we want to maximize the value of (A_{i }− A_{j}).A_{k}, and all elements in A are positive, it is best to maximise both (A_{i }− A_{j}) and A_{k}. There are two options:
 Select largest and smallest element in A as A_{i} and A_{j}, and choose second maximum element in A as A_{k}. The value is (A_{N1 }− A_{0}).A_{N−2}
 Choose the maximum element as A_{k} and choose the second maximum element, and the minimum element as A_{i} and A_{j}, getting a triplet of value (A_{N2 }− A_{0}).A_{N1}
Since A_{N – 2} ≤ A_{N1}, we can prove that (A_{N1 }− A_{0}).A_{N−2} ≥ (A_{N2 }− A_{0}).A_{N1}. Hence, the maximum value we can obtain is
(A_{N1 }− A_{0}).A_{N−2 }
Follow the below steps to solve the problem:
 Sort the array in ascending order.
 Find the difference between the maximum and minimum element of the sorted array
 Then multiply the difference with the second maximum element of the sorted array to get the answer.
Below is the implementation of the above approach.
Java

Time Complexity: O(N * log(N))
Auxiliary Space: O(1)