-
Notifications
You must be signed in to change notification settings - Fork 0
/
countZeros.java
46 lines (40 loc) · 845 Bytes
/
countZeros.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
35
36
37
38
39
40
41
42
43
44
45
46
/*
Given an integer N, count and return the number of zeros that are present in the given integer using recursion.
Input Format :
Integer N
Output Format :
Number of zeros in N
Constraints :
0 <= N <= 10^9
Sample Input 1 :
0
Sample Output 1 :
1
Sample Input 2 :
00010204
Sample Output 2 :
2
Explanation for Sample Output 2 :
Even though "00010204" has 5 zeros, the output would still be 2 because when you convert it to an integer, it becomes 10204.
Sample Input 3 :
708000
Sample Output 3 :
4*/
public class solution {
public static int countZerosRec(int input){
if(input==0){
return 1;
}
return countZerosRec(input, 0);
}
public static int countZerosRec(int input, int count){
if(input==0){
return count;
}
int smallValue = input%10;
if(smallValue==0){
count++;
}
return countZerosRec(input/10,count);
}
}