Question: Latest Placement OA Coding Questions Solved | Strings & DP Knapsack
1
Entering edit mode

Question 1: Reverse ASCII Power

Problem Statement: You are given a string Str containing only lowercase letters. Your task is to calculate and print the Reverse ASCII power of the string.

The Reverse ASCII power of the string is calculated as the sum of for . Where is equal to .

(Note: , essentially assigning 'a'=26, 'b'=25... 'z'=1).

Input Format: The first line contains a string Str.

Output Format: Print the Reverse ASCII power of the string.

Example: Input: abc Output: 148

Question 2: Maximum Protein Value (Modified Knapsack)

Problem Statement: Shengij goes to a dairy to buy milkshakes for a party. There are milkshakes numbered from 1 to , where the price of the milkshake is price[i], and the protein value in the milkshake is protein[i].

Shengij has discount coupons, and he can use each coupon at most one time. By using a discount coupon, he can halve the price (floor value) of any milkshake (But no more than one coupon can be applied to the same milkshake). Shengij wants to buy milkshakes such that the sum of prices does not exceed and the total protein value is the maximum possible.

Your task is to help Shengij find the maximum protein value.

Input Format:

  • The first line contains an integer .
  • The second line contains an integer .
  • The third line contains an integer .
  • The fourth line contains space-separated integers denoting the elements of the price array.
  • The fifth line contains space-separated integers denoting the elements of the protein array.

Example: Input: 4 4 1 5 7 2 4 15 10 20 17 Output: 37

Explanation: Shengij can halve the price at most milkshake, and the total price does not exceed . It's better to halve the price of the fourth milkshake and buy the third and fourth milkshake. After buying the third and fourth milkshake, the total price = and the total protein value = . Therefore, the answer is 37.

ADD COMMENTlink 5 days ago admin 1.8k
0
Entering edit mode

Question 1: Reverse ASCII Power

Approach-:

  • We flip the alphabet so that 'a' is 26, 'b' is 25, and down to 'z' which is 1. We can calculate this dynamically using 26 - (char - 'a').

  • The example output for "abc" is 148, which comes from multiplying each reversed value by its 1-based position in the string (26 *1) + (25 *2) + (24*3) = 148.

  • We just loop through the string, calculate the weighted value for each character, and sum it all up into a single result variable.

#include <iostream>
#include <string>

using namespace std;

long long revAscii(string s) {
    long long ans = 0;
    for (int i = 0; i < s.length(); i++) {
        ans += (26 - (s[i] - 'a')) * (i + 1); // Value * 1-based index
    }
    return ans;
}

int main() {
    string s = "abc";
    cout << revAscii(s) << "\n";
    return 0;
}

 

ADD COMMENTlink 6 hours ago Rohit • 0
0
Entering edit mode

Question 2: Maximum Protein Value (Modified Knapsack)

Approach -: 

This is basically the classic "0-1 Knapsack" problem, but with a fun twist—you have a limited number of discount coupons. the logic:

  1. We need to track our current item, the remaining budget, and the remaining coupons. By iterating backward, we can compress this into a 2D Dynamic Programming (DP) table to save memory.

  2.  For every single milkshake, we make a decision: skip it entirely, buy it at full price (if budget allows), or use a coupon to halve the price (if budget and coupons allow).

  3. At every step, we check which of those three choices yields the highest total protein, updating our DP table to remember the best outcome for that specific budget and coupon count.

  4. Once we check all items, the bottom-right of our "table" (representing max budget and max coupons used) will hold the absolute maximum protein we can get.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int solveKnapsack(int n, int w, int k, vector<int>& p, vector<int>& v) {
    vector<vector<int>> dp(w + 1, vector<int>(k + 1, 0));
    
    for (int i = 0; i < n; i++) {
        for (int j = w; j >= 0; j--) {
            for (int c = k; c >= 0; c--) {
                if (j >= p[i]) {
                    dp[j][c] = max(dp[j][c], dp[j - p[i]][c] + v[i]);
                }
                if (j >= p[i] / 2 && c > 0) {
                    dp[j][c] = max(dp[j][c], dp[j - p[i] / 2][c - 1] + v[i]); // Try with coupon
                }
            }
        }
    }
    return dp[w][k];
}

int main() {
    int n = 4, w = 4, k = 1;
    vector<int> p = {5, 7, 2, 4};
    vector<int> v = {15, 10, 20, 17};
    
    cout << solveKnapsack(n, w, k, p, v) << "\n";
    return 0;
}

 

ADD COMMENTlink 6 hours ago Rohit • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts