# Week 10: Sliding Windows

#### Intro

The sliding window technique is a powerful algorithmic approach used to solve problems involving arrays or sequences efficiently. It operates by breaking down a larger data structure, typically an array, into smaller subsections or windows, and then iteratively moving these windows through the array to perform computations or checks. This method is particularly useful for solving optimization or search problems where you need to find a subarray or substring that meets certain criteria.

There are two types of “Sliding Windows” (Dynamic and Static). The two types Dynamic and Static Sliding Windows refer to the size of the window being adjustable or remaining fixed. Dynamic sliding windows are often used when the size of the desired subarray or substring is not predefined and can vary based on the input data. This flexibility allows for efficient exploration of different window sizes to find the optimal solution. On the other hand, static sliding windows are advantageous when the window size is known beforehand and remains constant, leading to simpler implementation and potentially better performance.

### 1) Maximum Contiguous Subarray of Length k

Given an array of integers called `nums`

and a size `k`

. Return the maximum sum of a contiguous subarray of size k.

**Example 1:**

```
Input: nums = [1, 2, 3, 4, 5], k = 3
Output: 12
Explanation: The possible subarrays of size 3 are [1, 2, 3], [2, 3, 4], and [3, 4, 5]. The maximum sum among these subarrays is 12, which corresponds to the subarray [3, 4, 5].
```

**Example 2:**

```
Input: nums = [1, 4, 2, 10, 23, 3, 1, 0, 20], k = 4
Output: 39
Explanation: Subarray of size 4 with maximum sum is [4, 2, 10, 23] which has a sum of 39.
```

**Example 3:**

```
Input: nums = [4, -2, 1, 7, -5, 3, -6], k = 4
Output: 10
Explanation: The possible subarrays of size 4 are [4, -2, 1, 7], [-2, 1, 7, -5], [1, 7, -5, 3], and [7, -5, 3, -6]. The maximum sum among these subarrays is 10, which corresponds to the subarray [1, 7, -5, 3].
```

*Constraints:*

`k <= nums.length`

,`k >= 0`

`-1000 <= nums[i] <= 1000`

### 2) Permutation in String

Given two strings `s1`

and `s2`

, return `true`

if `s2`

contains a permutation of `s1`

, or `false`

otherwise.

In other words, return `true`

if one of `s1`

’s permutations is the substring of `s2`

.

**Example 1:**

```
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
```

**Example 2:**

```
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
```

*Constraints:*

`1 <= s1.length`

,`s2.length <= 104`

`s1`

and`s2`

consist of lowercase English letters.

### 3) Maximize the Confusion of an Exam

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.
```

### 4) Subarray Product Less Than K

Given an array of integers `nums`

and an integer `k`

, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than `k`

.

**Example 1:**

```
Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
```

**Example 2:**

```
Input: nums = [1,2,3], k = 0
Output: 0
```

*Constraints:*

`1 <= nums.length <= 3 * 10^4`

`1 <= nums[i] <= 1000`

`0 <= k <= 10^6`

### 5) Longest Turbulent Subarray

Given an integer array `arr`

, return the length of a maximum size turbulent subarray of `arr`

.

A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.

More formally, a subarray `[arr[i], arr[i + 1], ..., arr[j]]`

of `arr`

is said to be turbulent if and only if:

For `i <= k < j`

:

`arr[k] > arr[k + 1]`

when`k`

is odd, and`arr[k] < arr[k + 1]`

when`k`

is even.

Or, for `i <= k < j`

:

`arr[k] > arr[k + 1]`

when`k`

is even, and`arr[k] < arr[k + 1]`

when`k`

is odd.

See explaination in Example 1.

**Example 1:**

```
Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
```

**Example 2:**

```
Input: arr = [4,8,12,16]
Output: 2
```

**Example 3:**

```
Input: arr = [100]
Output: 1
```

*Constraints:*

`1 <= arr.length <= 4 * 10^4`

`0 <= arr[i] <= 10^9`