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 Vegetable Market problem solution

YASH PAL, 31 July 2024
In this HackerEarth Vegetable Market problem solution, There are N stalls in a vegetable market that are selling vegetables. You have to buy an equal amount of vegetables from each stall. If a stall does not have enough vegetables to offer, then you have to buy all the vegetables available in that stall. If you are given the number of vegetables that every stall can offer, determine the minimum quantity that you need to buy from each stall such that you have at least K vegetables in total.
HackerEarth Vegetable Market problem solution

HackerEarth Vegetable Market problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define si(x) scanf("%d", &x)
#define sc(x) scanf("%c", &x)
#define sl(x) scanf("%lld", &x)
#define pl(x) printf("%lldn", x)
#define pi(x) printf("%dn", x)
#define gu getchar_unlocked
#define pu putchar_unlocked
#define setbits __builtin_popcountll
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define speed ios::sync_with_stdio(false)

ll arr[100005];
ll leftarr[1000005], rightarr[1000005];
vector <ll> quan(1000005, 0);

int main(){
int n, i;
si(n);
for(i = 1; i <= n; i++){
sl(arr[i]);
leftarr[arr[i]]++;
rightarr[arr[i]]++;
}
for(i = 1; i <= 1000002; i++){
leftarr[i] = leftarr[i - 1] + (i * leftarr[i]);
}
for(i = 1000002; i >= 0; i--){
rightarr[i] += rightarr[i + 1];
}
for(i = 1; i <= 1000002; i++){
quan[i] = leftarr[i - 1] + (rightarr[i] * i);
}
int q;
si(q);
while(q--){
ll K;
sl(K);
vector <ll> :: iterator it;
it = lower_bound(quan.begin(), quan.end(), K);
if(it != quan.end()){
int index = it - quan.begin();
pi(index);
}
else{
pi(-1);
}
}
}
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