SICK NINJA

Hard
0/120
Average time to solve is 40m
profile
Contributed by
0 upvote

Problem statement

Ninja is having some health issues. So he decided to buy fruits to get nutrition. There are ‘N’ number of fruits. Each fruit has some amount of nutrition and a specific cost.

Ninja has ‘M’ units of money. You need to find the maximum amount of nutrition Ninja can get and the total cost to get maximum nutrition. The ninja can buy any number of fruits, but he can spend only at most ‘M’ units of money.

Note: If there are multiple values of costs possible for maximum nutrition, you need to find the minimum one.

Example:
Suppose there are 3 fruits with costs 1, 2, 3 and nutrition  2, 2, 3 units respectively. Ninja has 3 units of money. Then, the ninja can take fruits with costs 1 and 2, having the maximum nutritional value of  4 units for a cost of 3 units.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains ‘T,’ denoting the number of test cases.

The first line of each test case contains two space-separated integers, ‘M’ and ‘N’, denoting the amount of money ninja has and the total number of fruits.

The next ‘N’ line contains two space-separated integers, ‘X’ and ‘Y’, denoting each fruit's cost and nutrition value.
Output Format :
Print two space-separated integers the maximum amount of nutrition Ninja can get and the total cost to get maximum nutrition.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1  <= N <= 1000
1 < = M <=1000
0 < = X <= 1000
0 < = Y <= 1000

Time Limit: 1 sec
Sample Input 1 :
2
9 4
1 2
2 3
4 5
5 7
6 3
7 4
9 2
11 12
Sample Output 1 :
12 8
 0 0
Explanation of sample input 1 :
For the first test case:-
The ninja can select the fruits at indices ( 0 - based ) 0, 1, and 3, which constitutes 12 units of nutrition and 8 units of cost.

For the second test case:-
The Ninja can not buy any of these fruits so he will have his nutrition as 0 and cost also 0 as well
Sample Input 2 :
2
50 10
12 3
15 8
16 9
16 6
10 2
21 9
18 4
12 4
17 8
18 9 
50 10
13 8
19 10
16 8
12 9
10 2
12 8
13 5
15 5
11 7
16 2
Sample Output 2:
26 49
32 48
Hint

 Try all the sets of fruits possible.

Approaches (3)
Recursion

We can recursively try out all the possible sets of fruits possible. We can add the nutrition and cost of each fruit we include in the set. We have two options for each fruit either to take it or not. We can recursively try out for both of these options and return the best of them. 


 

When we reach the end of the array, we can simply return the current value of nutrition and cost.


 

The steps are as follows:-
 

// Recursive function to find maximum strength

function  getMaxNutritionUtil(int index, [FRUIT] FRUITS,int N, 

                           int cost, int nutrition, int M ):
 

  1. If index==N
    • If ( cost > M )
      • Return ( nutrition, cost ) = ( 0, 0 ).
    • Return ( nutrition, cost ).
  2. Call getMaxNutritionUtil with ( index + 1, FRUITS, N, cost + FRUITS [ index ].cost, nutrition +  FRUITS [ index ].nutrition, M ) and store it in a variable ‘maxNutrition1’.
  3. Call getMaxNutritionUtil with ( index + 1, FRUITS, N, cost, nutrition, M ) and store it in a variable ‘‘maxNutrition2’.
  4. If ( ‘maxNutrition2.nutrition > ‘maxNutrition1.nutrition ).
    • Return ‘maxNutrition2’.
  5. If ( maxNutrition2.nutrition == maxNutrition1.nutrition and maxNutrition2.cost < ‘maxNutrition1.cost).
    • Return ‘maxNutrition2’.
  6. Return ‘maxNutrition1’.
     

function getMaxNutrition ( [FRUIT] FRUITS, int N, int M):

 

  1. Call getMaxNutritionUtil with index as 0, cost as 0 and nutrition as 0 and store it in a variable ‘maxNutritionr’.
  2. Return ‘maxNutrition’.
Time Complexity

O(  2), Where ‘N’ is the number of fruits.

 

Each fruit has two choices. A total of N fruits are in the array, so a total of 2N  possibilities is there.

 

Hence The Time complexity is O( 2).

Space Complexity

