Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Lunch boxes problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Luch boxes problem solution, Alice works as a restaurant manager. The restaurant has prepared N lunch boxes and Alice plans to distribute them to some schools. Consider that there are M schools and an ith school orders Ai lunch boxes. She wants to distribute lunch boxes to as many schools as possible. Also, she has the following rule:
  1. For an ith school, she gives either zero or Ai lunch boxes
Your task is to help Alice to determine the maximum number of schools that can get lunch boxes.
hackerEarth Lunch boxes problem solution

HackerEarth Lunch boxes problem solution.

#include<bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 121;
int n, m;
int a[MAX_N];

void solve(){

cin >> n >> m;
for(int i = 1; i <= m; ++i){
cin >> a[i];
}

sort(a + 1,a + m + 1);

int cnt = 0;
int sum = 0;

for(int i = 1; i <= m; ++i){
sum += a[i];
if(sum <= n)++cnt; else break;
}

cout << cnt << endl;

}

int main(){

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int t;
cin >> t;
while(t-->0){
solve();
}

return 0;
}

second solution

import sys

input = lambda: sys.stdin.readline().rstrip("rn")

t = int(input())
while t > 0:
t -= 1
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
i = 0
while i < m and a[i] <= n:
n -= a[i]
i += 1
print(i)

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 *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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