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 →

  • AbhishekVermaIIT
    6 years ago+ 32 comments

    There's no need to complicate it, here's a simple Python solution :

    n = int(input())
    A = sorted(int(i) for i in input().split())
    
    i = n-3
    while i >= 0 and (A[i] + A[i+1] <= A[i+2]) :
        i -= 1
    
    if i >= 0 :
        print(A[i],A[i+1],A[i+2])
    else :
        print(-1)
    

    Logic : Select the longest possible side such that it can form a non-degenerate triangle using the two sides "just smaller" than it.

    It fulfills all other conditions. If no such selection is possible, then no non-degenerate triangle exists.

    Informative Tweets for Inquisitive Minds

    188|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature