Stock Buy and Sell Problem Statement

You are given an array of integers PRICES where PRICES[i] represents the price of a stock on the i-th day, and an integer K representing the number of transactions you can perform.

Your task is to determine the maximum profit that can be achieved with at most K transactions. A valid transaction is defined as buying and then selling a stock.

Note:

You cannot engage in multiple transactions simultaneously; you must sell a stock before buying again.

Example:

Input:
N = 6, PRICES = [3, 2, 6, 5, 0, 3], K = 2
Output:
7
Explanation:

The optimal strategy to maximize profit is to buy on day 2 (price = 2) and sell on day 3 (price = 6), and then buy on day 5 (price = 0) and sell on day 6 (price = 3). The total maximum profit will be (6 - 2) + (3 - 0) = 7.

Input Format:

The first line contains an integer T, the number of test cases. For each test case:
- The first line contains two integers, N and K, separated by a space.
- The second line contains N integers which are the elements of the PRICES array.

Output Format:

For each test case, output a single integer representing the maximum profit achievable with at most K transactions.

Print each test case's result on a new line.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 5000
  • 0 ≤ K ≤ N/2
  • 0 ≤ PRICES[i] ≤ 105
  • Time Limit: 1 second

Note:

Implementation does not require output printing; it is handled automatically. Just complete the function to return the maximum profit.
AnswerBot
4mo

Determine maximum profit with at most K transactions by buying and selling stocks on given days.

  • Iterate through the array of prices while keeping track of the maximum profit achievable with at most K ...read more

Help your peers!
Select
Add answer anonymously...

Visa Software Engineer interview questions & answers

A Software Engineer was asked 7mo agoQ. Explain the React lifecycle methods and how they work.
A Software Engineer was asked Q. Given an array of integers, rearrange the array such that all even numbers appea...read more
A Software Engineer was asked Q. How do you calculate the time taken by the kth person to collect n number of tic...read more

Popular interview questions of Software Engineer

A Software Engineer was asked 8mo agoQ1. Explain the React lifecycle methods and how they work.
A Software Engineer was asked Q2. How do you calculate the time taken by the kth person to collect n number of tic...read more
A Software Engineer was asked Q3. Given an array of integers, rearrange the array such that all even numbers appea...read more
Visa 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