Maximum Perimeter Triangle

  • + 4 comments

    Non-generate triangle happens when a side of triangle exceed the sum of the other 2 sides. Imagine a triangle with side of 1, 1, and 2; it becomes a line, not a triangle :p. This is the function check the non-generate with input of integer a, b, c where a <= b <= c:

    bool isvalid(int a, int b, int c){
        return a+b>c;
    }
    

    then the problem become sorting followed by triangle validation.

    int n;
    cin>>n;
    vector<int> v(n);
    for(int i = 0; i<n; ++i){
        cin>>v[i];
    }
    sort(v.begin(), v.end(), /* descending */);
    
    for(int i = 2; i<n; ++i){
        if(isvalid(v[i], v[i-1], v[i-2])){
            cout<<v[i]<<" "<<v[i-1]<<" "<<v[i-2]<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;