Question: Write the C code that solves the following problem with the mentioned algorithm. Algorithm: I will design this algorithm with the brute force method, but

 Write the C code that solves the following problem with the

mentioned algorithm. Algorithm: I will design this algorithm with the brute force

method, but I will do it in a similar way to the

pseudo-code a little less reduce-manage method. Our main idea is very simple,

let's assume that we buy the goods today in order for each

day, and then sell it for each day with higher prices than

Write the C code that solves the following problem with the mentioned algorithm. Algorithm: I will design this algorithm with the brute force method, but I will do it in a similar way to the pseudo-code a little less reduce-manage method. Our main idea is very simple, let's assume that we buy the goods today in order for each day, and then sell it for each day with higher prices than today. In the days after selling, let's continue the same transaction, calculate the profit and update our profit if this profit is more than our previous profit. Let me explain on Example1. Let profit = 0 initially. For i = 1, the price is 7. Let's get it at this price. If we look at other days, we cannot sell as prices are less than 7 and profit remains = 0. For i = 2, price = 1. Let's get it at this price. Let's try selling every day that we can make a profit. On the 3rd day, the price is 5 and the profit when we sell it = 5-1 = 4. Now, if we buy it for 3 liras on the 4th day, we can sell it on the 5th and 6th days. Since the total profit will be 4 + 6-3 = 7 if we sell on the 5th day, 4 +4-3 = 5 if we sell on the 6th day, our new profit is 7. But the case i = 2 is not over. Now, let's sell the goods we bought on the 2nd day, the profit will be 3-1 = 2 on the 4th day, and then we cannot make a profit even though we buy this item on the 5th day. In this case, the profit will not be updated since the total profit will be 2. Yet the i = 2 case is not over. If we sell the goods on the 2nd day, the profit will be = 6-1 = 5. However, since 5 P[i]j. we want to sell per day. Let us have a Max Kar (P, k, m) function that we can calculate the maximum profit in the interval [k, m). Naturally, MaxKar (P, k, m) = 0 if k>= m. In this case [b, s] interval i. take a day j. that we get from selling in a day total profit = MaxKar (P, b, i-1) + P [j] -P [i] + MaxKar (P, j +1, s). Below is the pseudo code of the algorithm. The first call of this code is MaxKar (P, 1, n). Max Kar (P, b, s) if x=b then retum 0 Kar-0 for i-b to s for j=i+1 to s if P[j]>P[i] then yeni_kar P[j]-P[i]+Max Kar(P, b, i-1)+Max Kar(P, j+1, s) if yeni_kar>Kar then Kar-new_kar return Kar Now let me explain this code again on Examplel (P= 7,1,5, 3, 6, 4). Called MaxKar (P, 1, n). So b=1, s=n became. The first if condition is not met. Profit = 0. Loopi = 1, n started. For i = 1, j = 2, loop n starts, but since P [1] = 7, no condition P[j]> P[i] is satisfied for any j. For i = 2, j = 3, loop n begins. For j = 3 new_kar =P [3] -P [2] + MaxKar (P, 1, 1) + MaxKar (P, 4, 6) = 5-1 + 0 + Max Kar (P, 4, 6) = 4 + Becomes Maxkar (P, 4.6). To calculate this number, the code will call itself for b = 4, s = 6. Again, the first if condition is not met. Inside this call, Profit = 0. The cycle i = 4,6 begins. For i = 4, the loop j = 5, 6 begins. Since P [5]>P[4] for j = 5, new_kar = P[5] -P [4] + Max Kar (P,4,3) + Max Kar (P, 6,6) = 3 in this call. Profit = 3 because new_kar = 3> Profit = 0 in this call. For j = 6, new_kar = P [6] -P[4] + Max Kar (P, 4, 3) + MaxKar (P, 7, 6) = 1. Profit = 3, since new_profit = 1 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

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!