-
Notifications
You must be signed in to change notification settings - Fork 154
/
CountDiv.java
34 lines (26 loc) · 1.01 KB
/
CountDiv.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package CountDiv;
class Solution {
public int solution(int A, int B, int K) {
// write your code in Java SE 8
// need to achieve low complexity O(1)
// using math equation (low complexity)
// number of divisible values smaller than B
int num_B = (int) Math.floor( B/K );
// note: take "Math.floor" which is the basic number
// number of divisible values smaller than A
int num_A = (int) Math.floor( A/K );
// note: take "Math.floor" which is the basic number
// number of divisible numbers
int num_div = num_B - num_A;
// note: plus one (if A % K == 0)
// because "A" is also divisble
// without "plus", "A" will be deducted
int plus = 0;
if(A % K == 0)
plus = 1;
// num_div + plus
num_div = num_div + plus;
// return the number of K-divisible values between A and B
return num_div;
}
}