HackerRank Maximum Perimeter Triangle problem solution YASH PAL, 31 July 2024 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. 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} algorithm coding problems