
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
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
- Related Articles
- C++ code to check phone number can be formed from numeric string
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- C++ code to count number of times stones can be given
- C++ code to check all bulbs can be turned on or not
- C++ code to get two numbers in range x with given rules
- C++ code to check reengagements can be done so elements sum is at most x
- C++ code to check given flag is stripped or not
- C++ code to check given matrix is good or not
- C++ code to check grasshopper can reach target or not
- C++ code to count maximum groups can be made
- C++ code to find out which number can be greater
- C++ Program to check given candies can be split with equal weights or not
- C++ code to find array from given array with conditions
- Queries to check whether a given digit is present in the given Range in C++
- C++ code to check pile count is valid from second day
