Header Ad

HackerEarth Eat Or Not? problem solution

In this HackerEarth Eat Or Not? problem solution Our friend Monk has decided to go on a diet in order to improve his Eating Habits. One of his friends told him that Monk should eat a fixed amount of Vitamins, Carbs, Fats and Proteins daily in order to loose weight. Monk wants to strictly follow this regime.

There are N fruits kept in Monk's Fridge. Each fruit contains a fixed amount of Vitamins, Carbs, Fats and Proteins. Now, Monk wants to know if upon selecting any subset of fruits from the fridge, the sum of their Vitamins, Carbs, Fats and Proteins is individually equal to their corresponding equivalent amount that Monk desires to eat. For example, if Monk selects Fruits i, j and k, then the sum of the vitamins of these 3 fruits should be equal to the amount of vitamins Monk wants to eat and so on for Carbs, Fats and Protiens as well.


HackerEarth Eat Or Not? problem solution


HackerEarth Eat Or Not? problem solution.

#include<stdio.h>
#include<iostream>
using namespace std;
int V[10001],C[10001],F[10001],P[10001], N;
bool isSubsetSum(int v,int c,int f,int p, int n )
{
// Base Cases
if (v == 0 &&c==0 && f==0 && p==0)
return true;
if (n == N && (v != 0 ||c!=0 || f!=0 || p!=0))
return false;

// If last element is greater than v, then ignore it
if (V[n] > v ||C[n]>c ||F[n]>f || P[n]>p)
return isSubsetSum(v,c,f,p, n+1);

/* else, check if v can be obtained by any of the following
(a) including the last element
(b) excluding the last element */
return isSubsetSum( v,c,f,p,n+1) || isSubsetSum(v-V[n],c-C[n],f-F[n],p-P[n], n+1);
}

// Driver program to test above function
int main()
{

string s;


int v ,c,f,p;
cin>>v>>c>>f>>p;

cin>>N;
for(int i=0;i<N;i++)
{
cin>>V[i]>>C[i]>>F[i]>>P[i];
}
if (isSubsetSum(v,c,f,p,0) == true)
{
s="Yes";
}

else
{

s="no";
}


cout<<s<<endl;

return 0;
}


Post a Comment

0 Comments