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:
function solution(A, B, 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 JavaScript
Time spent on task 9 minutes
Notes
not defined yet
Task timeline
Code: 05:21:29 UTC,
java,
autosave
Code: 05:22:37 UTC,
js,
autosave
Code: 05:22:52 UTC,
js,
autosave
Code: 05:23:15 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K);
B = Math.trunc(B / K)
return
}
Code: 05:23:29 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K);
B = Math.trunc(B / K)
return Math.trunc((B - A) / K) + 1;
}
Analysis
expand all
Example tests
1.
0.068 s
WRONG ANSWER,
got 2 expected 3
Code: 05:23:39 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K);
B = Math.trunc(B / K);
return Math.trunc((B - A) / K) + 1;
}
Analysis
expand all
Example tests
1.
0.076 s
WRONG ANSWER,
got 2 expected 3
Code: 05:23:50 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); //
B = Math.trunc(B / K);
return Math.trunc((B - A) / K) + 1;
}
Code: 05:24:20 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
return Math.trunc((B - A) / K) + 1;
}
Code: 05:24:40 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K,
return Math.trunc((B - A) / K) + 1;
}
Code: 05:25:11 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1
return Math.trunc((B - A) / K) + 1;
}
Code: 05:25:28 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
return Math.trunc((B - A) / K) + 1;
}
Code: 05:26:09 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
return Math.trunc((B - A) / K) + 1;
}
Code: 05:26:24 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K); // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
console.log(A, B)
return Math.trunc((B - A) / K) + 1;
}
Analysis
expand all
Example tests
1.
0.080 s
WRONG ANSWER,
got 2 expected 3
stdout:
3 5
Code: 05:26:53 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) ; // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K); // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
console.log(A, B)
return Math.trunc((B - A) / K) + 1;
}
Code: 05:26:58 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already.
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
console.log(A, B)
return Math.trunc((B - A) / K) + 1;
}
Analysis
Code: 05:27:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already. Using truncation
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
console.log(A, B)
return Math.trunc((B - A) / K) + 1;
}
Code: 05:27:31 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already. Using truncation trick to discard decimals.
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already. Using truncation trick to discard decimals.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
console.log(A, B)
return Math.trunc((B - A) / K) + 1;
}
Code: 05:27:39 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already. Using truncation trick to discard decimals.
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already. Using truncation trick to discard decimals.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
return Math.trunc((B - A) / K) + 1;
}
User test case 1:
[5,5]
Analysis
expand all
User tests
1.
0.001 s
RUNTIME ERROR,
invalid input, unexpected ']', expecting integer
Code: 05:27:49 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already. Using truncation trick to discard decimals.
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already. Using truncation trick to discard decimals.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
return Math.trunc((B - A) / K) + 1;
}
User test case 1:
[5, 5, 5]
Analysis
Code: 05:29:25 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already. Using truncation trick to discard decimals.
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already. Using truncation trick to discard decimals.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
return Math.trunc((B - A) / K) + 1;
}
User test case 1:
[5, 5, 5]
Analysis
Code: 05:29:28 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
// Idea is to ensure A and B are multiples of K, then should be no issue.
A = Math.trunc((A + K - 1) / K) * K; // Find next higher num divisible by K if A is not already. Using truncation trick to discard decimals.
B = Math.trunc(B / K) * K; // Find the next lower num divisible by K if B is not already. Using truncation trick to discard decimals.
// Given B-A are multiples of K, e.g. [16,18,20] for K=2, 20-16 = 4, 4/2 = 2. 2 + 1 since we need to count A/B as well.
return Math.trunc((B - A) / K) + 1;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
5.
0.068 s
OK
6.
0.068 s
OK
expand all
Performance tests
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK