Question
Write the C code that solves the following problem with the mentioned algorithm. Algorithm: This is actually a slightly smarter brute-force algorithm. Let's think like
P[i+1], then P [i] is the local maximum. If P[i-1]> P [i]
7,1,5, 3, 6, 4 // of good i. sale prices for the day Output => 7 // the maximum profit obtained in the output will be written. Let the prices be 7,1,5, 3, 6, 4, respectively. The answer is 7 for this series. To achieve this, we buy for 1 lira on the 2nd day and sell for 5 lira on the 3rd day. Then on the 4th day we buy for 3 liras, on the 5th day we sell for 6 liras and our profit becomes 5-1 + 6-3 = 7. Example 2nd Input => 1,2,3,4,5 Output => 4 Let the prices be 1, 2, 3, 4, 5, respectively. The profit for this series is 4 lira, to get it, we buy 1 lira on day 1, we sell it for 5 lira on day 5, the profit is 5-1 = 4 lira Example 3.Input => 7, 6, 4, 3, 1 Output => 0 Let the prices be 7, 6, 4, 3, 1, respectively. The profit for this series is 0 lira, the day we buy the goods, we cannot make a profit from the sale because the price on the other days will be smaller
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