Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

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 this. If you were allowed to buy only once and sell only once, you would want to buy at the lowest price and sell at the highest price, so the largest of the positive differences P [j] - P[i], j>i, would be our maximum profit. If none of these differences were positive, profit would be = 0. This would be max-min when the smallest element (min) of this array is to the left of the largest element (max). So if this situation exists, it would be sufficient to find max and min. Let's try to adapt this idea to our problem. For this, let's define the concepts of local minimum and local maximum. Actually, you know these concepts for functions. Let me explain on examples. For example, let's say 5,4,3,6,7,8,1,9. In this series, 3 and 1 are local minima. 5, 8 and 9 are local maximums. If the next element from the first element is less than the first element, the first element is a local maximum, and vice versa, the first element is a local minimum. If the previous element is less than the last element, the last element is a local maximum, otherwise the last element is a local minimum. For an initial and non-final element i, if P[i-1]

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions