Skip to content

Instantly share code, notes, and snippets.

@piusayowale
Created August 10, 2022 08:14
Show Gist options
  • Select an option

  • Save piusayowale/682dc93d183091545e3531b3e6308f09 to your computer and use it in GitHub Desktop.

Select an option

Save piusayowale/682dc93d183091545e3531b3e6308f09 to your computer and use it in GitHub Desktop.
pair<bool, tuple<int, int, int>> isTriangle(vector<int> sticks,int it){
/* triangle inequality theorem */
/* a + b > c; */
/* a + c > b */
/* b + c > a */
auto it_begin = sticks.begin() + it;
int a = *it_begin;
int b = *(it_begin + 1);
int c = *(it_begin + 2);
if(a + b > c && a + c > b && b + c > a){
return {true, make_tuple(a, b, c)};
}
return {false, {}};
}
vector<int> maximumPerimeterTriangle(vector<int> sticks) {
vector<tuple<int, int, int>> sides;
sort(sticks.rbegin(), sticks.rend());
for(int i = 0; i < sticks.size() - 2; i++){
auto ch = isTriangle(sticks, i);
if(ch.first == true){
sides.push_back(ch.second);
}
}
if(sides.empty()){
vector<int> ret;
ret.push_back(-1);
return ret;
}
return vector<int>{get<2>(sides[0]),get<1>(sides[0]), get<0>(sides[0])};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment