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:
Can someone explain?
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.
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.
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.
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.
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); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With