Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Maximum Perimeter Triangle problem solution

YASH PAL, 31 July 202424 January 2026

In this HackerRank Maximum Perimeter Triangle problem solution we have given an array of stick lengths, use 3 of them to construct a non-degenerate triangle with the maximum possible perimeter. Return an array of the lengths of its sides as 3 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 [-1].

HackerRank Maximum Perimeter Triangle problem solution HackerRank Maximum Perimeter Triangle problem solution in Python.

n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
for i in range(0,n-2):
x = a[n-1-i]
y = a[n-2-i]
z = a[n-3-i]
if y + z > x:
print(str(z) + " "+ str(y) + " " + str(x))
exit()
print(-1)

Maximum Perimeter Triangle problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] sticks = new int[n];
for(int i=0; i < n; i++){
sticks[i] = in.nextInt();
}
Arrays.sort(sticks);
int trianglePosition = n-3;
while ((trianglePosition>=0) && (sticks[trianglePosition] + sticks[trianglePosition+1] <= sticks[trianglePosition+2])) {
trianglePosition--;
}
if (trianglePosition < 0) {
System.out.println(-1);
} else {
System.out.println(sticks[trianglePosition] + " " + sticks[trianglePosition + 1] + " " + sticks[trianglePosition + 2]);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}

Problem solution in C++.

#include<bits/stdc++.h>

using namespace std;
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define FORE(ii,aa,bb) for(int ii=aa;ii<bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define type(x) __typeof(x.begin())

#define bit(x,y) ((x>>y)&1)
#define y1 fkfrgff
#define ll long long
#define pii pair<int,int>
#define mod 1000000007
#define N (int)(2e5+10)
#define mp make_pair
#define pb push_back
#define sd second
#define ft first
#define endll puts("")
#define endl 'n'
#define inf mod
#define ort ((sol+sag)/2)
int n,a[N];
vector<pair<pii,pii > >ans;
int main(){
    cin >> n;
    FOR(i,1,n) scanf("%d",&a[i]);
    FOR(i,1,n)
        FOR(j,i+1,n)
            FOR(k,j+1,n){
                vector<int>v;
                v.pb(a[i]);
                v.pb(a[j]);
                v.pb(a[k]);
                sort(v.rbegin(),v.rend());
                if(v[0] < v[1] + v[2])
                    ans.pb(mp(mp(v[0]+v[1]+v[2],v[0]),mp(v[2],v[1])));

            }
    if(!ans.size()) return puts("-1"),0;
    sort(ans.rbegin(),ans.rend());
    printf("%d %d %d",ans[0].sd.ft , ans[0].sd.sd , ans[0].ft.sd);
}

Problem solution in C.

#include <stdio.h>
void sort(int *a, int n)
{

int *i,*j,*max_i, temp;
for (i=a;i<a+n;i++)
{
max_i=i;
for(j=i+1;j<a+n;j++)
{
if(*max_i<*j)
max_i=j;
}
temp=*i;
*i=*max_i;
*max_i=temp;
}
}
int main()
{
int i,n;
scanf("%d",&n);
int array[n];
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

sort(array,n);
int c=0;
for(i=0;i<n-2;i++)
{
if(array[i]<array[i+1]+array[i+2])
{
printf("%d %d %d",array[i+2],array[i+1],array[i]);
c++;
break;
}
}
if(c==0)
printf("%d",-1);
return 0;

}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes