Checking a triplet (P, Q, R) is triangular

86 Views Asked by At

If a sorted array is given, where elements are represented as A[0],A[1],A[2] etc... To check below condition,

A[P] + A[Q] > A[R],
A[Q] + A[R] > A[P],
A[R] + A[P] > A[Q]

we can write, for i=0;

if( A[i]+A[i+1]>A[i+2] && A[i]+A[i+2]>A[i+1] && A[i+2]+A[i+1]>A[i])

Also, we can reduce it to

if ( A[i] > A[i+2] - A[i+1])

I dont understand the math behind the reduced form.

1

There are 1 best solutions below

1
On BEST ANSWER

Assuming the arrays are filled with positive numbers.

The key is that the list is sorted in a non-decreasing manner.

We already know that $$A_{i+2} \ge A_{i+1}$$ from the beginning and hence clearly, we have $$A_{i}+A_{i+2}>A_{i+1}$$

Similarly for the other condition.