Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Maximum Perimeter Triangle problem solution

YASH PAL, 31 July 202430 November 2025

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.

HackerRank Maximum Perimeter Triangle problem solution

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)

{“mode”:”full”,”isActive”:false}

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. */
    }
}

{“mode”:”full”,”isActive”:false}

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);
}

{“mode”:”full”,”isActive”:false}

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;
	
}

{“mode”:”full”,”isActive”:false}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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