Cerinta completa

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

Sample Input 0

5
1 1 1 3 3

Sample Output 0

1 3 3

Explanation 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 Input 1

3
1 2 3

Sample Output 1

-1

Explanation 1

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

Sample Input 2

6
1 1 1 2 3 5

Sample Output 2

1 1 1

Explanation 2

The triangle (1,1,1) is the only valid triangle.


Limbajul de programare folosit: java8

Cod:

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int[] sticks = new int[n];
		for (int i = 0; i < sticks.length; i++) {
			sticks[i] = sc.nextInt();
		}

		int[] result = solve(sticks);
		System.out.println(
				result == null ? -1 : Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" ")));

		sc.close();
	}

	static int[] solve(int[] sticks) {
		int[] result = null;
		for (int i = 0; i < sticks.length; i++) {
			for (int j = i + 1; j < sticks.length; j++) {
				for (int k = j + 1; k < sticks.length; k++) {
					if (isTriangle(sticks[i], sticks[j], sticks[k])) {
						int[] solution = { sticks[i], sticks[j], sticks[k] };
						Arrays.sort(solution);

						if (result == null || (solution[2] > result[2])
								|| (solution[2] == result[2] && solution[0] > result[0])) {
							result = solution;
						}
					}
				}
			}
		}
		return result;
	}

	static boolean isTriangle(int a, int b, int c) {
		return a + b > c && b + c > a && c + a > b;
	}
}

Scor obtinut: 1.0

Submission ID: 464603129

Link challenge: https://www.hackerrank.com/challenges/maximum-perimeter-triangle/problem

Maximum Perimeter Triangle