
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find largest perimeter triangle using Python
Suppose we have an array nums of positive lengths, we have to find the largest perimeter of a triangle, by taking three values from that array. When it is impossible to form any triangle of non-zero area, then return 0.
So, if the input is like [8,3,6,4,2,5], then the output will be 19.
To solve this, we will follow these steps −
sort the list nums
a := delete last element from nums
b := delete last element from nums
c := delete last element from nums
while b+c <= a, do
if not nums is non-zero, then
return 0
a := b
b := c
c := delete last element from nums
return a+b+c
Let us see the following implementation to get better understanding −
Example
def solve(nums): nums.sort() a, b, c = nums.pop(), nums.pop(), nums.pop() while b+c<=a: if not nums: return 0 a, b, c = b, c, nums.pop() return a+b+c nums = [8,3,6,4,2,5] print(solve(nums))
Input
[8,3,6,4,2,5]
Output
19
- Related Articles
- Largest Perimeter Triangle in Python
- JavaScript program to find perimeter of a triangle
- Program to find second largest digit in a string using Python
- What is perimeter ? How to find the perimeter of triangle ?
- Largest Triangle Area in Python
- Python Program to Find the Largest value in a Tree using Inorder Traversal
- Program to find perimeter of a polygon in Python
- Python Program to find largest element in an array
- Python program to find largest number in a list
- Program to Find K-Largest Sum Pairs in Python
- Program to find lexicographically largest mountain list in Python
- Program to find largest submatrix with rearrangements in Python
- Python Program to Find the Second Largest Number in a List Using Bubble Sort
- Program to calculate area and perimeter of equilateral triangle
- Program to find the perimeter of a rhombus using diagonals

Advertisements