

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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++
- Picking the triangle edges with maximum perimeter JavaScript
- Program to calculate area and perimeter of equilateral triangle
- Program to calculate area and perimeter of equilateral triangle in C++
- Find the area and perimeter of right triangle in PL/SQL
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle What is Equilateral Triangle in C?
- Area of the Largest Triangle inscribed in a Hexagon in C++
- Largest Number in Python
- Largest Gap in Python
- Area of the largest triangle that can be inscribed within a rectangle?
- Find the perimeter of a cylinder in Python Program
- Program to find perimeter of a polygon in Python
Advertisements