---
# System prepended metadata

title: L6-Triangle
tags: [Codility_lessons]

---

# L6-Triangle
###### tags: `Codility_lessons`
## Question
https://app.codility.com/programmers/lessons/6-sorting/triangle/
## Key
1. 因為要找出符合三角形定理的三個點，所以先排序後的相鄰點最有機會，且只要用一個loop再比較 中+小 > 大即可
2. 因為極值可能大於int的上限，所以注意比較跟加總時，要使用強轉long

## Reference
## Solution
```cpp=
#include <algorithm>

int solution(vector<int> &A) {
    
    if(A.size() < 3)
    {
        return 0;
    }
    long sum;
    sort(A.begin(), A.end());
    for(int i = 0; i<A.size()-2; i++)
    {
        sum = (long)A[i]+(long)A[i+1];
        if(sum > (long)A[i+2])
        {
            return 1;  
        }
    }
    return 0;
}
```