- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Largest Perimeter Triangle in Python
Suppose we have an array A of positive lengths, we have to find the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. When it is impossible to form any triangle of non-zero area, then return 0.
So, if the input is like [3,6,2,3], then the output will be 8.
To solve this, we will follow these steps −
- sort the list A
- a := delete last element from A
- b := delete last element from A
- c := delete last element from A
- while b+c <= a, do
- if not A is non-zero, then
- return 0
- a := b
- b := c
- c := delete last element from A
- if not A is non-zero, then
- return a+b+c
Let us see the following implementation to get better understanding −
Example
class Solution: def largestPerimeter(self, A): A.sort() a, b, c = A.pop(), A.pop(), A.pop() while b+c<=a: if not A: return 0 a, b, c = b, c, A.pop() return a+b+c ob = Solution() print(ob.largestPerimeter([3,6,2,3]))
Input
[3,6,2,3]
Output
8
- Related Articles
- Program to find largest perimeter triangle using Python
- Largest Triangle Area in Python
- Maximum Perimeter Triangle from array in C++
- Find Perimeter of a triangle in C++
- What is perimeter ? How to find the perimeter of triangle ?
- Find the perimeter of the triangle."
- What is the perimeter of a triangle?
- Explain the perimeter and area of a triangle.
- Picking the triangle edges with maximum perimeter JavaScript
- Largest Number in Python
- Largest Gap in Python
- Find the Perimeter of Triangle ABC.Find the Perimeter of Rectangle BCDE. from the following figure."
- Area of the Largest Triangle inscribed in a Hexagon in C++
- Program to calculate area and perimeter of equilateral triangle
- Program to calculate area and perimeter of equilateral triangle in C++

Advertisements