3
// ans-> store final result4
// mn -> Current minimum difference b/w target and curr_price5
int ans = INT_MAX, mn = INT_MAX;8
i-> index of tc(toppingCosts)9
curr -> current cost taken13
void find(int i, vector<int> &tc, int curr, int tar) {15
if difference b/w current cost and target cost is16
less than min difference , so we have to update ans17
and min difference because we got closest to target19
if (abs(curr - tar) < mn) {25
if difference b/w current cost and target cost is26
equal to min difference , so we have to update ans27
and takin minimum one as condition given in problem29
if (abs(curr - tar) == mn) ans = min(ans, curr);32
If we are going out of bound just return33
because we will nothing get after here35
if (i >= tc.size() || curr - tar > mn) return;37
/* Main interesting thing -> How to recurse */39
Just put all the required conditions41
NOTE: We have already taken 1 baseCosts, so left that42
We have to select from tc(toppingCosts)44
In our question we have 3 conditon45
1. Take ith toppingCosts and go for next (i+1)th toppingCost46
2. Take ith toppingCosts 2 times and go for next (i+1)th toppingCost47
3. We will not take ith toppingCosts and will select next ones50
// taking ith toppingCosts and moving to (i+1)th toppingCosts51
find(i + 1, tc, curr + tc[i], tar);53
// taking ith toppingCosts 2 times and moving to (i+1)th toppingCosts54
find(i + 1, tc, curr + 2 * tc[i], tar);56
// Without taking ith toppingCosts and move to (i+1)th toppingCosts for next58
find(i + 1, tc, curr, tar);60
int closestCost(vector<int> &baseCosts, vector<int> &toppingCosts, int target) {61
// take each baseCost every time62
for (auto x : baseCosts) {63
// taking ith baseCost and select toppingCosts64
find(0, toppingCosts, x, target);67
// return closest to target answer