Sort array and then apply binary search.
On Wed, May 25, 2011 at 11:31 AM, immanuel kingston <
[email protected]> wrote:
> Brute force Approach would be
>
>
> int checkForTriangle(int a, int b, int c) {
> return (a + b > c) && (b + c > a) && (a + c > b);
> }
>
> int triangle (int a[], int n) {
> if (a == null || n <= 0) return 0;
>
> for (int i=0 ; i < n ; i++) {
> for (int j=i + 1; j < n; j++ ) {
> for (int k=j + 1; k < n; k++ ) {
> if (checkForTriangle(a[i], a[j], a[k])) return 1;
> }
> }
> }
> return 0;
> }
>
> Thanks,
> Immanuel
>
>
> On Wed, May 25, 2011 at 11:23 PM, Piyush Sinha
> <[email protected]>wrote:
>
>> Write a function
>>
>> int triangle( int A [ ] )
>>
>> that given a zero-indexed array A consisting of N integers returns 1
>> if there exists a triple (P, Q, R) such that 0 <= P < Q < R < N and
>> A[P] + A[Q] > A[R],
>> A[Q] + A[R] > A[P],
>> A[R] + A[P] > A[Q].
>> The function should return 0 if such triple does not exist.
>>
>> For example, given array A such that
>>
>> A[0]=10, A[1]=2, A[2]=5, A[3]=1, A[4]=8, A[5]=20
>>
>> the function should return 1, because the triple (0, 2, 4) fulfills
>> all of the required conditions.
>>
>> For array A such that
>>
>> A[0]=10, A[1]=50, A[2]=5, A[3]=1
>>
>> the function should return 0
>>
>> --
>> *Piyush Sinha*
>> *IIIT, Allahabad*
>> *+91-8792136657*
>> *+91-7483122727*
>> *https://www.facebook.com/profile.php?id=100000655377926 *
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
-Aakash Johari
(IIIT Allahabad)
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.