Data Structures & AlgorithmsVery hard
Which property lets the 'longest subarray where every adjacent pair differs by exactly 1' be found in one pass?
- A. The condition depends on the global maximum only.
- B. The answer is always the array length.
- C. The condition is local: it holds for a block exactly when every adjacent pair satisfies it, so a run of consecutive good pairs can be tracked with one counter.
- D. Values must be sorted for the condition to be checked.
Correct answer: C. The condition is local: it holds for a block exactly when every adjacent pair satisfies it, so a run of consecutive good pairs can be tracked with one counter.
Because the condition is about adjacent pairs, a maximal run of good pairs gives the longest qualifying block, and a single counter tracks its length. Restarting at each broken pair handles the rest.
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
- When computing the minimum adjacent swaps by breadth-first search over arrangements, why does the first time a target is reached give the…
- Intervals [(1, 3), (2, 5), (4, 6), (3, 4), (6, 8), (5, 7)] are closed integer ranges. Choose as many as possible so that every integer…
- Closed real intervals [(1, 4), (3, 6), (8, 9), (9, 12), (14, 15)] are merged whenever they overlap (sharing an endpoint counts as…
- Over all pairs of DIFFERENT positions of [2, 5, 3, 8, 4], take the product of each pair and sort all these products. What is the 6-th…
- Starting from [9, 6, 15, 4], repeat 3 times: take the current LARGEST element, replace it by its floor half, and keep the others. What is…
- How many substrings of "cbaebabacd" of length 3 are anagrams of "abc" (contain exactly the same letters with the same counts, in any order)?