Help me correcting the mistake

My logic for the question is entirely right, also passing for the given test case, still failing all the test cases while submission.

Your code looks alright but what you can do is store all the results in a List< List < Integer > > , and at end print all the list from the set.
go through this code once