forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberLessThanK.java
More file actions
59 lines (49 loc) · 1.32 KB
/
NumberLessThanK.java
File metadata and controls
59 lines (49 loc) · 1.32 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Math;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Author - archit.s
* Date - 30/09/18
* Time - 10:52 PM
*/
public class NumberLessThanK {
public int solve(ArrayList<Integer> A, int B, int C) {
int cSize = (int) Math.log10(C) + 1;
int n = A.size();
if(cSize < B || n == 0){
return 0;
}
boolean zero = false;
if(A.get(0) == 0){
zero = true;
}
if(cSize > B){
if(B > 1 && zero){
return (n-1)* ((int)Math.pow(n, B-1));
}
else{
return (int)Math.pow(n, B);
}
}
int pow10 = (int) Math.pow(10, B-1);
int count = 0;
for(int i=B;i>0;i--){
int target = C/pow10;
int j=0;
C %= pow10;
pow10 /= 10;
for(j=0;j<n;j++){
if(A.get(j)>=target){
break;
}
}
count += (B > 1 && i == B && zero ? j-1:j) * (int)Math.pow(n, i-1);
if(j == n || A.get(j) > target)
break;
}
return count;
}
public static void main(String[] args) {
System.out.println(new NumberLessThanK().solve(new ArrayList<>(Arrays.asList(0, 1, 3, 4)), 3, 423));
}
}