Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT

Consider the array [1, 3, 2, 5, 4, 7, 6, 9]. You may change the value of AT MOST ONE element (to any integer). What is the length of the longest contiguous subarray that can be made non-decreasing?

  1. A. 3
  2. B. 4
  3. C. 6
  4. D. 5

Correct answer: B. 4

Track the longest non-decreasing run from the left and right ends of each position, then join two runs across one changed element when the left run's last value and the right run's first value can be bridged by changing a single neighbour. Counting only already-sorted runs misses bridges.

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

Exams that ask this topic