Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,230 views

For proper oa or interview experiences list of all top tech companies visit :

https://oa.desiqna.in

https://interview.desiqna.in

 

image

 

in Online Assessments by Expert (44,360 points) | 1,230 views

2 Answers

0 like 0 dislike
Best answer

IMAGES OF QUESTION 

 

image

 

image

 

image

by Expert (44,360 points)
0 like 0 dislike
int solve(vector<int> nutrition, vector<int> price, int k, int m){ //C++20
    vector<vector<int>> dp(m+1, vector<int>(k+1));
    for (int a = 0; a < ssize(price); ++a){ // for each item
        for (int i = min(k, a+1); ~i; --i){ // for each discount remaining
            for (int j = m; j >= price[a]/2; --j){ // for all possible money
                if (j >= price[a] && a >= i){ // if current money is enough to buy no discount and that there is no discount used more than current item index
                    dp[j][i] = max(dp[j][i], dp[j-price[a]][i] + nutrition[a]);
                }
                if (i){ // if we have discount left.
                    dp[j][i] = max(dp[j][i], dp[j-price[a]/2][i-1] + nutrition[a]);
                }
            }
        }
    }
    return dp[m][k];
}

Test Cases

    cout << solve({20,17,15},{2,4,5},1,4) << '\n';
    cout << solve({9,10},{10,20},1,10) << '\n';

C:\WINDOWS\system32\cmd.exe /c (a)
37
10
Hit any key to close this window...
by Expert (44,360 points)