Maximum Perimeter Triangle HackerRank Problem Solution

In this Maximum Perimeter Triangle HackerRank Problem, Given an array of stick lengths, use  of them to construct a non-degenerate triangle with the maximum possible perimeter. Return an array of the lengths of its sides as  integers in non-decreasing order.

If there are several valid triangles having the maximum perimeter:

  1. Choose the one with the longest maximum side.
  2. If more than one has that maximum, choose from them the one with the longest minimum side.
  3. If more than one has that maximum as well, print any one them.

If no non-degenerate triangle exists, return .

Example

The triplet  will not form a triangle. Neither will  or , so the problem is reduced to  and . The longer perimeter is .

Function Description

Complete the maximumPerimeterTriangle function in the editor below.

maximumPerimeterTriangle has the following parameter(s):

  • int sticks[n]: the lengths of sticks available

Returns

  • int[3] or int[1]: the side lengths of the chosen triangle in non-decreasing order or -1

Input Format

The first line contains single integer , the size of array .
The second line contains  space-separated integers , each a stick length.

Constraints

Explanation

Sample Case 0:
There are  possible unique triangles:

The second triangle has the largest perimeter, so we print its side lengths on a new line in non-decreasing order.

Sample Case 1:
The triangle  is degenerate and thus can’t be constructed, so we print -1 on a new line.

Maximum Perimeter Triangle HackerRank Problem Solutions

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in JavaScript

function isNonDegenerate(a, b, c) {
  if (a + b > c && b + c > a && a + c > b) {
    return true;
  } else {
    return false;
  }
}


function maximumPerimeterTriangle(sticks) {
  sticks = sticks.sort((a, b) => a - b);
  let triangles = [];
 
  for (let i = 0; i < sticks.length - 2; i++) {
    if (isNonDegenerate(sticks[i], sticks[i + 1], sticks[i+2])) {
      triangles.push([sticks[i], sticks[i + 1], sticks[i+2]])
    }
  }
  if(triangles.length === 0){
    return [-1];
  }else{
    return triangles.pop();
  }
}

Problem Solution in C#

public static List<int> maximumPerimeterTriangle(List<int> sticks)
  {
      sticks.Sort();
      for (int i = sticks.Count - 3; i >= 0; i--)
      {
          if (sticks[i] + sticks[i + 1] > sticks[i + 2])
          {
              return new List<int> { sticks[i], sticks[i + 1], sticks[i + 2] };
          }
      }
      return new List<int> { -1 };
  }

Maximum Perimeter Triangle Problem Solution in C++

vector<int> maximumPerimeterTriangle(vector<int> sticks) {
    int len = sticks.size();
    vector<int>triangle;
   
    unsigned long long int p = 0,s1,s2,s3;
    unsigned long long int result = 0;
   
    sort(sticks.begin(),sticks.end());
   
    for (int i = 0; i < len; i++) {
        for (int j = i+1; j < len; j++) {
            for (int k = j+1; k < len; k++) {
                if (sticks[i] + sticks[j] > sticks[k] && sticks[i] + sticks[k] > sticks[j] && sticks[j] + sticks[k] > sticks[i]) {
                    p = sticks[i] + sticks[j] + sticks[k];
                    if (result < p) {
                        result = p;
                        s1 = i;
                        s2 = j;
                        s3 = k;
                    }
                }
            }
        }
    }
    if (result != 0) {
        triangle.push_back(sticks[s1]);
        triangle.push_back(sticks[s2]);
        triangle.push_back(sticks[s3]);
    }else {
        triangle.push_back(-1);
    }
   
    return triangle;
}

Problem Solution in Python

def maximumPerimeterTriangle(sticks):
    sticks.sort(reverse = True)
    for i in range(len(sticks)-2):
        a = sticks[i]
        b = sticks[i+1]
        c = sticks[i+2]
        if a < b + c:
            return (c, b, a)
    return [-1]
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment