| ||You are planning to release a new product on the market and you want to find a strategy that will maximize your profit. That strategy will include fixing the optimal price for the product. You have made a customer list containing the maximum price each potential customer is willing to pay. You also know how much it costs to deliver the product to each of those customers. You are responsible for paying all the shipping costs, so if it's too expensive to deliver the product to a customer, you can choose not to sell to that customer.|
You will be given ints price and cost, where price[i] is the maximum price customer i is willing to pay for the product and cost[i] is the cost of delivering the product to customer i. Return the price for the product that maximizes profit. If there are multiple such prices, return the smallest among them. If it's impossible to achieve a positive profit, return 0.
|Method signature:||int optimalPrice(int price, int cost)|
|(be sure your method is public)|
|-||price will contain between 1 and 50 elements, inclusive.|
|-||Each element of price will be between 1 and 10^6, inclusive.|
|-||cost will contain the same number of elements as price.|
|-||Each element of cost will be between 0 and 10^6, inclusive.|
|If we sell the product at 13 then all three would buy it.(3x13=39)|
If we sell the product at 22 then only two would buy it. (2x22=44)
If we sell the product at 35 then only one would buy it. (1x35=35)
So, 22 is the optimal price for our product.
|If we sell the product at 13 then all three would buy it, but we would only sell to the first one.(13-5=8)|
If we sell the product at 22 then only two would buy it, but we would only sell to the second one.(22-15=7).
If we sell the product at 35 then only one would buy it. (35-30=5)
So, 13 is the optimal price for our product.
|Here it is too expensive to sell to anyone. So the optimal price is 0.|
|If we sell the product at 10 we gain 9 from the first customer and 5 from the second one(Total profit = 14). If we sell the product at 20 we gain 9 from the third customer and 5 from the fourth one(Total profit = 14). So both 10 and 20 are optimal prices but we must choose the smallest one.|