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?
- A. 11
- B. 6
- C. 8
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Choose two NON-OVERLAPPING contiguous windows of [5, -2, 8, 1, -3, 6, 2, 4]: one of length exactly 2 and one of length exactly 3. What is…
- How many pairs of positions (i, j) with j − i ≥ 3 satisfy a[i] = a[j], for the array [1, 2, 1, 3, 2, 1, 3, 1]?
- How many non-empty contiguous subarrays of [2, 2, 5, -1, 3, 4, 1] have a sum that is a perfect square (0 counts as a square)?
- How many non-empty subsets of [3, 1, 4, 2, 5] (chosen by position) have a sum between 4 and 7 inclusive?
- Choose a subsequence of length exactly 3 from [5, 1, 4, 2, 6, 3, 7, 2] (keeping order) whose elements are non-decreasing. What is the…
- How many DIFFERENT palindromic strings of length 3 occur as subsequences of "abcbaab" (each string counted once, however many index…