- 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
Picking the triangle edges with maximum perimeter JavaScript
The perimeter of a triangle is the sum of all three sides of the triangle. We are required to write a JavaScript function that takes in an array of numbers of at least three or more elements.
Our function should pick three longest sides (largest numbers) from the array that when summed can give the maximum perimeter from the array, we need to make sure that the three picked side can make a triangle in reality. If three exists no three sides in the array that can make a valid triangle, then we have to return zero.
A valid triangle is that triangle in which the sum of any two sides is always greater than the third side.
Example
const arr = [1, 2, 3, 5, 6, 7, 9]; const largestPerimeter = (arr = []) => { arr.sort((a, b) => a - b); let max = 0; for (let i = arr.length - 1; i >= 2; i--) { let start = i - 2; let end = i - 1; while (start < end) { if (arr[end] + arr[start] > arr[i]) { return arr[end] + arr[start] + arr[i]; } else { start++; }; }; }; return 0; }; console.log(largestPerimeter(arr));
Output
And the output in the console will be −
22
- Related Articles
- Valid triangle edges - JavaScript
- Maximum Perimeter Triangle from array in C++
- Picking the odd one out in JavaScript
- Find the perimeter of the triangle."
- What is perimeter ? How to find the perimeter of triangle ?
- Picking the largest elements from multidimensional array in JavaScript
- Picking index randomly from array in JavaScript
- What is the perimeter of a triangle?
- Maximum area of rectangle possible with given perimeter in C++
- Largest Perimeter Triangle in Python
- What should be done with the left/ right edges of the content on overflow with JavaScript?
- Maximum area rectangle by picking four sides from array in C++
- Maximum number of edges in Bipartite graph in C++
- Isosceles triangles with nearest perimeter using JavaScript
- Picking all the numbers present in a string in JavaScript

Advertisements