Selling Stock Problem Statement

You are given the stock prices for N days. Each day i signifies the price of a stock on that day. Your goal is to calculate the maximum profit that can be achieved by buying and selling the stocks.

Note:

You can make as many transactions as you want, but you cannot hold more than one transaction at a time. This means you must sell the stock before buying it again.

Input:

The first line contains an integer 't' which indicates the number of test cases. For each test case:
  • The first line contains an integer 'N' denoting the number of days.
  • The second line contains 'N' space-separated integers representing the stock price on each day.

Output:

For each test case, print the maximum profit that can be earned. Print the output for each test case on a new line.

Example:

Input:
t = 1
N = 6
prices = [7, 1, 5, 3, 6, 4]
Output:
7

Constraints:

  • 1 ≤ t ≤ 102
  • 0 ≤ N ≤ 105
  • Time Limit: 1 sec
Note:

You are responsible solely for implementing the function to compute the result. There is no need to handle input/output operations.

AnswerBot
2d

Calculate maximum profit by buying and selling stocks on different days.

  • Iterate through the stock prices and buy on the day when the price is lower than the next day's price, and sell on the day when ...read more

Help your peers!
Add answer anonymously...
PhonePe Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter