Tasks Details
medium
1.
CountDiv
Compute number of integers divisible by k in range [a..b].
Task Score
100%
Correctness
100%
Performance
100%
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 Java 8
Time spent on task 5 minutes
Notes
not defined yet
Task timeline
Code: 14:56:38 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int numberOfDivisibleByRange = 0;
if ( A % K == 0 || B % K == 0) { ++numberOfDivisibleByRange; }
return (B/K) - (A /K) + numberOfDivisibleByRange;
}
}
User test case 1:
[11, 14, 2]
Analysis
Code: 14:57:10 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int numberOfDivisibleByRange = 0;
if ( A % K == 0) { ++numberOfDivisibleByRange; }
return (B/K) - (A /K) + numberOfDivisibleByRange;
}
}
User test case 1:
[11, 14, 2]
Analysis
Code: 14:58:12 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int numberOfDivisibleByRange = 0;
if ( A % K == 0) { ++numberOfDivisibleByRange; }
return (B/K) - (A /K) + numberOfDivisibleByRange;
}
}
User test case 1:
[11, 14, 2]
User test case 2:
[0, 99, 2]
User test case 3:
[1, 1, 1]
Analysis
expand all
User tests
1.
1.353 s
OK
function result: 2
function result: 2
1.
1.359 s
OK
function result: 50
function result: 50
1.
1.341 s
OK
function result: 1
function result: 1
Code: 14:59:07 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int numberOfDivisibleByRange = 0;
if ( A % K == 0) { ++numberOfDivisibleByRange; }
return (B/K) - (A /K) + numberOfDivisibleByRange;
}
}
User test case 1:
[10, 10, 5]
User test case 2:
[11, 345, 17]
User test case 3:
[16, 29, 7]
Analysis
expand all
User tests
1.
1.323 s
OK
function result: 1
function result: 1
1.
1.324 s
OK
function result: 20
function result: 20
1.
1.323 s
OK
function result: 2
function result: 2
Code: 14:59:33 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int numberOfDivisibleByRange = 0;
if ( A % K == 0) { ++numberOfDivisibleByRange; }
return (B/K) - (A /K) + numberOfDivisibleByRange;
}
}
User test case 1:
[10, 10, 5]
User test case 2:
[11, 345, 17]
User test case 3:
[16, 29, 7]
User test case 4:
[1, 2, 1]
Analysis
expand all
User tests
1.
1.354 s
OK
function result: 1
function result: 1
1.
1.334 s
OK
function result: 20
function result: 20
1.
1.337 s
OK
function result: 2
function result: 2
1.
1.334 s
OK
function result: 2
function result: 2
Code: 15:00:03 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int offsetForLeftRange = 0;
if ( A % K == 0) { ++offsetForLeftRange; }
return (B/K) - (A /K) + offsetForLeftRange;
}
}
User test case 1:
[10, 10, 5]
User test case 2:
[11, 345, 17]
User test case 3:
[16, 29, 7]
User test case 4:
[1, 2, 1]
Analysis
expand all
User tests
1.
1.345 s
OK
function result: 1
function result: 1
1.
1.342 s
OK
function result: 20
function result: 20
1.
1.356 s
OK
function result: 2
function result: 2
1.
1.335 s
OK
function result: 2
function result: 2
Code: 15:00:18 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int offsetForLeftRange = 0;
if ( A % K == 0) { ++offsetForLeftRange; }
return (B/K) - (A /K) + offsetForLeftRange;
}
}
User test case 1:
[10, 10, 5]
User test case 2:
[11, 345, 17]
User test case 3:
[16, 29, 7]
User test case 4:
[1, 2, 1]
Analysis
expand all
User tests
1.
1.317 s
OK
function result: 1
function result: 1
1.
1.320 s
OK
function result: 20
function result: 20
1.
1.311 s
OK
function result: 2
function result: 2
1.
1.312 s
OK
function result: 2
function result: 2
Code: 15:00:30 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int A, int B, int K) {
int offsetForLeftRange = 0;
if ( A % K == 0) { ++offsetForLeftRange; }
return (B/K) - (A /K) + offsetForLeftRange;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
1.328 s
OK
1.
1.330 s
OK
2.
1.344 s
OK
1.
1.304 s
OK
2.
1.327 s
OK
3.
1.333 s
OK
1.
1.322 s
OK
2.
1.322 s
OK
3.
1.324 s
OK
4.
1.330 s
OK
5.
1.325 s
OK
6.
1.321 s
OK
expand all
Performance tests
1.
1.335 s
OK
1.
1.320 s
OK
1.
1.334 s
OK
2.
1.036 s
OK
1.
1.014 s
OK
2.
1.334 s
OK
3.
1.340 s
OK
4.
1.330 s
OK