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 21 minutes
Notes
not defined yet
Task timeline
Code: 06:12:17 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
count=count+(b/a)*(a/k);
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
Analysis
Code: 06:19:51 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
count=count+(b/a)*(a/k);
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
Analysis
Code: 06:20:14 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
count=count+(b/a)*(a/k);
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
User tests
1.
1.321 s
OK
function result: 5
function result: 5
1.
1.312 s
OK
function result: 1
function result: 1
1.
1.307 s
OK
function result: 11
function result: 11
Code: 06:21:23 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(k<a){
a=k;
}
if(a!=b){
count=count+(b/a)*(a/k);
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
Example tests
1.
1.309 s
WRONG ANSWER,
got 5 expected 3
expand all
User tests
1.
1.325 s
OK
function result: 7
function result: 7
1.
1.327 s
OK
function result: 1
function result: 1
1.
1.311 s
OK
function result: 19
function result: 19
Code: 06:22:09 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(k<a){
count=b-a;
}
if(a!=b){
count=count+(b/a)*(a/k);
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
Example tests
1.
1.258 s
WRONG ANSWER,
got 7 expected 3
expand all
User tests
1.
1.187 s
OK
function result: 9
function result: 9
1.
1.328 s
OK
function result: 1
function result: 1
1.
1.330 s
OK
function result: 19
function result: 19
Code: 06:22:29 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
count=count+(b/a)*(a/k);
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
User tests
1.
1.334 s
OK
function result: 5
function result: 5
1.
1.323 s
OK
function result: 1
function result: 1
1.
1.329 s
OK
function result: 11
function result: 11
Code: 06:23:27 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
//count=count+(b/a)*(a/k);
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
Example tests
1.
1.329 s
WRONG ANSWER,
got 2 expected 3
expand all
User tests
1.
1.329 s
OK
function result: 2
function result: 2
1.
1.327 s
OK
function result: 1
function result: 1
1.
1.330 s
OK
function result: 8
function result: 8
Code: 06:24:58 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
//count=count+(b/a)*(a/k);
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
Example tests
1.
1.330 s
WRONG ANSWER,
got 2 expected 3
expand all
User tests
1.
1.173 s
OK
function result: 2
function result: 2
1.
1.344 s
OK
function result: 1
function result: 1
1.
1.339 s
OK
function result: 8
function result: 8
Code: 06:26:47 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
Analysis
expand all
User tests
1.
1.302 s
OK
function result: 3
function result: 3
1.
1.312 s
OK
function result: 1
function result: 1
1.
1.306 s
OK
function result: 9
function result: 9
Code: 06:27:10 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
Analysis
expand all
User tests
1.
1.328 s
OK
function result: 3
function result: 3
1.
1.327 s
OK
function result: 1
function result: 1
1.
1.305 s
OK
function result: 9
function result: 9
1.
1.326 s
OK
function result: 0
function result: 0
Code: 06:27:42 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0){
a=k;
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
Analysis
Code: 06:29:14 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
/*if(a==0){
a=k;
count=1;
}*/
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
Analysis
expand all
User tests
1.
1.310 s
OK
function result: 3
function result: 3
1.
1.326 s
OK
function result: 1
function result: 1
1.
1.308 s
OK
function result: 9
function result: 9
1.
1.339 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Exception in thread "main" java.lang.ArithmeticException: / by zero at Solution.solution(Solution.java:35) at wrapper.run(wrapper.java:42) at wrapper.main(wrapper.java:31)
1.
1.336 s
OK
function result: 1
function result: 1
Code: 06:29:54 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
if(a==0 && b==0){
count=1;
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
Analysis
expand all
User tests
1.
1.300 s
OK
function result: 3
function result: 3
1.
1.300 s
OK
function result: 1
function result: 1
1.
1.299 s
OK
function result: 9
function result: 9
1.
1.333 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
Exception in thread "main" java.lang.ArithmeticException: / by zero at Solution.solution(Solution.java:34) at wrapper.run(wrapper.java:42) at wrapper.main(wrapper.java:31)
1.
1.310 s
OK
function result: 1
function result: 1
Code: 06:30:36 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a==0 && b==0){
count=1;
}else if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
Analysis
Code: 06:31:00 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a==0 && b==0){
count=1;
}else if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
User test case 6:
[1, 199, 1]
Analysis
expand all
User tests
1.
1.328 s
OK
function result: 3
function result: 3
1.
1.338 s
OK
function result: 1
function result: 1
1.
1.328 s
OK
function result: 9
function result: 9
1.
1.340 s
OK
function result: 1
function result: 1
1.
1.338 s
OK
function result: 1
function result: 1
1.
1.338 s
OK
function result: 199
function result: 199
Code: 06:31:34 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a==0 && b==0){
count=1;
}else if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
User test case 6:
[1, 199, 1]
User test case 7:
[0, 200, 1]
Analysis
expand all
User tests
1.
1.327 s
OK
function result: 3
function result: 3
1.
1.329 s
OK
function result: 1
function result: 1
1.
1.311 s
OK
function result: 9
function result: 9
1.
1.334 s
OK
function result: 1
function result: 1
1.
1.328 s
OK
function result: 1
function result: 1
1.
1.336 s
OK
function result: 199
function result: 199
1.
1.087 s
OK
function result: 201
function result: 201
Code: 06:32:00 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a==0 && b==0){
count=1;
}else if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
User test case 1:
[10, 14, 2]
User test case 2:
[11, 13, 12]
User test case 3:
[11, 19, 1]
User test case 4:
[0, 0, 1]
User test case 5:
[10, 10, 5]
User test case 6:
[1, 199, 1]
User test case 7:
[0, 200, 1]
Analysis
expand all
User tests
1.
1.323 s
OK
function result: 3
function result: 3
1.
1.317 s
OK
function result: 1
function result: 1
1.
1.316 s
OK
function result: 9
function result: 9
1.
1.321 s
OK
function result: 1
function result: 1
1.
1.322 s
OK
function result: 1
function result: 1
1.
1.312 s
OK
function result: 199
function result: 199
1.
1.321 s
OK
function result: 201
function result: 201
Code: 06:32:18 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout 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
long a=A;
long b=B;
long k=K;
long count=0;
long i=a%k;
//System.out.println(a+" "+b+" "+k);
if(i!=0){
a=a+Math.abs(i-k);
}
long j=b%k;
if(j!=0){
b=b-(Math.abs(j));
}
if(a!=b){
if(a%k==0) count=1;
count=count+(b-a)/k;
}else{
if(a==0 && b==0){
count=1;
}else if(a%k==0){
count=(b/a);
}else{
count=0;
}
}
return (int)count;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
1.342 s
OK
1.
1.353 s
OK
2.
1.341 s
OK
1.
1.349 s
OK
2.
1.348 s
OK
3.
1.353 s
OK
1.
1.358 s
OK
2.
1.346 s
OK
3.
0.969 s
OK
4.
0.915 s
OK
5.
0.905 s
OK
6.
0.980 s
OK
expand all
Performance tests
1.
1.191 s
OK
1.
1.110 s
OK
1.
1.056 s
OK
2.
1.100 s
OK
1.
1.083 s
OK
2.
1.006 s
OK
3.
1.339 s
OK
4.
1.341 s
OK