Data Structures & AlgorithmsVery hard
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?
- A. 9
- B. 2
- C. 8
- D. 10
Correct answer: A. 9
For each value record its first and last positions; the widest gap for a value is last minus first, and the answer is the maximum over all values. Using the nearest pair, the array length, or unequal pairs misreads the question.
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
- 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…
- How many contiguous subarrays of length exactly 3 in 1 2 1 3 3 2 3 1 1 read the same forwards and backwards?
- How many contiguous subarrays of 1 2 3 1 2 4 1 contain EXACTLY three distinct values?
- How many contiguous subarrays of 2 4 3 6 1 5 have a sum that is divisible by the number of DISTINCT values in them?
- For every window of exactly 3 consecutive elements of 7 3 9 4 8 2 6, compute (maximum − minimum) inside that window. What is the SMALLEST…
- A sorted linked list holds 1 2 2 3 3 3 4 4 4 4. Delete nodes so that each value keeps AT MOST TWO nodes, preserving order. How many nodes…