BackmediumArraysZomato

Optimal Single Transaction Gain Solution

Problem Statement

Given a sequence of integers representing daily market values and a specified interval defined by start_index and end_index where 0 ≤ start_index < end_index < sequence length, determine the maximum achievable gain from a single acquisition and disposal within the designated interval.

Example 1
Input
[1, 5, 3, 7, 2, 8, 4, 6, 10, 9]
Output
7

Explanation: Step-by-step: with input [1, 5, 3, 7, 2, 8, 4, 6, 10, 9], we first find the maximum value in the interval [1, 5, 3, 7] which is 7. Then we find the minimum value in the interval [2, 8, 4, 6, 10, 9] which is 2. The maximum achievable gain is 7 - 2 = 5. However, we need to consider the correct pair of buy and sell indices. In this case, we can buy at index 3 (value 7) and sell at index 6 (value 8) which results in a gain of 1. But we can also buy at index 3 (value 7) and sell at index 9 (value 10) which results in a gain of 3. The maximum achievable gain is 3.

Example 2
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
9

Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we first find the maximum value in the interval [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] which is 10. Then we find the minimum value in the interval [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] which is 1. The maximum achievable gain is 10 - 1 = 9.

Constraints

  • 1 <= length of array <= 10^5
  • 0 <= stock prices <= 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

Optimal Single Transaction Gain — Problem Statement & Solution Guide

ArraysMediumKadane's / Prefix Sum
TimeO(n^2)
|
SpaceO(1)

Problem Description

Given a sequence of integers representing daily market values and a specified interval defined by start_index and end_index where 0 ≤ start_index < end_index < sequence length, determine the maximum achievable gain from a single acquisition and disposal within the designated interval.

Examples

Example 1

Input

[1, 5, 3, 7, 2, 8, 4, 6, 10, 9]

Output

7

Explanation: Step-by-step: with input [1, 5, 3, 7, 2, 8, 4, 6, 10, 9], we first find the maximum value in the interval [1, 5, 3, 7] which is 7. Then we find the minimum value in the interval [2, 8, 4, 6, 10, 9] which is 2. The maximum achievable gain is 7 - 2 = 5. However, we need to consider the correct pair of buy and sell indices. In this case, we can buy at index 3 (value 7) and sell at index 6 (value 8) which results in a gain of 1. But we can also buy at index 3 (value 7) and sell at index 9 (value 10) which results in a gain of 3. The maximum achievable gain is 3.

Example 2

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Output

9

Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we first find the maximum value in the interval [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] which is 10. Then we find the minimum value in the interval [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] which is 1. The maximum achievable gain is 10 - 1 = 9.

Constraints

  • 1 <= length of array <= 10^5
  • 0 <= stock prices <= 10^4

Optimal Approach & Strategy

The optimized approach utilizes Kadane's algorithm and prefix sum technique to find the maximum subarray sum within the given range, resulting in a time complexity of O(n).

Brute Force Approach

The brute-force approach involves iterating through the array and checking every possible buy and sell combination within the given range, resulting in a time complexity of O(n²). This approach is inefficient for large arrays.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function optimalSingleTransactionGain(marketValues, startIndex, endIndex) {
  let maxGain = 0;
  for (let i = startIndex; i < endIndex; i++) {
    for (let j = i + 1; j < endIndex; j++) {
      let currentGain = Math.max(marketValues[j] - marketValues[i], 0);
      if (currentGain > maxGain) {
        maxGain = currentGain;
      }
    }
  }
  return maxGain;
}

Asked in Top Tech Interviews

Zomato

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.