You can find this problem in the Codility train page, section Sorting, under the nickname Triangle, where you can find also an extra requirement, the values in input could be also negative (even that doesn't make much sense, accordingly to the definition I gave), and could cover the complete range of signed 32 bit integers in C/C++.
Codility provides also a couple of test cases, described in informal language. I felt that at least a third one was missing. Here they are, written in C++ for the googletest xUnit testing framework:
TEST(Tria, Given) // 1 { std::vector<int> input; input.push_back(10); input.push_back(2); input.push_back(5); input.push_back(1); input.push_back(8); input.push_back(20); ASSERT_EQ(1, solution(input)); } TEST(Tria, Given2) // 2 { std::vector<int> input; input.push_back(10); input.push_back(50); input.push_back(5); input.push_back(1); ASSERT_EQ(0, solution(input)); } TEST(Tria, MaxInt) // 3 { std::vector<int> input; input.push_back(INT_MAX); input.push_back(INT_MAX); input.push_back(INT_MAX); ASSERT_EQ(1, solution(input)); }1. Example of a sequence that we should accept.
2. This input sequence does not contain any triangle triplet.
3. The input represents an equilateral triangle. However it is so huge that we could overflow, if our code is not designed carefully.
The idea of the algorithm I implemented is quite simple. Get the three smallest values in input, if the sum of the first two is less than the third one we have got our positive solution. Otherwise we know that the smallest number is too small to be part of any good triplet. Discard it and try again with the new smallest number in the lot. Go on till all the triplets are checked.
This was my implementation at the time of writing this post, in the meantime, codility has upgraded its compiler to get C++11, my code here depends on int type actual definition, and so now it fails in a extreme condition. Please, have a look to the newer post I have written about it for a more robust solution:
int solution(const std::vector<int>& input) { if(input.size() < 3) // 1 return 0; std::vector<int> buffer(input.begin(), input.end()); // 2 std::sort(buffer.begin(), buffer.end()); for(unsigned i = 0; i < buffer.size() - 2; ++i) // 3 { if(static_cast<int64_t>(buffer[i]) + buffer[i+1] > buffer[i+2]) return 1; } return 0; }1. Trivial case.
2. Performance-wise, would make no sense to operate on an unsorted collection. Being the input constant, create a copy and sort it.
3. Apply the above described algorithm. To avoid overflow, we should store the sum of the tentative catheti in a 64 bit integer. The simplest way to do that is casting one of them to int64_t.
No comments:
Post a Comment