Data Structures & AlgorithmsHard
From the digit string "53412", delete EXACTLY two characters (the remaining characters keep their order). What is the lexicographically LARGEST string that can remain?
- A. 245
- B. 345
- C. 543
- D. 542
Correct answer: D. 542
Greedily delete a smaller digit that is followed by a larger one, using a stack, which keeps the earliest digits as large as possible. Reversing the answer, or simply deleting the two smallest digits, ignores the order constraint.
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 DISTINCT non-empty strings can be formed as subsequences (characters taken in order, not necessarily adjacent) of "abab"?
- Arrange the numbers 3 30 34 5 9 in some order and concatenate their decimal digits. What is the LARGEST number that can be produced?
- From 4 9 1 7 3 8 2, choose positions so that no two CHOSEN positions are adjacent in the array. What is the maximum possible sum of the…
- How many index pairs i < j in 2 7 4 9 6 3 8 have an ODD sum A[i] + A[j] while also satisfying A[i] < A[j]?
- In 3 1 4 1 5 9 2 6 5 3, what is the largest possible index distance j − i between two positions i < j that hold EQUAL values?
- For 3 1 4 1 5 9 2 6 5 3, for each value that appears at least twice, add up the index gaps between consecutive occurrences of that value…