Data Structures & AlgorithmsHard
Choose a subsequence (elements need not be adjacent, but keep their original order) of [1, 9, 3, 4, 8, 2, 10, 5] of largest size such that max − min ≤ 2. What is that largest size?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
Sorting the values turns the condition into a window over sorted values, so the best subsequence is all values lying in a window of width 2. Restricting to contiguous subarrays loses the scattered values, and a wider bound overcounts.
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
- A string of length 4 is drawn from the alphabet {(, ), }. Each may be read as '(', as ')', or as the empty string. How many such strings…
- A linked list has nodes numbered 0 to 9 in order, and node 9's next pointer returns to node 3. Two pointers start at node 0: slow moves…
- In the sorted array [1, 1, 2, 2, 3, 4, 4, 5], how many different VALUE pairs {x, y} with x < y satisfy x + y = 6? (Repeated copies of a…
- Among all permutations of 1, 2, 3, 4 in which no two ADJACENT entries sum to 5, which one is lexicographically smallest? Give it as a…
- What is the smallest positive integer that is NOT the sum of any CONTIGUOUS (adjacent) block of [1, 2, 4, 8]?
- How many ways can [1, 2, 3, 4, 5, 7] be cut into one or more contiguous, non-empty segments so that EVERY segment has an even sum?