Company: Deutsche_Bank_26nov
Difficulty: medium
Triangle Problem Description An array A consisting of N integers is given. A triplet of indices (P, Q, R) is called a triangle if 0 ≤ P < Q < R < N and: A[P] + A[Q] > A[R] , A[Q] + A[R] > A[P] , A[R] + A[P] > A[Q] . Such a triangle's perimeter is A[P] + A[Q] + A[R] . Write a function that, given the array A , returns the largest perimeter achievable among all valid triangle triplets, or -1 if no triplet of indices satisfies the triangle condition. The strict ordering only applies to the indices P, Q, R themselves — the values at those indices are allowed to repeat, so having A[P] = A[Q] is perfectly fine. Examples Example 1: Input: A = [10, 2, 5, 1, 8, 20] Output: 23 Explanation: Triplet (0, 2, 4) is a triangle and its perimeter equals 10 + 5 + 8 = 23. There is no other triangle in this array with a larger perimeter. Example 2: Input: A = [5, 10, 18, 7, 8, 3] Output: 25 Explanation: The triangle with the maximum perimeter is (1, 3, 4), with a perimeter of 10 + 7 + 8 = 25. E