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?
- A. 3
- B. 4
- C. 6
- 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.
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
- What is the maximum value of x XOR y over all pairs of DIFFERENT positions of the list [7, 12, 5, 9, 14, 3]?
- Consider all non-empty contiguous subarrays of [2, 7, 3, 8, 6]. For each, take its sum modulo 10. What is the largest of these residues?
- A robot walks from the top-left corner of a 3 × 4 grid of cells to the bottom-right corner, moving only right or down. How many such paths…
- Two lists a = [4, 9, 1, 7] and b = [3, 8, 2, 10] are paired one-to-one (any bijection). What is the minimum possible value of the sum of…
- In the sequence [1, 3, 5, 2, 4, 6], how many strictly increasing subsequences have the maximum possible length (count by index positions)?
- Consider the routine that computes the length of the longest STRICTLY increasing subsequence of [2, 2, 3, 3, 4, 4, 5] using the tails…