Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B
How many subarrays (i ≤ j) of [2, 1, 2, 3, 1, 2] have their first and last elements equal? A single element counts (its first and last are the same position).
- A. 4
- B. 11
- C. 21
- D. 10
Correct answer: D. 10
Count pairs of positions with equal values: each value v appearing c times contributes c(c+1)/2 subarrays including the single-element ones. Forgetting the single-element subarrays undercounts by n.
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
- Closed integer intervals are [(1, 4), (2, 6), (5, 9), (3, 3), (6, 8), (7, 12)]. What is the largest number of intervals that share a…
- How many indices i of [2, 2, 0, 2, 2] have the sum of the elements strictly to the left of i equal to the sum of the elements strictly to…
- From the list 1 to 12, every node whose value is a perfect square (1, 4, 9) is deleted. What is the sum of the remaining values?
- Process [5, 3, 6, 2, 7, 1] with a stack: before each push, pop every element that is greater than or equal to the new value. How many…
- How many contiguous subarrays of [1, 2, 3, 4, 6, 2] have a product exactly equal to 12?
- How many index pairs i < j in [1, 2, 3, 4, 5, 6, 7, 8] have equal parity and a[i] + a[j] <= 10?