Sum it up problem 2

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt(), arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = cin.nextInt();
}
Arrays.sort(arr);
int t = cin.nextInt();
ArrayList st = new ArrayList<>();
ArrayList ne = new ArrayList<>();
printCombinations(arr, t, β€œβ€, -1, st);
for (String s : st) {
if (!ne.contains(s)) {
ne.add(s);
}
}

	Collections.sort(ne);
	for (int i = 0 ;i<ne.size();i++) {
		System.out.println(ne.get(i));
	}
}

public static void printCombinations(int[] arr, int t, String ans, int lastIndexVisited, ArrayList<String> st) {
	if (t <= 0) {
		st.add(ans);
		return;
	}
	for (int i = lastIndexVisited + 1; i < arr.length; i++) {
		if (t >= arr[i]) {
			printCombinations(arr, t - arr[i], ans + " " + arr[i], i, st);
		}
	}
}

}-------what’s the error in my code I have removed duplicates please make corrections

hi @AbhishekAhlawat1102
Sort the array to maintain the order of the provided answer and to remove duplicates i have checked the previous element as duplicacy will be because of the similar characters.

@AbhishekAhlawat1102
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class temp {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt(), arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = cin.nextInt();
}
Arrays.sort(arr);
int t = cin.nextInt();
ArrayList st = new ArrayList<>();
ArrayList ne = new ArrayList<>();
printCombinations(arr, t, β€œβ€, -1, st);
for (String s : st) {
if (!ne.contains(s)) {
ne.add(s);
}
}
for (int i = 0; i < ne.size(); i++) {
System.out.println(ne.get(i));
}
}

public static void printCombinations(int[] arr, int t, String ans, int lastIndexVisited, ArrayList<String> st) {
	if (t == 0) {
		st.add(ans);
		return;
	}
	else if(t<0){
		return;
	}
	for (int i = lastIndexVisited + 1; i < arr.length; i++) {
		if (t >= arr[i]) {
			printCombinations(arr, t - arr[i], ans + " " + arr[i], i, st);
		}
	}
}

}

i have made few changes to your code if you have any doubt you can ask.

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.