Code not working

Code fails all test cases but is giving the right answer when executed . Please explain