BackmediumArraysSwiggySalesforce

Maximize Price Difference Solution

Problem Statement

You are given an array of integers prices representing daily stock prices. Find the maximum possible return you can achieve by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Note that the day you buy must be before the day you sell.

Example 1
Input
[7,1,5,3,6,4]
Output
5

Explanation: Step-by-step: with input [7,1,5,3,6,4], we find the minimum price on day 2 which is 1. Then we find the maximum price after day 2 which is 6 on day 5. This gives us a maximum possible return of 6 - 1 = 5. We can also get a return of 5 by buying on day 0 and selling on day 5.

Example 2
Input
[7,6,4,3,1]
Output
0

Explanation: Step-by-step: with input [7,6,4,3,1], the prices are decreasing every day. This means we cannot sell for a higher price than we buy. Therefore, the maximum possible return is 0. We can also get a return of 0 by buying on day 4 and selling on day 4.

Constraints

  • 2 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximize Price Difference — Problem Statement & Solution Guide

ArraysMediumBasic Traversal
TimeO(n)
|
SpaceO(1)

Problem Description

You are given an array of integers prices representing daily stock prices. Find the maximum possible return you can achieve by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Note that the day you buy must be before the day you sell.

Examples

Example 1

Input

[7,1,5,3,6,4]

Output

5

Explanation: Step-by-step: with input [7,1,5,3,6,4], we find the minimum price on day 2 which is 1. Then we find the maximum price after day 2 which is 6 on day 5. This gives us a maximum possible return of 6 - 1 = 5. We can also get a return of 5 by buying on day 0 and selling on day 5.

Example 2

Input

[7,6,4,3,1]

Output

0

Explanation: Step-by-step: with input [7,6,4,3,1], the prices are decreasing every day. This means we cannot sell for a higher price than we buy. Therefore, the maximum possible return is 0. We can also get a return of 0 by buying on day 4 and selling on day 4.

Constraints

  • 2 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4

Optimal Approach & Strategy

The optimized approach involves iterating through the array once to find the maximum return. This approach has a linear time complexity and is efficient for large inputs. It can be implemented using a single loop to keep track of the minimum price and the maximum return seen so far.

Brute Force Approach

The brute force approach involves checking every possible buying and selling price combination. This approach has a high time complexity and is not efficient for large inputs. It can be implemented using nested loops to compare each pair of prices.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxReturn(prices) {
  if (prices.length < 2) return 0;
  let minPrice = prices[0];
  let maxReturn = 0;
  for (let i = 1; i < prices.length; i++) {
    if (prices[i] < minPrice) {
      minPrice = prices[i];
    } else if (prices[i] - minPrice > maxReturn) {
      maxReturn = prices[i] - minPrice;
    }
  }
  return maxReturn;
}

Asked in Top Tech Interviews

SwiggySalesforce

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.