Data Structures & AlgorithmsHard
What is the minimum number of elements you must DELETE from [5, 1, 9, 4, 8, 3, 7] (the remaining elements keep their order but need not be adjacent) so that the remaining values have max − min ≤ 3?
- A. 6
- B. 3
- C. 4
- D. 5
Correct answer: C. 4
Sort the values; the best kept set is a window of sorted values of width at most 3, and the answer is n minus its size. Restricting to contiguous kept elements loses scattered choices, while confusing the kept count with the deleted count gives the wrong side.
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
- How many index pairs i < j in [2, 8, 3, 18, 4, 12, 5] have a product A[i]·A[j] that is a perfect square?
- How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8] have a sum that is a prime number?
- For each contiguous window of length 3 in [1, 2, 1, 3, 2, 1], count the number of distinct values inside it, then add these counts over…
- The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and a pop may be performed whenever the stack is…
- What is the minimum number of positive integers (each value at most 10, repetitions allowed in the array) needed so that EVERY integer…
- Given a sorted array, why is counting triplets with sum < T O(n²) rather than O(n³)?