Maximize Stock Trading Profit

You are given an array prices, representing stock prices over N consecutive days. Your goal is to compute the maximum profit achievable by performing multiple transactions (i.e., buying one share and then selling one share multiple times).

Note: You must sell a stock before buying again.

Input:

N  
prices[0] prices[1] ... prices[N-1]

Output:

maximum_profit

Example:

Input:
6  
7 1 5 3 6 4
Output:
7
Explanation:

The maximum profit can be achieved by buying on day 2 (at price 1) and selling on day 3 (at price 5), profit = 5-1 = 4. Then buying again on day 4 (at price 3) and selling on day 5 (at price 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.

Constraints:

  • 1 <= N <= 5 * 104
  • 0 <= prices[i] <= 104
  • Time Limit: 1 second
AnswerBot
4mo

Calculate maximum profit by buying and selling stocks multiple times.

  • Iterate through the array of stock prices and find all increasing sequences of prices.

  • Calculate profit for each increasing sequence...read more

Help your peers!
Select
Add answer anonymously...

UnitedHealth Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. What are some SQL queries and JOIN operations you have worked with in your datab...read more
A Software Developer Intern was asked Q. Maximize Stock Trading Profit You are given an array prices, representing stock ...read more
A Software Developer Intern was asked Q. What are the differences between fragmentation and segmentation in operating sys...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. What are some SQL queries and JOIN operations you have worked with in your datab...read more
A Software Developer Intern was asked Q2. Maximize Stock Trading Profit You are given an array prices, representing stock ...read more
A Software Developer Intern was asked Q3. What are the differences between fragmentation and segmentation in operating sys...read more
UnitedHealth Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits