Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Eat Or Not? problem solution

YASH PAL, 31 July 2024
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;
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes