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 16 minutes
Notes
not defined yet
Code: 12:55:12 UTC,
java,
autosave
Code: 12:56:15 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 0, 1]
Analysis
Code: 12:56:23 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 0, 11]
Analysis
Code: 12:57:45 UTC,
java,
verify,
result: Failed
Analysis
Compile error
Solution.java:5: error: ';' expected System.out.println(B / K) ^ 1 error
Code: 12:57:51 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
System.out.println(B / K);
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 0, 11]
Analysis
Code: 12:58:14 UTC,
java,
verify,
result: Failed
Analysis
Compile error
Solution.java:5: error: cannot find symbol System.out.println(Math.fllor((double)B / K)); ^ symbol: method fllor(double) location: class Math 1 error
Code: 12:58:21 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
System.out.println(Math.floor((double)B / K));
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 0, 11]
Analysis
Code: 12:58:45 UTC,
java,
verify,
result: Failed
Analysis
Compile error
Solution.java:11: error: cannot find symbol int ret = floor(b/K) - floor((a - 1)/K); // b? ? - a?? ???? ? ^ symbol: method floor(double) location: class Solution Solution.java:11: error: cannot find symbol int ret = floor(b/K) - floor((a - 1)/K); // b? ? - a?? ???? ? ^ symbol: method floor(double) location: class Solution 2 errors
Code: 12:58:55 UTC,
java,
verify,
result: Failed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
// System.out.println(Math.floor((double )B / K));
// return (B / K) - (A - 1) / K;
double a = A;
double b = B;
int ret = Math.floor(b/K) - Math.floor((a - 1)/K); // b의 몫 - a보다 작은수의 몫
return ret;
}
}
Analysis
Compile error
Solution.java:11: error: incompatible types: possible lossy conversion from double to int int ret = Math.floor(b/K) - Math.floor((a - 1)/K); // b? ? - a?? ???? ? ^ 1 error
Code: 12:59:57 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 0, 11]
Analysis
Code: 13:00:17 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 12, 2]
Analysis
Code: 13:02:08 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
Analysis
Code: 13:02:18 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
System.out.println((B / K));
return (B / K) - (A - 1) / K;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 6
function result: 6
stdout:
6
1.
0.004 s
OK
function result: 0
function result: 0
stdout:
0
Code: 13:03:38 UTC,
java,
verify,
result: Failed
Analysis
Compile error
Solution.java:5: error: cannot find symbol return r = (B / K) + 1; ^ symbol: variable r location: class Solution Solution.java:7: error: cannot find symbol r -= (A - 1) / K - 1; ^ symbol: variable r location: class Solution Solution.java:10: error: cannot find symbol return r; ^ symbol: variable r location: class Solution 3 errors
Code: 13:05:37 UTC,
java,
verify,
result: Failed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K - 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 5 expected 3
Code: 13:09:10 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K + 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
Analysis
Code: 13:09:25 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K + 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
User test case 3:
[0, 2000000000, 1]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 7
function result: 7
1.
0.004 s
OK
function result: 1
function result: 1
1.
0.004 s
OK
function result: 2000000001
function result: 2000000001
Code: 13:09:33 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K + 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
User test case 3:
[0, 2000000000, 1]
User test case 4:
[11, 354, 17]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 7
function result: 7
1.
0.008 s
OK
function result: 1
function result: 1
1.
0.008 s
OK
function result: 2000000001
function result: 2000000001
1.
0.008 s
OK
function result: 20
function result: 20
Code: 13:10:04 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K + 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
User test case 3:
[0, 2000000000, 1]
User test case 4:
[11, 354, 17]
User test case 5:
[101, 12345678900, 100000]
Analysis
expand all
User tests
1.
0.004 s
OK
function result: 7
function result: 7
1.
0.004 s
OK
function result: 1
function result: 1
1.
0.004 s
OK
function result: 2000000001
function result: 2000000001
1.
0.004 s
OK
function result: 20
function result: 20
1.
0.001 s
RUNTIME ERROR,
invalid input, integer value out of range: 12345678900
Code: 13:10:19 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K + 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
User test case 3:
[0, 2000000000, 1]
User test case 4:
[11, 354, 17]
User test case 5:
[101, 1234567890, 100000]
Analysis
Code: 13:10:29 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int A, int B, int K) {
int r = (B / K) + 1;
if (A != 0) {
r -= (A - 1) / K + 1;
}
return r;
}
}
User test case 1:
[0, 12, 2]
User test case 2:
[0, 0, 11]
User test case 3:
[0, 2000000000, 1]
User test case 4:
[11, 354, 17]
User test case 5:
[101, 1234567890, 100000]
Analysis
Code: 13:10:34 UTC,
java,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
0.004 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
expand all
Performance tests
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK