Skip to content

CountDiv

a920604a edited this page Apr 14, 2023 · 1 revision

title: CountDiv categories: - codility - Prefix Sum comments: false

solution

// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(int A, int B, int K) {
    // write your code in C++14 (g++ 6.2.0)
    // if(K==1) return B-A+1;
    while(A<B && A%K!=0) A++;
    while(A<B && B%K!=0) B--;
    if(A%K!=0) return 0;
    return (B-A)/K+1; 
}

analysis

  • time complexity O(1)
  • space complexity O(1)
Clone this wiki locally