2 test cases does not pass

out of 4 two test cases are not passing
My code:

hey @Ramitgoel, your function is not correct, check the below link, everything is explained in comments

my code was working for 3 test cases then kindly tell whats wrong in my previous code
I want to learn from my previous code

hey @Ramitgoel, I have not given you new code, but corrected your code only please compare both the code you will able to find your mistakes.