I have the following vector:
std::vector<int> ExampleVector {10, 2, 5, 1, 8, 20};
I need to find a 3 elements (A, B, C) where:
1) A > B < C
2) (A + B) > C
3) A < (B + C)
In the case of ExampleVector, 10 5 8 meet the above conditions.
My initial attempt used a number of iterators with given conditions:
int main()
{
for(auto first_iterator = A.begin(); first_iterator != A.end(); first_iterator++)
{
for(auto second_iterator = first_iterator() + 1; second_iterator != A.end(); second_iterator++)
{
for(auto third_iterator = second_iterator() + 1; third_iterator != A.end(); third_iterator++)
{
bool condition_1 {(*first_iterator + *second_iterator) > *third_iterator};
bool condition_2 {(*second_iterator + *third_iterator) > *first_iterator};
if(condition_1 && condition_2)
{
return 1;
}
}
}
}
return 0;
}
This effectively results in:
iterator cycle1 cycle2 cycle3 cycle4 cycle5 cycle6 cycle7 ... N
first_iterator: 10 10 10 10 10 10 10
second_iterator: 2 2 2 2 5 5 5
third_iterator: 5 1 8 20 1 8 20
Clearly this is extremely inefficient!
Are there better approaches I can use for tackling problems such as this?