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
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Lunch boxes problem solution

YASH PAL, 31 July 202413 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 *

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