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 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
Get step-by-step solutions from verified subject matter experts
