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
4mo

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!
Select
Add answer anonymously...

PhonePe Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Why do you want to join?
A Software Developer was asked 8mo agoQ. Given a graph, write a function to perform a Breadth-First Search (BFS) traversa...read more
A Software Developer was asked Q. Insertion in AVL Tree Problem Statement You are required to implement an AVL Tre...read more

Popular interview questions of Software Developer

A Software Developer was asked 6mo agoQ1. Why do you want to join?
A Software Developer was asked Q2. Insertion in AVL Tree Problem Statement You are required to implement an AVL Tre...read more
A Software Developer was asked Q3. Unlock the Briefcase Problem Statement You have a briefcase secured by a lock wi...read more
PhonePe Software Developer 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