Open in App
Not now

# Javascript Program For Stock Buy Sell To Maximize Profit

• Last Updated : 10 Jan, 2023

The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given array of prices is sorted in decreasing order, then profit cannot be earned at all.

Naive approach: A simple approach is to try buying the stocks and selling them on every single day when profitable and keep updating the maximum profit so far.

Below is the implementation of the above approach:

## Javascript

 ``

Output:

`865`

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient approach: If we are allowed to buy and sell only once, then we can use following algorithm. Maximum difference between two elements. Here we are allowed to buy and sell multiple times.
Following is the algorithm for this problem.

1. Find the local minima and store it as starting index. If not exists, return.
2. Find the local maxima. and store it as an ending index. If we reach the end, set the end as the ending index.
3. Update the solution (Increment count of buy-sell pairs)
4. Repeat the above steps if the end is not reached.

## Javascript

 ``

Output:

```Buy on day: 0     Sell on day: 3
Buy on day: 4     Sell on day: 6```

Time Complexity: The outer loop runs till I become n-1. The inner two loops increment value of I in every iteration. So overall time complexity is O(n)

Space Complexity: O(1) since using constant variables

Please refer complete article on Stock Buy Sell to Maximize Profit for more details!

My Personal Notes arrow_drop_up
Related Articles
{"ft_lang_label":"__label__en","ft_lang_prob":0.7793472,"math_prob":0.892596,"size":3725,"snap":"2022-40-2023-06","text_gpt3_token_len":1013,"char_repetition_ratio":0.11179791,"word_repetition_ratio":0.06638418,"special_character_ratio":0.31248322,"punctuation_ratio":0.13654618,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96910304,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-27T07:55:27Z\",\"WARC-Record-ID\":\"<urn:uuid:0e1a843c-d21a-49f3-87d9-ef702b1022e7>\",\"Content-Length\":\"210087\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:41eee2a5-372e-4537-9c92-37ba2ab4d8de>\",\"WARC-Concurrent-To\":\"<urn:uuid:5f39027b-2a59-44b9-9d73-aaac5bdc8af5>\",\"WARC-IP-Address\":\"44.228.100.190\",\"WARC-Target-URI\":\"https://origin.geeksforgeeks.org/javascript-program-for-stock-buy-sell-to-maximize-profit/\",\"WARC-Payload-Digest\":\"sha1:HZ3ANPYFFNZYRCUKNW4UGE3GLZZKJKCO\",\"WARC-Block-Digest\":\"sha1:46JQGLYS4XB7VKP2SCEX476J7MKJLK5M\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764494974.98_warc_CC-MAIN-20230127065356-20230127095356-00736.warc.gz\"}"}