We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Maximum Perimeter Triangle
  5. Discussions

Maximum Perimeter Triangle

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • Ardiya
    5 years ago+ 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;
    
    10|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature