Tasks Details
medium
1.
CountDiv
Compute number of integers divisible by k in range [a..b].
Task Score
75%
Correctness
100%
Performance
50%
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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Time spent on task 9 minutes
Notes
not defined yet
Code: 06:07:32 UTC,
cs,
verify,
result: Passed
using System;
class Solution {
public int solution(int A, int B, int K) {
if(A < 0 || 2000000000 < A || B < 0 || 2000000000 < B || K < 1 || 2000000000 < K || B < A) throw new InvalidOperationException();
if(K == 1) return (B + 1) - A;
int cnt = 0;
for(int i = A; i <= B; i++)
{
if(i % K == 0) cnt++;
}
return cnt;
}
}
Analysis
Code: 06:07:48 UTC,
cs,
verify,
result: Passed
using System;
class Solution {
public int solution(int A, int B, int K) {
if(A < 0 || 2000000000 < A || B < 0 || 2000000000 < B || K < 1 || 2000000000 < K || B < A) throw new InvalidOperationException();
if(K == 1) return (B + 1) - A;
int cnt = 0;
for(int i = A; i <= B; i++)
{
if(i % K == 0) cnt++;
}
return cnt;
}
}
User test case 1:
[100, 123456789, 2]
Analysis
Code: 06:14:07 UTC,
cs,
verify,
result: Failed
using System;
class Solution {
public int solution(int A, int B, int K) {
if(A < 0 || 2000000000 < A || B < 0 || 2000000000 < B || K < 1 || 2000000000 < K || B < A) throw new InvalidOperationException();
int cnt = 0;
int result = 0;
while(cnt <= B)
{
cnt += K;
if(A <= cnt) result++;
}
return result;
}
}
User test case 1:
[100, 123456789, 2]
Analysis
expand all
Example tests
1.
0.041 s
WRONG ANSWER,
got 4 expected 3
Code: 06:15:24 UTC,
cs,
verify,
result: Passed
using System;
class Solution {
public int solution(int A, int B, int K) {
if(A < 0 || 2000000000 < A || B < 0 || 2000000000 < B || K < 1 || 2000000000 < K || B < A) throw new InvalidOperationException();
int cnt = 0;
int result = 0;
while(cnt <= B)
{
if(A <= cnt) result++;
cnt += K;
}
return result;
}
}
User test case 1:
[100, 123456789, 2]
Analysis
Code: 06:15:32 UTC,
cs,
verify,
result: Passed
using System;
class Solution {
public int solution(int A, int B, int K) {
if(A < 0 || 2000000000 < A || B < 0 || 2000000000 < B || K < 1 || 2000000000 < K || B < A) throw new InvalidOperationException();
int cnt = 0;
int result = 0;
while(cnt <= B)
{
if(A <= cnt) result++;
cnt += K;
}
return result;
}
}
User test case 1:
[100, 123456789, 2]
Analysis
Code: 06:15:35 UTC,
cs,
final,
score: 
75
using System;
class Solution {
public int solution(int A, int B, int K) {
if(A < 0 || 2000000000 < A || B < 0 || 2000000000 < B || K < 1 || 2000000000 < K || B < A) throw new InvalidOperationException();
int cnt = 0;
int result = 0;
while(cnt <= B)
{
if(A <= cnt) result++;
cnt += K;
}
return result;
}
}
Analysis summary
The following issues have been detected: wrong answers, timeout errors.
For example, for the input [0, 2000000000, 1] the solution exceeded the time limit.
Analysis
Detected time complexity:
O((B-A)/K)
expand all
Correctness tests
1.
0.051 s
OK
1.
0.051 s
OK
2.
0.052 s
OK
1.
0.054 s
OK
2.
0.053 s
OK
3.
0.053 s
OK
1.
0.051 s
OK
2.
0.052 s
OK
3.
0.053 s
OK
4.
0.055 s
OK
5.
0.054 s
OK
6.
0.054 s
OK
expand all
Performance tests
1.
0.093 s
OK
1.
0.053 s
OK
big_values3
A = 0, B = MAXINT, K in {1,MAXINT}
A = 0, B = MAXINT, K in {1,MAXINT}
✘
TIMEOUT ERROR
running time: 1.35 sec., time limit: 0.21 sec.
running time: 1.35 sec., time limit: 0.21 sec.
1.
1.349 s
TIMEOUT ERROR,
running time: 1.35 sec., time limit: 0.21 sec.
2.
0.053 s
WRONG ANSWER,
got 22 expected 2
1.
0.054 s
OK
2.
1.350 s
TIMEOUT ERROR,
running time: 1.35 sec., time limit: 0.21 sec.
3.
1.349 s
TIMEOUT ERROR,
running time: 1.35 sec., time limit: 0.21 sec.
4.
0.052 s
OK