Maximum Perimeter Triangle from array
Last Updated :
23 Apr, 2025
Given an array arr[] of positive integers. Find out the maximum perimeter of the triangle from the array.
Note: Return -1, if it is not possible to construct a triangle.
Examples:
Input: arr[] = [6, 1, 6, 5, 8, 4]
Output: 20
Explanation: Triangle formed by 8,6 & 6 has perimeter 20, which is the max possible.
Input: arr[] = [7, 55, 20, 1, 4, 33, 12]
Output: -1
Explanation: The triangle is not possible because the condition: the sum of two sides should be greater than third is not fulfilled here.
[Naive Approach] - Using Nested Loops - O(n ^ 3) Time and O(1) Space
The idea is to check for all possible combinations of three integers using nested loops, whether it forms a valid triangle (the sum of any two must be more than the third), if so, update the maximum possible perimeter.
C++
#include <bits/stdc++.h>
using namespace std;
int maxPerimeter(vector<int> &arr) {
int n = arr.size();
// to store the result
int ans = -1;
// nested loops to generall
// all possible combinations
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
for(int k = j + 1; k < n; k++) {
// check if the three sides
// can form a triangle or not
if(arr[i] + arr[j] > arr[k] &&
arr[j] + arr[k] > arr[i] &&
arr[k] + arr[i] > arr[j]) {
ans = max(ans, arr[i] + arr[j] + arr[k]);
}
}
}
}
return ans;
}
int main() {
vector<int> arr = {6, 1, 6, 5, 8, 4};
cout << maxPerimeter(arr);
return 0;
}
Java
import java.util.*;
class GfG {
// to store the result
static int maxPerimeter(int[] arr) {
int n = arr.length;
// to store the result
int ans = -1;
// nested loops to generall
// all possible combinations
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// check if the three sides
// can form a triangle or not
if (arr[i] + arr[j] > arr[k] &&
arr[j] + arr[k] > arr[i] &&
arr[k] + arr[i] > arr[j]) {
ans = Math.max(ans, arr[i] + arr[j] + arr[k]);
}
}
}
}
return ans;
}
public static void main(String[] args) {
int[] arr = {6, 1, 6, 5, 8, 4};
System.out.println(maxPerimeter(arr));
}
}
Python
# to store the result
def maxPerimeter(arr):
n = len(arr)
# to store the result
ans = -1
# nested loops to generall
# all possible combinations
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
# check if the three sides
# can form a triangle or not
if arr[i] + arr[j] > arr[k] and arr[j] + arr[k] > arr[i] and arr[k] + arr[i] > arr[j]:
ans = max(ans, arr[i] + arr[j] + arr[k])
return ans
arr = [6, 1, 6, 5, 8, 4]
print(maxPerimeter(arr))
C#
using System;
using System.Collections.Generic;
class GfG {
// to store the result
static int maxPerimeter(int[] arr) {
int n = arr.Length;
// to store the result
int ans = -1;
// nested loops to generall
// all possible combinations
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// check if the three sides
// can form a triangle or not
if (arr[i] + arr[j] > arr[k] &&
arr[j] + arr[k] > arr[i] &&
arr[k] + arr[i] > arr[j]) {
ans = Math.Max(ans, arr[i] + arr[j] + arr[k]);
}
}
}
}
return ans;
}
public static void Main(string[] args) {
int[] arr = {6, 1, 6, 5, 8, 4};
Console.WriteLine(maxPerimeter(arr));
}
}
JavaScript
// to store the result
function maxPerimeter(arr) {
let n = arr.length;
// to store the result
let ans = -1;
// nested loops to generall
// all possible combinations
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
// check if the three sides
// can form a triangle or not
if (arr[i] + arr[j] > arr[k] &&
arr[j] + arr[k] > arr[i] &&
arr[k] + arr[i] > arr[j]) {
ans = Math.max(ans, arr[i] + arr[j] + arr[k]);
}
}
}
}
return ans;
}
function main() {
let arr = [6, 1, 6, 5, 8, 4];
console.log(maxPerimeter(arr));
}
main();
[Expected Approach] - Using Sorting - (n * log n) Time and O(1) Space
The idea is to sort the array in non-increasing order so that the first element is the maximum and the last is the minimum. Then for every three consecutive elements, check if they form a triangle, the first such combination is the required answer.
Please note that, we pick the first arr[i] such that arr[i] < arr[i+1] + arr[i+2] (0 <= i <= n-3).
How does this work? For any arr[i] to be part of the triangle, it must be smaller than sum of other two. In a decreasing order sorted array if arr[i] is greater than or equal to arr[i+1] + arr[i+2], then it would be greater than all other pairs after arr[i+1]. So there is no point checking other pairs. So after sorting, we only need to check the next 2, Since the array is sorted in decreasing order, the first arr[i] satisfying the condition from left to right would give us the max result.
Below is given the step-by-step approach:
- First, sort the array in non-increasing order.
- Then, check the first three elements of the sorted array. If they satisfy the triangle inequality (i.e. arr[i] < arr[i+1] + arr[i+2]), they form a triangle with the maximum possible perimeter because any other combination will result in a lower sum.
- If the first three elements do not form a triangle, it implies that the largest element is too large (i.e., a ≥ b + c), so drop the largest element and consider the next triple.
- Repeat the process by checking consecutive triples (i.e., b, c, d; then c, d, e; and so on) until a valid triangle is found.
- Once a valid triple is identified, it gives the maximum perimeter, and further checking is unnecessary.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPerimeter(vector<int> &arr) {
int n = arr.size();
// sort the array in descending order
sort(arr.begin(), arr.end(), greater<int>());
// loop through the array
for(int i = 0; i < n - 2; i++) {
// check if the three sides can
// form a triangle or not
if(arr[i] < arr[i + 1] + arr[i + 2]) {
return arr[i] + arr[i + 1] + arr[i + 2];
}
}
return -1;
}
int main() {
vector<int> arr = {6, 1, 6, 5, 8, 4};
cout << maxPerimeter(arr);
return 0;
}
Java
import java.util.*;
class GfG {
public static int maxPerimeter(int[] arr) {
int n = arr.length;
// sort the array in descending order
Arrays.sort(arr);
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
// loop through the array
for (int i = 0; i < n - 2; i++) {
// check if the three sides can
// form a triangle or not
if (arr[i] < arr[i + 1] + arr[i + 2]) {
return arr[i] + arr[i + 1] + arr[i + 2];
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {6, 1, 6, 5, 8, 4};
System.out.println(maxPerimeter(arr));
}
}
Python
def maxPerimeter(arr):
n = len(arr)
# sort the array in descending order
arr.sort(reverse=True)
# loop through the array
for i in range(n - 2):
# check if the three sides can
# form a triangle or not
if arr[i] < arr[i + 1] + arr[i + 2]:
return arr[i] + arr[i + 1] + arr[i + 2]
return -1
arr = [6, 1, 6, 5, 8, 4]
print(maxPerimeter(arr))
C#
using System;
using System.Collections.Generic;
class GfG {
public static int maxPerimeter(int[] arr) {
int n = arr.Length;
// sort the array in descending order
Array.Sort(arr);
Array.Reverse(arr);
// loop through the array
for (int i = 0; i < n - 2; i++) {
// check if the three sides can
// form a triangle or not
if (arr[i] < arr[i + 1] + arr[i + 2]) {
return arr[i] + arr[i + 1] + arr[i + 2];
}
}
return -1;
}
public static void Main() {
int[] arr = {6, 1, 6, 5, 8, 4};
Console.WriteLine(maxPerimeter(arr));
}
}
JavaScript
function maxPerimeter(arr) {
let n = arr.length;
// sort the array in descending order
arr.sort((a, b) => b - a);
// loop through the array
for (let i = 0; i < n - 2; i++) {
// check if the three sides can
// form a triangle or not
if (arr[i] < arr[i + 1] + arr[i + 2]) {
return arr[i] + arr[i + 1] + arr[i + 2];
}
}
return -1;
}
let arr = [6, 1, 6, 5, 8, 4];
console.log(maxPerimeter(arr));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem