Data Structures & AlgorithmsHard
How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] have equal first and last elements and length at least 2?
- A. 13
- B. 5
- C. 6
- D. 3
Correct answer: C. 6
Each equal-value pair at positions i < j gives one subarray with equal ends, so this is a pair-count per value. Counting singletons adds all n trivially equal ones, requiring length ≥ 3 drops the length-2 cases, and the distinct-value count is unrelated.
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
- Counting subarrays whose sum is divisible by k with prefix remainders, which statement is TRUE?
- Two pointers count subarrays of POSITIVE integers with sum at most K. Why can the left pointer only move forward as the right pointer moves?
- When summing the MAXIMUM over all subarrays with repeated values, the standard contribution method uses one strict and one non-strict…
- Which statement about subarrays with XOR equal to 0 is CORRECT?
- A zero-sum subarray counter keeps a map of prefix sums. At each index the current prefix is inserted into the map BEFORE the lookup. What…
- For pairs i < j with (ai + aj) divisible by an EVEN number m, with remainder counts cr, which formula is correct?