O ( N ), Where ‘N’ is the number of fruits.

 

Extra auxiliary space will be needed for the recursion call stack.

 

Hence space complexity is O( N ).

Code Solution
(100% EXP penalty)
SICK NINJA
All tags
Sort by
Search icon

Interview problems

C++

#include<bits/stdc++.h>
fruit f(int ind, int n, int money, vector<fruit> &a, vector<vector<fruit>> &dp) {
    fruit ans;
    ans.nutrition=0;
    ans.cost=0;
    if(money <= 0  || ind == n ) return ans;
    if (dp[ind][money].nutrition != -1) return dp[ind][money];
    fruit take, notTake, val;
    val.cost = 0;
    val.nutrition = 0;
    take.cost = 0;
    notTake.cost = 0;
    take.nutrition = 0;
    notTake.nutrition = 0;
    notTake=f(ind+1, n, money, a, dp);
    if(money >= a[ind].cost) {
        val.cost += a[ind].cost;
        val.nutrition += a[ind].nutrition;
        fruit temp = f(ind+1, n, money-a[ind].cost, a, dp);
        take.cost = val.cost + temp.cost;
        take.nutrition = val.nutrition + temp.nutrition;
    }
    if(take.nutrition == notTake.nutrition) {
        ans.nutrition = take.nutrition;
        ans.cost=min(take.cost, notTake.cost);
    } else {
        if(take.nutrition > notTake.nutrition) {
            ans.nutrition = take.nutrition;
            ans.cost = take.cost;
        } else {
            ans.nutrition = notTake.nutrition;
            ans.cost = notTake.cost;
        }
    }
    return dp[ind][money] = ans;
}
fruit getMaxNutrition(int n, int money, vector<fruit> a) {
    vector<vector<fruit>> dp(n + 1, vector<fruit>(money + 1, fruit(-1, -1)));
    return f(0, n, money, a, dp);
}
23 views
0 replies
0 upvotes

Interview problems

JAVA || DP || MEMOIZATION

import java.util.*;
public class Solution {
    private static fruit[][] dp;
    public static fruit getMaxNutrition(int n, int m, fruit []fruits) {
        // Write your code here
        dp = new fruit[n+1][m+1];
        //Arrays.sort(fruits , (a,b) -> a.cost == b.cost ? a.nutrition - b.nutrition : a.cost-b.cost);
        
        return getFruit(n, m, 0, fruits);
    }
    public static fruit getFruit(int n, int m,int idx, fruit []fruits){
        if( m <= 0 || n == idx) return new fruit(0,0);
        if(dp[idx][m] != null) return dp[idx][m];
        fruit temp = new fruit(0,0);
        for(int i = idx ;i < n ;i++){
            if(m-fruits[i].cost >= 0){
                fruit f = getSum(fruits[i], getFruit(n, m-fruits[i].cost, i+1, fruits));
                if(compareTo(f, temp) == 1){
                    temp = f;
                }
            }
        }
        return dp[idx][m] = temp;
    }
    public static fruit getSum(fruit f1,fruit f2){
        fruit temp =  new fruit();
        temp.cost = f1.cost+f2.cost ; temp.nutrition = f1.nutrition + f2.nutrition;
        return temp;
    }
    public static int compareTo(fruit f1,fruit f2){
        if(f1.nutrition > f2.nutrition) return 1;
        if(f1.nutrition < f2.nutrition) return -1;
        if(f1.nutrition == f2.nutrition){
            return f1.cost - f2.cost < 0 ? 1 : -1 ;
        }
        return -1;
    }
}
17 views
0 replies
0 upvotes

Interview problems

DP | C++

fruit getMaxNutrition(int n, int m, vector<fruit> fruits){

    vector<int> nutrition(m+1,0);

    for(fruit f : fruits){

        for(int cost=m;cost>=f.cost;cost--){

            nutrition[cost] = max(nutrition[cost],f.nutrition + nutrition[cost-f.cost]);

        }

    }

    int moneySpent = m;

    while(moneySpent >= 1 && nutrition[moneySpent] == nutrition[moneySpent-1]) moneySpent--;

    return fruit(moneySpent,nutrition[moneySpent]);

}

26 views
0 replies
0 upvotes
Full screen
Console