Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional
What is the length of the longest contiguous subarray of [0, 1, 0, 0, 1, 1, 0, 1] containing equal numbers of 0s and 1s?
- A. 8
- B. 6
- C. 4
- D. 2
Correct answer: A. 8
Map 0 to −1 and store the first index of each prefix sum, seeding prefix 0 at index −1. The whole array has four 0s and four 1s, so the answer is 8. Without the seed, spans starting at index 0 are missed and the best is 6; 4 counts the zeros.
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 ordered pairs of distinct positions (i, j) in [2, 3, 4, 6, 12] satisfy a[j] divisible by a[i]?
- In how many ways can 12 be written as a sum of distinct positive odd integers (order does not matter)?
- How many integers from 1 to 60 inclusive have exactly four positive divisors?
- For [4, 2, 5, 3, 1], each index i gets the index of its next strictly smaller element to the right (or -1 if none). What is the sum of…
- A singly linked list holds the values 1 2 3 4 5 6 7 in order. It is reordered into L0 → Ln → L1 → Ln−1 → L2 → Ln−2 … using only node…
- From the linked list 5 2 13 3 8 7 9 4, delete every node that has a strictly greater value somewhere AFTER it in the list. Which list…