
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Pack Size from Given Range in C++
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.
Steps
To solve this, we will follow these steps −
if r / 2 >= l, then: return false Otherwise return true
Example
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; else return true; } int main(){ int l = 3; int r = 4; cout << solve(l, r) << endl; }
Input
3,4
Output
1