8

PROBLEM OF THE DAY : 23/02/2024 | Buy and Sell a Share at most twice

 6 months ago
source link: https://www.geeksforgeeks.org/videos/problem-of-the-day-23022024-buy-and-sell-a-share-at-most-twice/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Buy and Sell a Share at most twice

February 24, 2024 |470 Views
PROBLEM OF THE DAY : 23/02/2024 | Buy and Sell a Share at most twice
Problem of the Day, Array, Data Structures and Algorithms
 Save  Share  2 Likes
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Yash Dwivedi. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Arrays but also build up problem-solving skills.

In this problem, we are given a daily share trading scenario in which, a buyer buys shares in the morning and sells them on the same day. If the trader is allowed to make at most 2 transactions in a day, the second transaction can only start after the first one is complete (buy->sell->buy->sell). The stock prices throughout the day are represented in the form of an array of prices.

Given an array price of size n, find out the maximum profit that a share trader could have made.

Example :

Input:
n = 6
prices[] = {10,22,5,75,65,80}
Output:
87
Explanation:
Trader earns 87 as sum of 12, 75 Buy at 10, sell at 22, Buy at 5 and sell at 80.

Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/buy-and-sell-a-share-at-most-twice/1
Solution IDE Link: https://ide.geeksforgeeks.org/online-cpp14-compiler/aab99ff5-8218-4661-acd9-773a65c770e0

Read More

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK