Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't understand how Codility CountDiv solution is correct

Tags:

c

algorithm

Codility CountDiv Exercise:

Given a range A..B and value K, return the number of values in the range that are divisible by K.

The example given is A = 6, B = 11 and K = 2. In the range 6 to 11, the numbers divisible by 2 are 6, 8 and 10, so the answer is 3. The solution required must be O(1) - so a simple calculation is needed.

You can assume that A and B are in the range 0..2,000,000,000, K is 1..2,000,000,000 and that 0 <= A <= B.

The accepted solution, that scores 100% is as follows:

int solution(int A, int B, int K)
{
    int inclusive = ((A%K)==0) ? 1 : 0;
    return (B/K) - (A/K) + inclusive;
}

Where I get confused is that when I test this solution with inputs A=0, B=0 and K=1, the result is 1? I would have thought that in the range 0 to 0, the number of values divisible by 1 is... 0!

I thought this was an error and that the +1 for inclusive values of A should only be set if A is non-zero.

So I submitted the following solution (test that A is non-zero):

int solution(int A, int B, int K)
{
    int inclusive = (A && (A%K)==0) ? 1 : 0;
    return (B/K) - (A/K) + inclusive;
}

But this only scored 62% (50% correctness and 75% performance). Some of the test cases it failed were:

  • A = 0, B = 1, K = 11 - Got 0, expected 1
  • A = 0, B = MAXINT, K in {1,MAXINT}, got 2000000000, expected 2000000001

Can someone explain?

like image 488
Simon Peverett Avatar asked Jul 05 '16 23:07

Simon Peverett


People also ask

What is codility countdiv problem (lesson 5) in Java?

Java solution to Codility CountDiv problem (Lesson 5 – Prefix Sums) which scored 100%. The problem is to calculate number of integers divisible by a given number form a sequential range of integers.

Are there any good codility lesson solutions?

Here is [ codility-lesson-solutions Solutions are very well implemented and contains almost all the problems, i have personally used this and was really surprised. Hope it will help you as well. Good luck. How is it possible for strong, English speaking programmers to fail at Codility or HackerRank tests? Nope. They don’t test that.

How difficult is the Count Div problem in O(1)?

The Count Div problems is classified as a medium difficulty but I found the solution to be quite trivial and easily solvable in O (1). The only thing we need to do is to calculate how many numbers are divisible by a third one in between two value. The report is here.

Can codility be used to do assessments?

i've seen people using codility to do assessments and judge on the technical qualification of people. i didn't find this helpful at all - it just tests a very very small subset of skills. mainly algorithmic knowledge.


2 Answers

The range is inclusive: there is 1 value in the range 0 to 0: the value 0 itself. All values are divisible by 1, so the result is indeed 1 value.

Note that the proposed code is redundant:

int inclusive = ((A%K)==0) ? 1 : 0; is equivalent to int inclusive = (A%K)==0;. It can be further simplified as int inclusive = !(A%K); and the complete solution becomes a one-liner:

int solution(int A, int B, int K) { return B/K - A/K + !(A%K); }

And here is a variant with only 2 divisions:

int solution(int A, int B, int K) { return B/K - (A ? (A-1)/K : -1); }
like image 24
chqrlie Avatar answered Sep 30 '22 19:09

chqrlie


The value 0 is divisible by K for all K that are allowed (non zero). There is nothing special about zero. The definition of divisible means there is no remainder after dividing.

like image 82
Mark Lakata Avatar answered Sep 30 '22 18:09

Mark Lakata