Maximum Perimeter Triangle HackerRank Solution


Maximum Perimeter Triangle HackerRank Solution
Source : https://www.hackerrank.com/challenges/maximum-perimeter-triangle



Source : https://www.hackerrank.com/challenges/maximum-perimeter-triangle


Solution


// Karthikalapati.blogspot.com
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
// Time complexity: O(n log n)
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
Integer [] stick = new Integer[N];
for (int i = 0; i < N; i++) {
stick[i] = scan.nextInt();
}
scan.close();
findTriangle(stick);
}
/* Greedy Approach: Try to use the biggest sticks first */
private static void findTriangle(Integer [] stick) {
Arrays.sort(stick, Collections.reverseOrder());
for (int i = 0; i < stick.length - 2; i++) {
if (stick[i] < stick[i+1] + stick[i+2]) {
System.out.println(stick[i+2] + " " + stick[i+1] + " " + stick[i]);
return;
}
}
System.out.println(-1);
}
}

2 comments:

  1. If you are looking for where to buy Black Cherry Gelato, then here you are at the right place. Black Cherry Gelato is a 60/40 Indica predominant cross of Black Cherry Funk and Acai.

    ReplyDelete