Solution to Dominator by codility

24 Jan

Question Name: Dominator

Same question in the introduction pdf.

25 Replies to “Solution to Dominator by codility”

1. Martin says:

Hey Sheng,

as I was reading this task I misread it and came up with a really tough problem. I would be interested in your ideas.

What is the leading denominator in a sequence of numbers (the number that divides more than half the array entries)?

There are roughly 200 milion primes in an integer. The rest would be the same.

Have you seen a similar problem somewhere? Any great ideas how to solve it :)?

• Sheng says:

I did not test it. But if 1 is permitted, 1 is the leading denominator.

If 1 is not counted, and the sequence of numbers are continue (from 1 to som number, like 1 – 2 -3 -4 -5 -6), 2 would be in most case.

In other cases, we have to compute the denominator for each number.

PS: Better solution might exist. But I do not know~. If you get it, please tell me!

2. Sis says:

Hi there.
One trick to avoid loops here is to use Counter from collections.

• Sheng says:

Yes and no. Yes, literally we can avoid loops by using Counter. No, essentially, with or without Counter, our program is going to iterate all the items.

3. Rafael Jasso says:

Solution in Java

• Sheng says:

Correct, but you could break in the second if statement.

• Julian says:

Hi! I was wondering does HashMap provide required memory complexity O(1). For me it doesn’t as we can create HashMap with N entries if each entry is different.

• Sheng says:

Did not notice the space requirement. Yes, you are totally right. This solution does not meet the space requirement.

4. df611 says:

Here’s a simple C implementation for which I scored 100% on codility:

5. C#, 100%. Because of the Dictionary its time complexity detected as O(N) or O(N*log(N))

6. A Java solution without using a HashMap, based on the introductory lecture for the lesson ( I read it and then after understanding the lecture I started the implementation).

7. Minh says:

Here’s my first solution in PHP 100% ðŸ˜€

8. Pierre-Yves says:

Like Martin, I also misunderstood the demand at first but this is also a way to improve. Anyway, here’s my solution, and it is O(2*log(N)) instead of O(N) as demanded thanks to sorted (once) and bisect (twice but each time over N/2).

https://codility.com/demo/results/trainingVJESZY-PDW/

• Sheng says:

It should be your typo. Its complexity is O(N * log(N)), not O(2*log(N)).

9. Amanda says:

This is a very interesting idea. The main point lies in that a dominator has to appear more than half of the total number of elements in the list. Wonderful. Is there a rigorous proof for this?

• Sheng says:

By the definition in the question statement.

10. Fabio says:

Hi Sheng,

May I share my python solution? Got 100% in O(N). Any thoughts?

• Sheng says:

It should be correct. But it does not meet the space complexity requirement of the problem.

11. Marcelo says:

Hi Sheng,

I think your code does not meet the space complexity, because this line:

creates an array so it is no o(1) complexity or am I wrong?,

Thanks,

• Sheng says:

You are absolutely right. That code uses O(N) space, while you are easily reduce the space usage to O(1)-space by an O(N)-time traversal. Thanks!

12. John Kowalsky says:

It seems that your code generates 6 instead of 3, and 0 instead of 1, respectively for:

All functions from PDF (i.e. slowLeader, fastLeader and goldenLeader) give the results: 3 and 1. I am not sure, cause I have not analyzed your code very carefully, but shouldn’t the last line of code be changed to:

or just

Now it works fine. Solution should return found dominator, not its index.

• Sheng says:

The PDF has some similar questions, but not the same. The original question description says “returns index of any element of array A in which the dominator of A occurs”.

• John Kowalsky says:

You are right. This is specified in the leading description of the Dominator task ( https://goo.gl/t7YDqi ). I have been misleaded by functions in codility lessons and this text “The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.” But this is obviouos after reading such statement: “the function may return 0, 2, 4, 6 or 7, as explained above”. Probably automatic testing checks whether the result belongs to the given set of indexes or not. It is interesting that the specific result mainly depends on the implementation. There is no strict requirement for that, let’s say: first or last index. OK, that is clear now.

Nevertheless, thanks for your blog. I have learned a lot from published Python solutions (in posts and comments). Some of the used approaches to solve problems are realy brilliant and tricky, in order to meet complexity expectations. I have already run them all in Eclipse. Tens of files ðŸ™‚ Now I want to do the same with Java versions ðŸ˜‰