Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
What is the length of the longest well-formed (valid) parentheses substring of "()(()())))(()"?
- A. 10
- B. 12
- C. 8
- D. 9
Correct answer: C. 8
Use a stack of indices seeded with −1: on '(' push its index, on ')' pop and measure from the new top (or push the index if the stack empties). Counting matched pairs across the whole string overcounts substrings that are not contiguous.
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
- What is the minimum number of parentheses that must be ADDED (at any positions) to make "())(()))((" a valid parentheses string?
- For daily temperatures [73, 74, 75, 71, 69, 72, 76, 73], the wait for day i is the number of days until a strictly warmer day (0 if none)…
- Four processes need CPU bursts [5, 3, 8, 2] (all arrive at time 0). A round-robin scheduler gives each process up to a quantum of 2 units…
- Among all non-empty contiguous subarrays of 2 -1 4 -3 5 1, what is the largest sum that does NOT exceed 6?
- In how many ways can 1 2 2 0 3 2 1 2 2 be cut into exactly THREE non-empty contiguous parts whose sums are all equal? (A cut is a pair of…
- What is the maximum AVERAGE over all contiguous subarrays of 1 12 -5 -6 50 3 that have length at least 3? Give the answer as a fraction in…