Problem Statement
The Demand is given as p = a - b * q where p is the maximum price we can charge if we want to sell q items, and a and b are constants. q is number of items, and p is cents per item. Both must be non-negative integers.
Create a class HighestRevenue that contains a method optimalPrice that takes a and b as inputs and returns the price to charge that will result in the maximum revenue (p*q) possible. If two different prices result in the maximum revenue, return the larger price.
Definition
- Class:
- HighestRevenue
- Method:
- optimalPrice
- Parameters:
- int, int
- Returns:
- int
- Method signature:
- int optimalPrice(int a, int b)
- (be sure your method is public)
Notes
- The maximum revenue may be 0 (in which case your method should return a)
- Both p and q must be non-negative integers
Constraints
- a and b are between 1 and 10,000 inclusive
Examples
7000
50
Returns: 3500
p = 7000 - 50q. At a price of 3500, the quantity demanded will be 70 and the total revenue will be 70 * 3500 = 245,000. The next higher price we could charge would be 3550, resulting in the sale of 69 items generating a revenue of 69 * 3550 = 244,950. The next lower price we could charge would be 3450 generating sales of 71 items and revenue of 71 * 3450 = 244,950.70
35
Returns: 35
p = 70 - 35q The only prices that correspond to valid values for q are 70, 35, and 0. Only a price of 35 yields a positive revenue.70
80
Returns: 70
p = 70 - 80q At a price of 70, 0 units will be "sold" yielding a revenue of 0. At no other price will the corresponding quantity be a non-negative integer.5625
125
Returns: 2875
Both p=2875 and p=2750 yield the same maximal revenue of 63250, so the larger one is returned.