C++ code to check pack size can be determined from given range

Suppose we have two numbers l and r. There is a shop and we want to sell some food container with 'a' number of foods with a discount, and some customer wants to buy x foods. The customer following a greedy strategy −

  • He buys floor of (x/a) packs with discount

  • Then wants to buy remaining (x mod a) foods one by one.

But the customer is greedy, so if he wants to buy (x mod a) foods one by one and it happens that (x mod a) ≥ a/2, so he decides to buy the whole pack of a foods. A customer can buy any number of foods in range l to r (both inclusive), we have to check whether we can pick such size of pack a that each customer buys more cans than they wanted initially?

So, if the input is like l = 3; r = 4, then the output will be True, because if a = 5, so if they want to buy 3 or 4 cans they can buy a pack.


To solve this, we will follow these steps −

if r / 2 >= l, then:
   return false
   return true


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
bool solve(int l, int r){
   if (r / 2 >= l)
      return false;
      return true;
int main(){
   int l = 3;
   int r = 4;
   cout << solve(l, r) << endl;