Introduction
Professor Reddy is a Physics teacher. He will be organizing a physics test that will contain True / False questions. Each question has an answer which is either true or false. He wants to design the question paper to maximize the confusion for the students. One way he can do this is to keep a series of questions whose answer is True, then keep a series of questions whose answer is False, and so on. Thus he can create confusion among the students. Let us now discuss the problem statement and the possible solution.
Recommended topic, kth largest element in an array, and Euclid GCD Algorithm
Problem Statement
A teacher is creating an exam containing N true/false questions, with the letters 'T' indicating true and 'F' indicating false. He intends to confuse the students by increasing the number of questions with the same answer in succession (multiple trues or multiple falses in a row). You're given a string ‘ANSWER_KEY’, where ANSWER_KEY[i] is the ‘i’th question's original answer. You're also provided with an integer ‘K’, which represents the maximum number of times you can conduct the following operation: Set any question's answer key to 'T' or 'F' (i.e., set ANSWER_KEY[i] to 'T' or 'F'). After completing the operation ‘K’ times, return the maximum number of consecutive 'T's or 'F's in the answer key.
Example 1:
ANSWER_KEY = ”FFTTTTTT”, K = 2.
We can toggle the first two ‘F’ to ‘T’ as K = 2. So we can do the operation two times at max. Thus, the final string will be “TTTTTTTT”. As a result, there are four consecutive ‘T’ in a row.
Output: 8
Example 2:
ANSWER_KEY = ”TTFTTTTT”, K = 1.
We can toggle ‘F’ at index 2. Thus the modifies ‘ANSWER_KEY’ will be “TTTTTTTT”. In both cases, we have 8 consecutive ‘T’. Therefore the output is 8.
Output: 8