Question Name:MAXIMUM PERIMETER TRIANGLE

#include <bits/stdc++.h>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define mod 1000000007
#define N 1000009
#define ss(s) cin>>s;
#define si(x)  scanf("%d",&x);
#define sl(x)  cin>>x;

string a,b,c;
int q,n,t;
int arr[N],brr[N],freq[N];
int main(){cin>>n;
           for(int i=0;i<n;i++)cin>>arr[i];
           sort(arr,arr+n);q=-1;
           for(int i=n-1;i>=2;i--){
               if(arr[i]<arr[i-1]+arr[i-2]){q=i;break;}




           }
           if(q==-1)cout<<q;
           else cout<<arr[q-2]<<" "<<arr[q-1]<<" "<<arr[q];
return 0;

          }
  • Problem Description
    Given n sticks of lengths l0,l1,.,ln-1, use 3 of the sticks to construct a non-degenerate triangle with the maximum possible perimeter. Then print the lengths of its sides as 3 space-separated integers in non-decreasing order.

    If there are several valid triangles having the maximum perimeter: 

    1. Choose the one with the longest maximum side (i.e., the largest value for the longest side of any valid triangle having the maximum perimeter). 
    2. If more than one such triangle meets the first criterion, choose the one with the longest minimum side (i.e., the largest value for the shortest side of any valid triangle having the maximum perimeter). 
    3. If more than one such triangle meets the second criterion, print any one of the qualifying triangles.

    If no non-degenerate triangle exists, print -1.

    Input Format

    The first line contains single integer, n, denoting the number of sticks. 

    The second line contains n space-separated integers, l0,l1,.,ln-1, describing the respective stick lengths.

    Constraints
    3 <= n <= 50
    1 <= li <= 10^9

    Output Format

    Print 3 non-decreasing space-separated integers, a ,b , and c (where a <= b <= c) describing the respective lengths of a triangle meeting the criteria in the above Problem Statement.
    If no non-degenerate triangle can be constructed, print -1.
  • Test Case 1
    Input (stdin)5
    1 1 1 3 3
    Expected Output1 3 3
  • Test Case 2
    Input (stdin)3
    1 2 3
    Expected Output-1

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro