Data Structures & AlgorithmsHardTCS Digital · Infosys SP

Daily prices are [15, 17, 19, 13, 20, 17, 38, 38, 40]. What is the maximum profit with at most ONE buy and one later sell, and with UNLIMITED non-overlapping transactions?

  1. A. 27 and 33
  2. B. 27 and 27
  3. C. 34 and 34
  4. D. 27 and 34

Correct answer: D. 27 and 34

One transaction: track the minimum price so far and the best difference (27) — max − min fails when the minimum comes after the maximum. Unlimited: add every positive day-to-day rise (34).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic