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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Maximum Perimeter Triangle
  5. Discussions

Maximum Perimeter Triangle

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 301 Discussions, By:

recency

Please Login in order to post a comment

  • thinhproee
    2 weeks ago+ 0 comments
    def maximumPerimeterTriangle(sticks):
        # Sort the array in non-decreasing order
        sticks.sort()
    
        # Iterate from the end to find the largest perimeter triangle
        for i in range(len(sticks) - 3, -1, -1):
            if sticks[i] + sticks[i+1] > sticks[i+2]:
                return [sticks[i], sticks[i+1], sticks[i+2]]
        
        # If no non-degenerate triangle exists
        return [-1]
    
    # Read input values
    n = int(input().strip())
    sticks = list(map(int, input().split()))
    
    # Call the function and print the result
    result = maximumPerimeterTriangle(sticks)
    print(*result)
    
    0|
    Permalink
  • arunjb639
    1 month ago+ 0 comments

    Python Soln:

    from itertools import combinations
    
    def traingleInequality(triangle):
        side1 = triangle[0] + triangle[1] > triangle[2]
        side2 = triangle[1] + triangle[2] > triangle[0]
        side3 = triangle[0] + triangle[2] > triangle[1]
        
        return side1 and side2 and side3
    
    def maximumPerimeterTriangle(sticks):
        sticks = sorted(sticks, reverse=True)
        comb = list(combinations(sticks, 3))
        for i in comb:
            isNonDegenerate = traingleInequality(list(i))
            if isNonDegenerate:
                return sorted(list(i))
        return [-1]
    
    0|
    Permalink
  • alban_tyrex
    2 months ago+ 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/cP4PLTgC1OE

    vector<int> maximumPerimeterTriangle(vector<int> sticks) {
        sort(sticks.begin(), sticks.end());
        for(int i = sticks.size() - 1; i >= 2; i--){
            if(sticks[i] < sticks[i - 1] + sticks[i - 2]) return {sticks[i - 2], sticks[i -1], sticks[i] };
        }
        return {-1};
    }
    
    0|
    Permalink
  • mc002
    2 months ago+ 0 comments

    JS/Javascript:

    function maximumPerimeterTriangle(sticks) {
        sticks.sort((a, b) => a - b);
        
        for (let i = sticks.length - 1; i > 1; i--) {
            const a = sticks[i - 2];
            const b = sticks[i - 1];
            const c = sticks[i];
            
            if (a + b > c) {
                return [a, b, c];
            }
        }
        return [-1];
    }
    
    0|
    Permalink
  • tiagoiesbick
    2 months ago+ 0 comments

    Python 3

    import itertools
    def maximumPerimeterTriangle(sticks):    
        p, max_side, min_side = 0,0,0
        ans = [-1]    
        for i in set(itertools.combinations(sticks,3)):
            a,b,c = i
            if (a + b <= c) or (a + c <= b) or (b + c <= a):
                continue
            elif sum(i) >= p:
                p = sum(i)
                if max(i) > max_side:
                    max_side = max(i)
                    min_side = min(i)
                    ans = sorted(i)
                elif min(i) > min_side:
                    max_side = max(i)
                    min_side = min(i)
                    ans = sorted(i)
                else:
                    ans = sorted(i)
        return ans
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy