Question
You have been hired as a programmer at a Stock Trading company where your main objective is to tell when the best price is to
You have been hired as a programmer at a Stock Trading company where your main objective is to tell when the best price is to buy a particular stock and when to sell that particular stock. Your algorithm must be designed in such a way that when a particular stock is bought and sold the customer must have the maximum profit. Given a list of prices for a particular stock write a C++ program using arrays and other functions you seem fit to determine the maximum profit.
Note:
If are only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock). Also, you are not allowed to use any in built functions like max, min and sum.
Sample Output:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference/max profit = 6 - 1 = 5 (not 7 - 1 = 6, as selling price needs to be larger than buying price), which means you must buy the stock at $1 and sell at $6.
Input:
[7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
Any price you buy and sell the profit will still remain the same.
Input and Input Validation:
All the prices are positive numbers and there will be a maximum of 10 transactions. If any of them do not hold good print invalid inputs.
Input Scenarios:
Input 1: [6, 9, 100, 45, 21, 200, 600]
Input 2: [-10,9,0,0,9]
Input 3: [100, 45, 40, 25, 10, 5, 2]
Input 4: [135, 43, 100, 43, 500, 700]
Input 5: [1, 2, 3, 4, 2, 10, 25, 8, 3, 1]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started