Data Structures & AlgorithmsHard
What is the length of the shortest contiguous subarray of 3 8 1 5 8 2 3 7 whose two END elements are equal?
- A. 4
- B. 3
- C. 7
- D. 5
Correct answer: A. 4
For each value take the closest pair of equal positions; the length counts both ends, so add one to the index difference. Measuring the gap without adding one, or taking the widest pair, gives wrong values.
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 contiguous windows of length exactly 3 in 4 1 5 1 6 2 2 9 contain their minimum value AT LEAST TWICE?
- Count the pairs of positions i < j in 2 5 3 2 4 5 6 3 with EQUAL values such that the value does not occur strictly between them (i and j…
- For 3 8 2 9 4 7, compute for every index i the MAXIMUM of the suffix A[i..n−1], then add all these suffix maxima together. What is the…
- For 5 3 8 1 4 2, compute for every index i the MINIMUM of the prefix A[0..i], then add all these prefix minima together. What is the total?
- How many positions i in 1 1 2 2 3 1 4 2 satisfy: A[i] equals the number of DISTINCT values among A[0..i−1] (the elements strictly before i)?
- How many contiguous windows of length exactly 3 in 1 4 7 2 5 8 3 6 have their MIDDLE element equal to the average of the first and last…