Java Backtracking
Per the rules mentioned in the problem, we need \n1. exactly one icecream base. \n2. Toppings can be 0, 1 or 2\n\nSo, we can check all three cases (toppings 0,1
Per the rules mentioned in the problem, we need \n1. exactly one icecream base. \n2. Toppings can be 0, 1 or 2\n\nSo, we can check all three cases (toppings 0,1,2) for every baseCost to find the closest cost to the target (find minimum: target - cost).\n\nProblem also states that If there are multiple, return the lower one. So, I have added a check \nif abs(target - current) == abs(target - result) && current < result then result = current\n\nAlso, in case if target is greater than or equal to current, we do not need to check further as adding topping to it will further increase the difference.\n\n```\nclass Solution {\n    int result;\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        result = baseCosts[0];\n        for(int base: baseCosts)\n            helper(base, toppingCosts, 0, target);\n        return result;\n    }\n    private void helper(int current, int[] toppingCosts, int index, int target) {\n        if( Math.abs(target - current) < Math.abs(target - result) || Math.abs(target - current) == Math.abs(target - result) && current < result)\n            result = current;\n        if(index==toppingCosts.length || current >= target) return;\n        helper(current, toppingCosts, index + 1, target);\n        helper(current + toppingCosts[index], toppingCosts, index + 1, target);\n        helper(current + toppingCosts[index]*2, toppingCosts, index + 1, target);\n    }\n}\n```
[]