Simple JAVA Simple JAVA JAVAAAAA


#1
public class Solution {
    HashSet<ArrayList<Integer>>hs=new HashSet<>();
	public ArrayList<ArrayList<Integer>> combinationSum(ArrayList<Integer> a, int b) {
	    Collections.sort(a);
	    ArrayList<ArrayList<Integer>>res = new ArrayList<ArrayList<Integer>>();
	    ArrayList<Integer>temp1=new ArrayList<Integer>();
	    fun(a,b,temp1,res,0,0);
	    return res;
	}
	public void fun(ArrayList<Integer> a, int b,  ArrayList<Integer>temp,ArrayList<ArrayList<Integer>>res,int sum,int idx){
	    if(sum>b)return ;
	    if(sum==b){
	            if(!hs.contains(temp)){
	            res.add(temp);
	            hs.add(temp);
	            return;
	            }
	    }
	    if(idx==a.size())return;
	    ArrayList<Integer>cur=new ArrayList<Integer>(temp);
    	    cur.add(a.get(idx));
    	    // BELOW 3 STATEMENTS MUST BE IN THIS ORDER TO MEET GIVEN CONSTRAINTS ON ANSWER
    	    fun(a,b,cur,res,sum+(a.get(idx)),idx);
    	    fun(a,b,temp,res,sum,idx+1);
    	    fun(a,b,cur,res,sum+(a.get(idx)),idx+1);
	    return;
	}
}