From 6d1c3be47d61984ebd31f5c2f4eaeac348926621 Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Fri, 8 Jan 2021 07:18:05 -0800 Subject: [PATCH] Updated CountZero code Your previous code was giving wrong output when input is 0 itself. When input is 0, your code was giving 0 as output instead of 1. --- Recursion 1/CountZero.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Recursion 1/CountZero.java b/Recursion 1/CountZero.java index 55a35d4..da906a1 100644 --- a/Recursion 1/CountZero.java +++ b/Recursion 1/CountZero.java @@ -3,14 +3,21 @@ public class solution { public static int countZerosRec(int input){ // Write your code here - if(input == 0) - return 0; - if(input % 10 == 0){ - return countZerosRec(input / 10) + 1; - } - else{ - return countZerosRec(input / 10); - } + if(input/10 == 0) { + + if(input == 0) { + return 1; + } + return 0; + } + + + + int smallAns = countZerosRec(input/10); + if(input % 10 == 0) { + return smallAns + 1; + } + return smallAns; } public static void main(String[] args) {