Skip to content

Commit 10372a3

Browse files
Added Largest Perimeter leetcode problem solution in c++
1 parent 701f4db commit 10372a3

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
!*.*
44
!*/
55
.vscode
6+
.DS_Store
7+
testCPP/*
68
Coding-ninjas-competitive
79
Competitive-Coding
810
Dynamic-Programming
911
leetcode_company_wise_questions-master
1012
comptetive-programming_interview-resource
11-
coding template by other
13+
coding template by other
14+
low level/Online_Examination_CPP
15+
low level/Online_Examination_CPP
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.
2+
// Example 1:
3+
4+
// Input: nums = [2,1,2]
5+
// Output: 5
6+
// Example 2:
7+
8+
// Input: nums = [1,2,1]
9+
// Output: 0
10+
11+
class Solution {
12+
public:
13+
int largestPerimeter(vector<int>& nums) {
14+
sort(nums.begin(),nums.end(), greater<int>());
15+
for(int i=0;i<nums.size()-2;i++){
16+
int a = nums[i];
17+
int b = nums[i+1];
18+
int c = nums[i+2];
19+
if(a < b+c){
20+
return a+b+c;
21+
}
22+
}
23+
return 0;
24+
}
25+
};

0 commit comments

Comments
 (0)