This my code work fine but output are not give test case output and my output are same but order are change

public class subsetSum {
public static void subset(int[] arr,LinkedList ans,int solution,int index) {
if(index==arr.length) {
if(solution==0) {
for(int i:ans) {
System.out.print(i+" ");
}
System.out.println();
}
return ;
}
subset(arr,ans,solution,index+1);
ans.add(arr[index]);
subset(arr,ans,solution-arr[index],index+1);
ans.removeLast();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int solution=sc.nextInt();
int []arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
LinkedList ans=new LinkedList();
subset(arr,ans,solution,0);
}

}

@sksumitkumardiwaker,

Can you share your complete code?

It is complete code without package

@sksumitkumardiwaker,

Input of solution will be after the input of array.
Also, sort the array before passing it.

Your code will give you duplicate recursive calls, take care of that as well.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.