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));
Similar Reads
Maximum path sum in a triangle. We have given numbers in form of a triangle, by starting at the top of the triangle and moving to adjacent numbers on the row below, find the maximum total from top to bottom. Examples : Input : 3 7 4 2 4 6 8 5 9 3 Output : 23 Explanation : 3 + 7 + 4 + 9 = 23 Input : 8 -4 4 2 2 6 1 1 1 1 Output : 19
15+ min read
Possible to form a triangle from array values Given an array of integers, we need to find out whether it is possible to construct at least one non-degenerate triangle using array values as its sides. In other words, we need to find out 3 such array indices which can become sides of a non-degenerate triangle. Examples : Input : [4, 1, 2] Output
7 min read
Maximum area rectangle by picking four sides from array Given an array arr[] of positive integers representing lengths, the task is to find the maximum possible rectangular area that can be formed using four sides from the array. Note: A valid rectangle requires at least two pairs of equal values. If no such rectangle is possible, return 0.Examples: Inpu
9 min read
Program to print Sum Triangle for a given array Given a array, write a program to construct a triangle where last row contains elements of given array, every element of second last row contains sum of below two elements and so on. Example: Input: arr[] = {4, 7, 3, 6, 7};Output:8140 4121 19 2211 10 9 134 7 3 6 7 Input: {10, 40, 50}Output:14050 901
9 min read
Maximum sum of a path in a Right Number Triangle Given a right triangle of numbers, find the largest of the sum of numbers that appear on the paths starting from the top towards the base, so that on each path the next number is located directly below or below and one place to the right. Examples :Â Input : 1 1 2 4 1 2 2 3 1 1 Output : 9 Explanatio
15+ min read