I have tried two approaches to the problem, although they are based on same principle and similar loops and conditions, but are failing the test cases…
I have submitted two solutions as follows:-
Submission #8379165
Submission #8436552
I am unable to figure out the problem in approach.