The code works for sample test case but lacks in other test cases

the code is working very well for the sample test case but failed during submission.

the code is written in java but the logioc is same

help me to find the flaws in my code