Tasks Details
medium
1.
CountDiv
Compute number of integers divisible by k in range [a..b].
Task Score
37%
Correctness
25%
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 15 minutes
Notes
not defined yet
Task timeline
Code: 08:33:48 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start;
if(B<A || K==0 || K>B )
return 0;
else if(K<A)
start = A%K +1;
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
Compile error
Solution.java:21: error: variable start might not have been initialized return (B-start+1)/K+ 1; ^ 1 error
Code: 08:34:04 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A)
start = A%K +1;
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
expand all
Example tests
1.
0.868 s
WRONG ANSWER,
got 6 expected 3
Code: 08:36:11 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A)
start =K* ( A%K +1);
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
expand all
Example tests
1.
1.252 s
WRONG ANSWER,
got 6 expected 3
Code: 08:36:40 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A)
start =K* ( A%K );
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
expand all
Example tests
1.
1.500 s
WRONG ANSWER,
got 7 expected 3
Code: 08:39:13 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A){
start = K * ( A%K );
system.out.println("coucou"+start);
}
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
Compile error
Solution.java:18: error: package system does not exist system.out.println("coucou"+start); ^ 1 error
Code: 08:39:29 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A){
start = K * ( A%K );
System.out.println("coucou"+start);
}
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
expand all
Example tests
1.
1.568 s
WRONG ANSWER,
got 7 expected 3
stdout:
coucou0
Code: 08:40:59 UTC,
java,
verify,
result: Failed
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A){
start = K * ( A/K );
System.out.println("coucou"+start);
}
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
expand all
Example tests
1.
1.300 s
WRONG ANSWER,
got 4 expected 3
stdout:
coucou6
Code: 08:41:30 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A){
start = K * ( A/K +1);
System.out.println("coucou"+start);
}
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
expand all
Example tests
1.
1.332 s
OK
stdout:
WARNING: producing output may seriously slow down your code! coucou8
Code: 08:42:23 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A){
start = K * ( A/K +1);
//System.out.println("coucou"+start);
}
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis
Code: 08:42:31 UTC,
java,
final,
score: 
37
// 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) {
// write your code in Java SE 8
int start=0;;
if(B<A || K==0 || K>B )
return 0;
else if(K<A){
start = K * ( A/K +1);
//System.out.println("coucou"+start);
}
else if (K<=B)
start = K;
return (B-start+1)/K+ 1;
}
}
Analysis summary
The following issues have been detected: wrong answers.
Analysis
expand all
Correctness tests
1.
1.616 s
OK
1.
1.492 s
WRONG ANSWER,
got 0 expected 1
1.
1.456 s
WRONG ANSWER,
got 1 expected 0
extreme_endpoints
verify handling of range endpoints, multiple runs
verify handling of range endpoints, multiple runs
✘
WRONG ANSWER
got 2 expected 3
got 2 expected 3
1.
1.388 s
WRONG ANSWER,
got 2 expected 3
expand all
Performance tests
1.
1.476 s
OK
1.
1.428 s
OK
1.
1.416 s
WRONG ANSWER,
got 1 expected 2
1.
1.488 s
WRONG ANSWER,
got 2 expected 1