Question: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted

 Say you have an array for which the ith element is

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), write a C++ function maxProfit(int prices/], int size) to find the 2. maximum profit. Example 1: Input: prices = { 7, 1, 5,3, 6,4 } Output: 5 Max difference = 6-1-5 (not 7-1-6, as selling price needs to be larger than buying price) Example 2: Input, prices = 17, 6, 4, 31] Output: 0 In this case, no transaction is done, i.e. max profit -0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!