Tasks Details
medium
1.
CountDiv
Compute number of integers divisible by k in range [a..b].
Task Score
75%
Correctness
75%
Performance
75%
Write a function:
class Solution { public int solution(int A, int B, int K); }
that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:
{ i : A ≤ i ≤ B, i mod K = 0 }
For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.
Write an efficient algorithm for the following assumptions:
- A and B are integers within the range [0..2,000,000,000];
- K is an integer within the range [1..2,000,000,000];
- A ≤ B.
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Time spent on task 32 minutes
Notes
not defined yet
Task timeline
Code: 18:38:02 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
if (A == B)
{
return (A%K == 0) ? 1 : 0;
}
if (B < K || K == 0)
{
return 0;
}
return (B - A)/K + 1;
}
}
Analysis
Code: 18:38:08 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
if (A == B)
{
return (A%K == 0) ? 1 : 0;
}
if (B < K || K == 0)
{
return 0;
}
return (B - A)/K + 1;
}
}
Analysis
Code: 18:38:11 UTC,
cs,
final,
score: 
75
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
if (A == B)
{
return (A%K == 0) ? 1 : 0;
}
if (B < K || K == 0)
{
return 0;
}
return (B - A)/K + 1;
}
}
Analysis summary
The following issues have been detected: wrong answers.
For example, for the input [11, 13, 2] the solution returned a wrong answer (got 2 expected 1).
Analysis
expand all
Correctness tests
1.
0.054 s
OK
1.
0.055 s
OK
2.
0.055 s
OK
1.
0.053 s
OK
2.
0.053 s
OK
3.
0.053 s
OK
extreme_endpoints
verify handling of range endpoints, multiple runs
verify handling of range endpoints, multiple runs
✘
WRONG ANSWER
got 2 expected 1
got 2 expected 1
1.
0.052 s
OK
2.
0.053 s
OK
3.
0.055 s
OK
4.
0.053 s
WRONG ANSWER,
got 2 expected 1
5.
0.052 s
OK
6.
0.053 s
OK
expand all
Performance tests
1.
0.052 s
OK
1.
0.055 s
WRONG ANSWER,
got 12346 expected 12345
1.
0.053 s
OK
2.
0.055 s
OK
1.
0.051 s
OK
2.
0.055 s
OK
3.
0.054 s
OK
4.
0.053 s
OK