Best Time to Buy and Sell Stock II Problem Statement

Given the stock prices for a certain number of days, represented as an array, determine the maximum profit you can achieve. You may perform as many transactions as you like, but you must sell the stock before you buy again.

Input:

The input starts with an integer 't', the number of test cases that follow.
For each test case:
The first line contains an integer 'N' indicating the number of days.
The second line contains 'N' space-separated integers, where each integer denotes the price of the stock on that respective day.

Output:

For each test case, output the maximum profit possible on a new line.

Example:

Input:
t = 2
N = 6
prices = 7 1 5 3 6 4
N = 5
prices = 1 2 3 4 5
Output:
7
4
Explanation:

For the first test case, buying on day 2 and selling on day 3, then buying on day 4 and selling on day 5 yields a maximum profit of 7. For the second test case, buying on day 1 and selling on day 5 yields a maximum profit of 4.

Constraints:

  • 1 ≤ t ≤ 10^2
  • 0 ≤ N ≤ 10^5
  • Time Limit: 1 sec

Note:

Implement the function but do not write explicit printing logic, as it is handled separately.

AnswerBot
3mo

The problem involves finding the maximum profit that can be achieved by buying and selling stocks on different days.

  • Iterate through the array of stock prices and find the local minima and maxima to ca...read more

Help your peers!
Select
Add answer anonymously...

Clarivate Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 7mo agoQ. Given the head of a singly linked list, reverse the list, and return the reverse...read more
An Associate Software Engineer was asked Q. Given a string, reverse the alternate words in it.
An Associate Software Engineer was asked Q. Write a function to find the frequency of each letter in a given string.

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 8mo agoQ1. Given the head of a singly linked list, reverse the list, and return the reverse...read more
An Associate Software Engineer was asked Q2. Given a string, reverse the alternate words in it.
An Associate Software Engineer was asked Q3. Write a function to find the frequency of each letter in a given string.
Clarivate Associate Software Engineer 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