Data Structures & AlgorithmsVery hardCodeforces Div2 C · TCS NQT
A string of 8 brackets is "]][[][][" where '[' and ']' appear equally often. Each operation swaps any two characters. What is the minimum number of swaps needed to make the string a balanced bracket sequence?
- A. 1
- B. 2
- C. 4
- D. 0
Correct answer: A. 1
Count the unmatched closing brackets with a running balance: the number of swaps is ⌈unmatched/2⌉ after the greedy match. Counting the total number of misplaced brackets over-counts, since one swap can fix two of them.
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
- For each index i of [4, 2, 5, 3, 1, 6], let dist(i) be the distance to the next index j > i with a[j] < a[i] (dist = n − i if no such…
- Let A = [1, 4, 6, 9], B = [2, 3, 7] and S = [5, 9, 10, 13]. Count the ordered pairs (x from A, y from B) whose sum x + y lies in S.
- How many unordered pairs of positions in [5, 1, 4, 7, 2, 6] have bitwise XOR at most 3?
- What is the minimum number of characters you must DELETE from "aabbbccddddeee" so that every letter that remains appears the same number…
- A rod of length 17 must be cut into pieces each of length at most 5. What is the minimum number of cuts?
- What is the maximum number of DISJOINT non-empty contiguous subarrays of [1, -1, 2, -2, 3, 4, -7, 5, -5, 6] that each have sum 0?