# 23. Maximize the Confusion of an Exam / Max Consecutive Ones III

A teacher is writing a test with `n` true/false questions, with `'T'` denoting true and `'F'` denoting false. He wants to confuse the students by **maximizing** the number of **consecutive** questions with the **same** answer (multiple trues or multiple falses in a row).

You are given a string `answerKey`, where `answerKey[i]` is the original answer to the `ith` question. In addition, you are given an integer `k`, the maximum number of times you may perform the following operation:

* Change the answer key for any question to `'T'` or `'F'` (i.e., set `answerKey[i]` to `'T'` or `'F'`).

Return *the **maximum** number of consecutive* `'T'`s or `'F'`s *in the answer key after performing the operation at most* `k` *times*.�

**Example 1:**

```
Input: answerKey = "TTFF", k = 2
Output: 4
Explanation: We can replace both the 'F's with 'T's to make answerKey = "TTTT".
There are four consecutive 'T's.
```

**Example 2:**

```
Input: answerKey = "TFFT", k = 1
Output: 3
Explanation: We can replace the first 'T' with an 'F' to make answerKey = "FFFT".
Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF".
In both cases, there are three consecutive 'F's.
```

**Example 3:**

```
Input: answerKey = "TTFTTFTT", k = 1
Output: 5
Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT"
Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT". 
In both cases, there are five consecutive 'T's.
```

## Solution: (Sliding Window)

**Finding a window with max no true having k false and vice versa**

```cpp
class Solution
{
public:
    int findMaxCons(string s, int k, char val)
    {

        int start = 0;
        int count = 0;

        int maxLen = INT_MIN;

        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == val)
            {
                count++;
            }

            while (count > k)
            {

                if (s[start] == val)
                {
                    count--;
                }
                start++;
            }

            maxLen = max(maxLen, i - start);
        }
        
        return maxLen+1;
    }

    int maxConsecutiveAnswers(string answerKey, int k)
    {
        
        return max(findMaxCons(answerKey, k, 'T'), findMaxCons(answerKey, k, 'F'));
    }
};
```

**Time Complexity: O(n)**

## Max Consecutive Ones III

Given a binary array `nums` and an integer `k`, return *the maximum number of consecutive* `1`*'s in the array if you can flip at most* `k` `0`'s.

**Example 1:**

```
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
```

**Example 2:**

```
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
```

## Solution:

```cpp
class Solution
{
public:
    int longestOnes(vector<int> &nums, int k)
    {

        int start = 0;
        int count = 0;
        int maxLen = INT_MIN;

        for (int i = 0; i < nums.size(); i++)
        {

            if (nums[i] == 0)
            {
                count++;
            }

            while (count > k)
            {
                if (nums[start] == 0)
                {
                    count--;
                }
                start++;
            }
            
            maxLen = max(maxLen, i - start + 1);
        }

        return maxLen;
    }
};
```
