> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/greedy/perimeter-with-conditions.md).

# Perimeter with conditions

Aahad gives an array of integers and asks Harshit to find which three elements form a triangle (non-degenerate). The task seems easy to Harshit.

So, Aahad adds some conditions to this task -

1\. Find the triangle with maximum perimeter

2\. If there are two or more combinations with same value of maximum perimeter, then find the one with the longest side.

3.If there are more than one combinations which satisfy all the above conditions the find with maximum longest minimum side.

**Input Format**

```
The First line contains no of elements of array: N
Each T lines contains N space-separated integers: A [i]
```

**Output Format**

```
The output contains three space-separated elements that denote the length of the sides of triangle. If no such triangle is possible, then print -1.
```

**Constraints**

```
1 =< N <= 10^5 
1 <= A[i] <= 10^9
Time Limit: 1 second
```

**Sample Input1:**

```
5
1 1 1 3 3
```

**Sample Output1:**

```
1 3 3 
```

**Sample Input2:**

```
3
2 2 4
```

**Sample Output3:**

```
-1 
```

**Explaination**

```
In the First Sample case, the elements that form a triangle with maximum perimeter is 1,3,3.
In the Second Sample case, the elements that can form a triangle are degenerate, so, we printed -1.
```

```java
public class Main {
    public static List<Integer> findTriangle(int n, int[] arr) {
        List<Integer> ans = new ArrayList<>();
        if (arr.length < 3) {
            ans.add(-1);
            return ans;
        }
        Arrays.sort(arr);
        // Choose greedily from last
        for (int i = n - 1; i >= 2; i--) {
            if (arr[i - 1] + arr[i - 2] > arr[i]) {
                ans.addAll(Arrays.asList(arr[i - 2], arr[i - 1], arr[i]));
                break;
            }
        }
        // If no combination found
        if (ans.size() == 0)
            ans.add(-1);
        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/greedy/perimeter-with-conditions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
