Count Div Codility 100% Correct Javascript Solution

The Count Div problems is classified as a medium difficulty but I found the solution to be quite trivial and easily solvable in O(1).

The only thing we need to do is to calculate how many numbers are divisible by a third one in between two value.

Thus, the solution:

function solution(A, B, K) {
    // write your code in JavaScript (Node.js 8.9.4)
    
    if (A === B) {
        if (A % K === 0) {
            return 1
        }
        else {
            return 0
        }
    }
    
    leftLimit = Math.ceil(A/K)
    rightLimit = Math.floor(B/K)
    
    return (rightLimit - leftLimit + 1)
}

The report is here.

Leave a Reply