Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Eat Or Not? problem solution

YASH PAL, 31 July 202416 February 2026
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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes