Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Lunch boxes problem solution

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

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes