C++ | BackTracking | Bonus : Similar Problem
We can use a backtracking solution exploring all the possibiities of picking a particular topping atmost 2 times (possibly 0) and finding the total cost that ha
We can use a backtracking solution exploring all the possibiities of picking a particular topping atmost 2 times (possibly 0) and finding the total *cost* that has minimum absolute difference with the target ( *abs(cost,target)* ). \nAlgorithm : \n1. Go over every base and include that in the cost and for every base call a recursive function that finds the icecream with given base and price closest to the given target . \n2. Every recursive function will have following parameters \n\t```\n\t(int cost, int idx, vector<int> count, vector<int>& toppingCosts, int target, pair<int, int>& result) \n   ```\n\t**cost** : an integer denoting the current cost of the icecream.\n\t**count** : a sequential data structure (vector in case of c++) of size similar to that of *toppingCosts*. This will store the\n\t\t\t\t\t\tfrequency of topping that has been taken till now. This will be used to present an important base case: no icecream should have more than 2 types of topping of each type.\n\t**toppingCosts** : Sequential data structures containing cost of the toppings.\n\t**target** : Given target \n\t**result** : pair containing the cost and its absolute difference with the target. This will be updated every time we are done taking the toppings i.e *idx >= toppingCosts.size()*\n\nFull Commented Code :\n```\n\nclass Solution {\npublic:\n\t// custom comparator to find the required cost. \n    static bool compare(pair<int, int> a, pair<int, int> b) {\n        if (a.second == b.second)\n            return a.first < b.first;\n        return a.second < b.second;\n    }\n    void rec(int cost, int idx, vector<int> count, vector<int>& toppingCosts, int target, pair<int, int>& result) {\n        if (idx >= toppingCosts.size()) {    // base case : when we are done taking the toppings. \n            if (abs(target - cost) < result.second)      // if found cost is more closer to the target , update result pair. \n                result = {cost, abs(target - cost)};\n\t\t\t// if found cost is having same difference with target , update result pair with lower value of cost. \n            if (abs(target - cost) == result.second) {   \n                if (cost < result.first)\n                    result.first = cost;\n            }\n            return;\n        }\n\t   // base case : when we have taken a particular topping more than two times. we have entered an illegal state so just return. \n        if (count[idx] > 2) \n            return;\n        count[idx] += 1;  // take topping at index : idx. \n        rec(cost + toppingCosts[idx], idx, count, toppingCosts, target, result);\n       // backtracking step , i.e revert the decision taken earlier so as to take one more decision of not taking topping at index : idx\n        count[idx] -= 1; \n        rec(cost, idx + 1, count, toppingCosts, target, result);\n    }\n    int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {\n        int p = toppingCosts.size();\n        vector<int> count(p, 0); // to store the frequency of the topping taken at each recursive call. \n        vector<pair<int, int> > v;  // stores the result pair for every base taken. This will be later used to find the best ans according to the question. \n        for (int i = 0; i < baseCosts.size(); i++) {\n            int cost = baseCosts[i]; // take current base\n            pair<int, int> result = {0, INT_MAX};  // {cost,absdiff};\n            rec(cost, 0, count, toppingCosts, target, result);\n            v.push_back({result.first, abs(result.first - target)});\n        }\n        sort(v.begin(), v.end(), compare);\n        return v[0].first;\n    }\n};\n```\n\n**One can solve another problem on the similar lines by visiting below link :**\n[Link to similar problem from Atcoder]([https://atcoder.jp/contests/abc190/tasks/abc190_c)
[]