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 Fill the boxes problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Fill the boxes problem solution You are given an array of size N, denoting capacity of N boxes, and an integer K, denoting extended capacity factor. You are also given the weights of M balls. Each ith box can accommodate exactly one ball having weight in the range [capacityi, capacityj + K] (both inclusive). Find the maximum number of boxes that can be filled.
HackerEarth Fill the boxes problem solution

HackerEarth Fill the boxes problem solution.

#include <bits/stdc++.h>
#define sflld(n) scanf("%lld",&n)
#define sfulld(n) scanf("%llu",&n)
#define sfd(n) scanf("%d",&n)
#define sfld(n) scanf("%ld",&n)
#define sfs(n) scanf("%s",&n)
#define ll long long
#define s(t) int t; while(t--)
#define ull unsigned long long int
#define pflld(n) printf("%lldn",n)
#define pfd(n) printf("%dn",n)
#define pfld(n) printf("%ldn",n)
#define lt 2*idx
#define rt 2*idx+1
#define f(i,k,n) for(i=k;i<n;i++)
#define MAXN 100005


using namespace std;
int wt[MAXN],cap[MAXN];
int main()
{
int t,i,j;
sfd(t);
while(t--)
{
int n,m,k,x;
sfd(n);
sfd(m);
sfd(k);
f(i,0,n)
sfd(cap[i]);
f(i,0,m)
sfd(wt[i]);
sort(cap,cap+n);
sort(wt,wt+m);
i=0;
j=0;
int ans=0;
while(i<n&&j<m)
{
if(cap[i]<=wt[j]&&wt[j]<=cap[i]+k)
{
ans++;
i++;
j++;
}
else if(wt[j]<cap[i])
j++;
else
i++;
}
pfd(ans);
}
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 *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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