Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

Prices [3, 8, 2, 9, 1, 7] are listed for six days. You pick two different days, buy on one and sell on the other, with no time-order requirement, so the profit is the sale price minus the purchase price. What is the maximum profit, or 0 if no profit is possible?

  1. A. 11
  2. B. 6
  3. C. 8
  4. D. 18

Correct answer: C. 8

Without a time-order rule, the best profit is the largest price minus the smallest price, found by trying every ordered pair of days (or by tracking the minimum and maximum). Using only the first day as the buy price, or summing the daily rises, answers a different question.

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

Exams that ask this topic