Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B
A binary string is "10110010". Flipping position i costs weight[i] = [3, 1, 4, 1, 5, 9, 2, 6]. You want the string to alternate (0101... or 1010...). What is the minimum total flipping cost?
- A. 6
- B. 25
- C. 7
- D. 1
Correct answer: A. 6
For each of the two alternating targets, sum the weights of the positions where the current character differs, and take the cheaper target. Flipping only the cheapest positions can break alternation elsewhere.
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 contiguous subarrays of the binary array [1, 0, 1, 1, 0, 0, 1, 0] contain equally many 0s and 1s? (Non-empty subarrays only.)
- A string of brackets is ')))(()(' where '(' opens and ')' closes. What is the minimum number of brackets that must be removed to make the…
- How many elements of [2, 5, 1, 4, 3] have no strictly smaller element anywhere to their right?
- Three processes arrive at time 0 with bursts 5, 2 and 7. Round-robin uses quantum 3 and the queue starts as P1, P2, P3. Which process…
- A list holds 1 to 10 in order. All nodes whose value is prime are deleted. What is the sum of the remaining values?
- How many index pairs i < j in [1, 2, 3, 4, 5, 6, 7, 8] satisfy (a[i] + a[j]) divisible by 4?