Data Structures & AlgorithmsHard
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)?
- A. 6
- B. 2
- C. 3
- D. 1
Correct answer: B. 2
For each index count the distinct values strictly before it and compare that count with the element. Counting all earlier elements, including the current element in the set, or adding one each measure a different thing.
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 1 4 7 2 5 8 3 6 have their MIDDLE element equal to the average of the first and last…
- How many contiguous subarrays of 1 2 3 1 4 2 5 contain EXACTLY two distinct values that occur exactly once inside the subarray?
- Prices over days are 3 3 5 0 0 3 1 4. You may complete at most TWO buy-then-sell transactions, and you must sell before buying again. What…
- Consider all cyclic rotations of 3 1 2 5 4 (including the identity). For each rotation count its inversions (pairs i < j with a[i] >…
- Using only swaps of ADJACENT elements, what is the minimum number of swaps needed to make the MINIMUM value of 3 1 5 2 4 the FIRST element…
- What is the maximum number of pairwise DISJOINT contiguous blocks of 1 -1 2 3 -3 4 -4 5 whose sums are all zero? (Blocks must not overlap…