I was recently faced with two codility tasks when applying for a job as an Embedded Software Engineer. For those of you who arn't familiar with Codility you can check out their website here: www.codility.com
Task one - Dominator
The first task was called Dominator. The goal was to, given a std::vector of integers, find an integer that occurs in more than half of the positions in the vector. If no dominator was found -1 should be returned.
My approach was to loop through the vector from the first to the last element, using a std::map to count the number of occurences of each integer. If the count ever reached above half the size of the vector I stopped and returned that integer and if I reached the end without finding a dominator I returned -1.
So was that a good approach? Well, the reviewer at the company rated the solution as 'pretty ok'. His preferred solution was store the first integer in the array and set a counter to 1. Then loop through the remaining integers in the vector. If the current integer in the vector is the same as the one stored, the counter is increased by one, otherwise the counter is decreased by one. If the counter ever reaches zero you throw away the stored integer and replace it with the current integer in the vector. When you finally have looped through all integers you are left with one candidate. You then need to loop through the vector again and count the occurences of the candidate to verify that this really is a dominator.
Let's check this solution in C++ code:
#include <cstddef>
#include <vector>
int getDominator(const std::vector<int>& A)
{
if (A.empty())
{
return -1;
}
int counter = 1;
int candidate = A[0];
for (std::size_t i = 1; i < A.size(); ++i)
{
if (A[i] == candidate)
{
++counter;
}
else
{
--counter;
if (0 == counter)
{
candidate = A[i];
counter = 1;
}
}
}
counter = 0;
for (std::size_t i = 0; i < A.size(); ++i)
{
if (A[i] == candidate)
{
++counter;
}
}
int dominator = -1;
if (counter > (A.size() / 2))
{
dominator = candidate;
}
return dominator;
}
So what do you think? Is there a better solution? Would you had figured this one out?
Stay tuned for Part II - The K-complement.
Hi,
SvaraRaderaHave you tested this solution against testcase like A[] = {1,1,1,2,2,2,1,3}?
I don't think above solution works for above test case.
Hi Hitesh,
SvaraRaderaThanks for your comment.
A[] = {1,1,1,2,2,2,1,3} has no dominator (no integer occurs in more than half the positions). Hence, the algorithm should return -1, which it does.
Den här kommentaren har tagits bort av skribenten.
SvaraRaderai was wrote two functions.
SvaraRaderafirst using 'set' and second using 'map'.
in case if i suspect that the function may return a value other then -1, the second algorithm is fastest.
in other case both of them gives similar duration times, though the first is a little bit faster.
can anybody tell me which of these solutions is satisfied (if any).
= = =
int function1(const std::vector & A) {
if (A.empty()) {
return -1;
}
if (A.size() == 1) {
return 0;
}
std::map values;
unsigned int half = A.size() / 2;
for (unsigned int index = 0; index < A.size(); index++) {
if (++values[A[index]] > half) {
return index;
}
}
return -1;
}
int function2(const std::vector & A) {
if (A.empty()) {
return -1;
}
if (A.size() == 1) {
return 0;
}
std::set usedValues;
unsigned int half = A.size() / 2;
unsigned int indexMax = (unsigned int)((float)A.size() / 2.0 + 0.5);
for (unsigned int index = 0; index < indexMax; index++) {
if (usedValues.find(A[index]) != usedValues.end()) {
continue;
}
if (std::count(A.begin() + index, A.end(), A[index]) > half) {
return index;
}
usedValues.insert(A[index]);
}
return -1;
}
Hi ArkadiuszG,
SvaraRaderaYour function1 looks pretty much like the solution that I submitted. It should be ok, just some remarks. If A.size() == 1 you probably wish to return A[0], and not 0 right?
Secondly, try using std::size_t instead of unsigned int when looping up to A.size(). Probably it won't matter but std::size_t should always be able to represent the size of any object.
Well then what about this case [1, 1, 3, 3, 3, 3]. So here it will take 1, go to next number counter will be 2, go to next number ( 3rd position) counter will be 1 (as it is not 1). go to the next number (4th position) , counter will be 0 , take 3 and counter will be 1 and in the end counter will be 3 but since it is not counted in 4 places it will give -1. in reality it should give 3
SvaraRaderaHi Prashant
SvaraRaderaThe algorithm works. Codility has made a version of the Dominator problem available, you can verify it there yourself, see http://codility.com/train/. Note that you will have to make some minor changes since the version available at Codility expects the index number of the dominator to be returned and not the actual dominator value.