# Divide an Array Into Subarrays With Minimum Cost I

You are given an array of integers `nums` of length `n`.

The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`.

You need to divide `nums` into `3` **disjoint contiguous** subarrays.

Return *the **minimum** possible **sum** of the cost of these subarrays*.

**Example 1:**

```

**Input:** nums = [1,2,3,12]
**Output:** 6
**Explanation:** The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.

```

**Example 2:**

```

**Input:** nums = [5,4,3]
**Output:** 12
**Explanation:** The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.

```

**Example 3:**

```

**Input:** nums = [10,3,1,1]
**Output:** 12
**Explanation:** The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.

```

**Constraints:**

* `3 <= n <= 50`
* `1 <= nums[i] <= 50`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumCost(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minimumCost(**[[1, 2, 3, 12]]) == 6
assert my_solution.minimumCost(**[[5, 4, 3]]) == 12
assert my_solution.minimumCost(**[[10, 3, 1, 1]]) == 12
assert my_solution.minimumCost(**[[1, 1, 1]]) == 3
assert my_solution.minimumCost(**[[1, 1, 2]]) == 4
assert my_solution.minimumCost(**[[1, 1, 3]]) == 5
assert my_solution.minimumCost(**[[1, 1, 4]]) == 6
assert my_solution.minimumCost(**[[1, 1, 5]]) == 7
assert my_solution.minimumCost(**[[1, 2, 1]]) == 4
assert my_solution.minimumCost(**[[1, 2, 2]]) == 5
assert my_solution.minimumCost(**[[1, 2, 3]]) == 6
assert my_solution.minimumCost(**[[1, 2, 4]]) == 7
assert my_solution.minimumCost(**[[1, 2, 5]]) == 8
assert my_solution.minimumCost(**[[1, 3, 1]]) == 5
assert my_solution.minimumCost(**[[1, 3, 2]]) == 6
assert my_solution.minimumCost(**[[1, 3, 3]]) == 7
assert my_solution.minimumCost(**[[1, 3, 4]]) == 8
assert my_solution.minimumCost(**[[1, 3, 5]]) == 9
assert my_solution.minimumCost(**[[1, 4, 1]]) == 6
assert my_solution.minimumCost(**[[1, 4, 2]]) == 7


# Find if Array Can Be Sorted

You are given a **0-indexed** array of **positive** integers `nums`.

In one **operation**, you can swap any two **adjacent** elements if they have the **same** number of set bits. You are allowed to do this operation **any** number of times (**including zero**).

Return `true` *if you can sort the array, else return* `false`.

**Example 1:**

```

**Input:** nums = [8,4,2,30,15]
**Output:** true
**Explanation:** Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
We can sort the array using 4 operations:
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
The array has become sorted, hence we return true.
Note that there may be other sequences of operations which also sort the array.

```

**Example 2:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** true
**Explanation:** The array is already sorted, hence we return true.

```

**Example 3:**

```

**Input:** nums = [3,16,8,4,2]
**Output:** false
**Explanation:** It can be shown that it is not possible to sort the input array using any number of operations.

```

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 28`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def canSortArray(self, nums: List[int]) -> bool:
```

my_solution = Solution()

assert my_solution.canSortArray(**[[8, 4, 2, 30, 15]]) == True
assert my_solution.canSortArray(**[[1, 2, 3, 4, 5]]) == True
assert my_solution.canSortArray(**[[3, 16, 8, 4, 2]]) == False
assert my_solution.canSortArray(**[[1]]) == True
assert my_solution.canSortArray(**[[4]]) == True
assert my_solution.canSortArray(**[[7]]) == True
assert my_solution.canSortArray(**[[10]]) == True
assert my_solution.canSortArray(**[[18]]) == True
assert my_solution.canSortArray(**[[30]]) == True
assert my_solution.canSortArray(**[[1, 2]]) == True
assert my_solution.canSortArray(**[[2, 17]]) == True
assert my_solution.canSortArray(**[[20, 16]]) == False
assert my_solution.canSortArray(**[[21, 17]]) == False
assert my_solution.canSortArray(**[[24, 12]]) == True
assert my_solution.canSortArray(**[[26, 10]]) == False
assert my_solution.canSortArray(**[[128, 128]]) == True
assert my_solution.canSortArray(**[[1, 2, 3]]) == True
assert my_solution.canSortArray(**[[1, 256, 64]]) == True
assert my_solution.canSortArray(**[[2, 28, 9]]) == False
assert my_solution.canSortArray(**[[6, 6, 192]]) == True


# Minimize Length of Array Using Operations

You are given a **0-indexed** integer array `nums` containing **positive** integers.

Your task is to **minimize** the length of `nums` by performing the following operations **any** number of times (including zero):

* Select **two** **distinct** indices `i` and `j` from `nums`, such that `nums[i] > 0` and `nums[j] > 0`.
* Insert the result of `nums[i] % nums[j]` at the end of `nums`.
* Delete the elements at indices `i` and `j` from `nums`.

Return *an integer denoting the **minimum** **length** of* `nums` *after performing the operation any number of times.*

**Example 1:**

```

**Input:** nums = [1,4,3,1]
**Output:** 1
**Explanation:** One way to minimize the length of the array is as follows:
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
nums becomes [1,1,3].
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
nums becomes [1,1].
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
nums becomes [0].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
```

**Example 2:**

```

**Input:** nums = [5,5,5,10,5]
**Output:** 2
**Explanation:** One way to minimize the length of the array is as follows:
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
nums becomes [5,5,5,5].
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3.
nums becomes [5,5,0].
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
nums becomes [0,0].
The length of nums cannot be reduced further. Hence, the answer is 2.
It can be shown that 2 is the minimum achievable length.
```

**Example 3:**

```

**Input:** nums = [2,3,4]
**Output:** 1
**Explanation:** One way to minimize the length of the array is as follows:
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
nums becomes [2,3].
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
nums becomes [1].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
```

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumArrayLength(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minimumArrayLength(**[[1, 4, 3, 1]]) == 1
assert my_solution.minimumArrayLength(**[[5, 5, 5, 10, 5]]) == 2
assert my_solution.minimumArrayLength(**[[2, 3, 4]]) == 1
assert my_solution.minimumArrayLength(**[[1]]) == 1
assert my_solution.minimumArrayLength(**[[3]]) == 1
assert my_solution.minimumArrayLength(**[[6]]) == 1
assert my_solution.minimumArrayLength(**[[1, 4]]) == 1
assert my_solution.minimumArrayLength(**[[1, 5]]) == 1
assert my_solution.minimumArrayLength(**[[2, 4]]) == 1
assert my_solution.minimumArrayLength(**[[3, 4]]) == 1
assert my_solution.minimumArrayLength(**[[5, 3]]) == 1
assert my_solution.minimumArrayLength(**[[6, 9]]) == 1
assert my_solution.minimumArrayLength(**[[8, 2]]) == 1
assert my_solution.minimumArrayLength(**[[9, 9]]) == 1
assert my_solution.minimumArrayLength(**[[9, 10]]) == 1
assert my_solution.minimumArrayLength(**[[1, 2, 5]]) == 1
assert my_solution.minimumArrayLength(**[[2, 1, 1]]) == 1
assert my_solution.minimumArrayLength(**[[2, 7, 10]]) == 1
assert my_solution.minimumArrayLength(**[[2, 9, 5]]) == 1
assert my_solution.minimumArrayLength(**[[3, 2, 1]]) == 1


# Divide an Array Into Subarrays With Minimum Cost II

You are given a **0-indexed** array of integers `nums` of length `n`, and two **positive** integers `k` and `dist`.

The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`.

You need to divide `nums` into `k` **disjoint contiguous** subarrays, such that the difference between the starting index of the **second** subarray and the starting index of the `kth` subarray should be **less than or equal to** `dist`. In other words, if you divide `nums` into the subarrays `nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)]`, then `ik-1 - i1 <= dist`.

Return *the **minimum** possible sum of the cost of these* *subarrays*.

**Example 1:**

```

**Input:** nums = [1,3,2,6,4,2], k = 3, dist = 3
**Output:** 5
**Explanation:** The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.

```

**Example 2:**

```

**Input:** nums = [10,1,2,2,2,1], k = 4, dist = 3
**Output:** 15
**Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.

```

**Example 3:**

```

**Input:** nums = [10,8,18,9], k = 3, dist = 1
**Output:** 36
**Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.

```

**Constraints:**

* `3 <= n <= 105`
* `1 <= nums[i] <= 109`
* `3 <= k <= n`
* `k - 2 <= dist <= n - 2`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumCost(self, nums: List[int], k: int, dist: int) -> int:
```

my_solution = Solution()

assert my_solution.minimumCost(**[[1, 3, 2, 6, 4, 2], 3, 3]) == 5
assert my_solution.minimumCost(**[[10, 1, 2, 2, 2, 1], 4, 3]) == 15
assert my_solution.minimumCost(**[[10, 8, 18, 9], 3, 1]) == 36
assert my_solution.minimumCost(**[[1, 1, 1], 3, 1]) == 3
assert my_solution.minimumCost(**[[1, 1, 3], 3, 1]) == 5
assert my_solution.minimumCost(**[[1, 2, 2], 3, 1]) == 5
assert my_solution.minimumCost(**[[1, 2, 5], 3, 1]) == 8
assert my_solution.minimumCost(**[[1, 4, 4], 3, 1]) == 9
assert my_solution.minimumCost(**[[2, 2, 1], 3, 1]) == 5
assert my_solution.minimumCost(**[[2, 3, 2], 3, 1]) == 7
assert my_solution.minimumCost(**[[2, 5, 4], 3, 1]) == 11
assert my_solution.minimumCost(**[[3, 1, 2], 3, 1]) == 6
assert my_solution.minimumCost(**[[3, 1, 3], 3, 1]) == 7
assert my_solution.minimumCost(**[[3, 2, 2], 3, 1]) == 7
assert my_solution.minimumCost(**[[3, 3, 2], 3, 1]) == 8
assert my_solution.minimumCost(**[[3, 4, 1], 3, 1]) == 8
assert my_solution.minimumCost(**[[3, 5, 3], 3, 1]) == 11
assert my_solution.minimumCost(**[[4, 1, 4], 3, 1]) == 9
assert my_solution.minimumCost(**[[4, 1, 5], 3, 1]) == 10
assert my_solution.minimumCost(**[[4, 2, 1], 3, 1]) == 7


# Count Elements With Maximum Frequency

You are given an array `nums` consisting of **positive** integers.

Return *the **total frequencies** of elements in*`nums` *such that those elements all have the **maximum** frequency*.

The **frequency** of an element is the number of occurrences of that element in the array.

**Example 1:**

```

**Input:** nums = [1,2,2,3,1,4]
**Output:** 4
**Explanation:** The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.

```

**Example 2:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** 5
**Explanation:** All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.

```

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maxFrequencyElements(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.maxFrequencyElements(**[[1, 2, 2, 3, 1, 4]]) == 4
assert my_solution.maxFrequencyElements(**[[1, 2, 3, 4, 5]]) == 5
assert my_solution.maxFrequencyElements(**[[15]]) == 1
assert my_solution.maxFrequencyElements(**[[10, 12, 11, 9, 6, 19, 11]]) == 2
assert my_solution.maxFrequencyElements(**[[2, 12, 17, 18, 11]]) == 5
assert my_solution.maxFrequencyElements(**[[19, 19, 19, 20, 19, 8, 19]]) == 5
assert my_solution.maxFrequencyElements(**[[1, 1, 1, 1]]) == 4
assert my_solution.maxFrequencyElements(**[[10, 1, 12, 10, 10, 19, 10]]) == 4
assert my_solution.maxFrequencyElements(**[[1, 1, 1, 20, 6, 1]]) == 4
assert my_solution.maxFrequencyElements(**[[17, 17]]) == 2
assert my_solution.maxFrequencyElements(**[[6, 13, 15, 15, 11, 6, 7, 12, 4, 11]]) == 6
assert my_solution.maxFrequencyElements(**[[1, 2]]) == 2
assert my_solution.maxFrequencyElements(**[[14, 14, 17]]) == 2
assert my_solution.maxFrequencyElements(**[[17, 17, 2, 12, 20, 17, 12]]) == 3
assert my_solution.maxFrequencyElements(**[[3, 9, 11, 11, 20]]) == 2
assert my_solution.maxFrequencyElements(**[[8, 15, 8, 11, 8, 13, 12, 11, 8]]) == 4
assert my_solution.maxFrequencyElements(**[[17, 8, 17, 19, 17, 13, 17, 17, 17, 5]]) == 6
assert my_solution.maxFrequencyElements(**[[11]]) == 1
assert my_solution.maxFrequencyElements(**[[5]]) == 1
assert my_solution.maxFrequencyElements(**[[4, 4, 10]]) == 2


# Find Beautiful Indices in the Given Array I

You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`.

An index `i` is **beautiful** if:

* `0 <= i <= s.length - a.length`
* `s[i..(i + a.length - 1)] == a`
* There exists an index `j` such that:
	+ `0 <= j <= s.length - b.length`
	+ `s[j..(j + b.length - 1)] == b`
	+ `|j - i| <= k`

Return *the array that contains beautiful indices in **sorted order from smallest to largest***.

**Example 1:**

```

**Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
**Output:** [16,33]
**Explanation:** There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.

```

**Example 2:**

```

**Input:** s = "abcd", a = "a", b = "a", k = 4
**Output:** [0]
**Explanation:** There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.

```

**Constraints:**

* `1 <= k <= s.length <= 105`
* `1 <= a.length, b.length <= 10`
* `s`, `a`, and `b` contain only lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
```

my_solution = Solution()

assert my_solution.beautifulIndices(**['isawsquirrelnearmysquirrelhouseohmy', 'my', 'squirrel', 15]) == [16, 33]
assert my_solution.beautifulIndices(**['abcd', 'a', 'a', 4]) == [0]
assert my_solution.beautifulIndices(**['sqgrt', 'rt', 'sq', 3]) == [3]
assert my_solution.beautifulIndices(**['mquz', 'tklr', 'caz', 4]) == []
assert my_solution.beautifulIndices(**['wl', 'xjigt', 'wl', 2]) == []
assert my_solution.beautifulIndices(**['bavgoc', 'ba', 'c', 6]) == [0]
assert my_solution.beautifulIndices(**['xpcp', 'yxnod', 'xpc', 4]) == []
assert my_solution.beautifulIndices(**['lahhnlwx', 'hhnlw', 'ty', 6]) == []
assert my_solution.beautifulIndices(**['dexgscgecd', 'gscge', 'd', 6]) == [3]
assert my_solution.beautifulIndices(**['vjrao', 'vjr', 'yxpsw', 5]) == []
assert my_solution.beautifulIndices(**['oo', 'swhup', 'o', 1]) == []
assert my_solution.beautifulIndices(**['bxlzgxc', 'ducf', 'xlzgx', 3]) == []
assert my_solution.beautifulIndices(**['wetlgztzm', 'box', 'wetl', 4]) == []
assert my_solution.beautifulIndices(**['ocmm', 'm', 'oc', 3]) == [2, 3]
assert my_solution.beautifulIndices(**['goxmox', 'gibs', 'ox', 6]) == []
assert my_solution.beautifulIndices(**['kzlrqzldvy', 'zl', 'tfsr', 9]) == []
assert my_solution.beautifulIndices(**['qhd', 'hd', 'od', 1]) == []
assert my_solution.beautifulIndices(**['bozpeh', 'bozp', 'vrjn', 2]) == []
assert my_solution.beautifulIndices(**['ggfsg', 'gfsg', 'g', 4]) == [1]
assert my_solution.beautifulIndices(**['fape', 'vq', 'ap', 4]) == []

isawsquirrelnearmysquirrelhouseohmy my squirrel 15


# Maximum Number That Sum of the Prices Is Less Than or Equal to K

You are given an integer `k` and an integer `x`.

Consider `s` is the **1-indexed** binary representation of an integer `num`. The **price** of a number `num` is the number of `i`'s such that `i % x == 0` and `s[i]` is a **set bit**.

Return *the **greatest** integer* `num` *such that the sum of **prices** of all numbers from* `1` *to* `num` *is less than or equal to* `k`*.*

**Note**:

* In the binary representation of a number **set bit** is a bit of value `1`.
* The binary representation of a number will be indexed from right to left. For example, if `s == 11100`, `s[4] == 1` and `s[2] == 0`.

**Example 1:**

```

**Input:** k = 9, x = 1
**Output:** 6
**Explanation:** The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
Since x is equal to 1, the price of each number is the number of its set bits.
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
So the answer is 6.
```

**Example 2:**

```

**Input:** k = 7, x = 2
**Output:** 9
**Explanation:** Since x is equal to 2, we should just check eventh bits.
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
Numbers 1, 4, and 5 don't have set bits in their eventh bits in their binary representation. So the sum of their prices is 0.
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
The sum of the prices of the first 9 numbers is 6.
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.
```

**Constraints:**

* `1 <= k <= 1015`
* `1 <= x <= 8`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findMaximumNumber(self, k: int, x: int) -> int:
```

my_solution = Solution()

assert my_solution.findMaximumNumber(**[9, 1]) == 6
assert my_solution.findMaximumNumber(**[7, 2]) == 9
assert my_solution.findMaximumNumber(**[19, 6]) == 50
assert my_solution.findMaximumNumber(**[57, 4]) == 120
assert my_solution.findMaximumNumber(**[58, 5]) == 121
assert my_solution.findMaximumNumber(**[60, 8]) == 187
assert my_solution.findMaximumNumber(**[72, 5]) == 151
assert my_solution.findMaximumNumber(**[81, 6]) == 176
assert my_solution.findMaximumNumber(**[83, 1]) == 33
assert my_solution.findMaximumNumber(**[83, 7]) == 210
assert my_solution.findMaximumNumber(**[116, 5]) == 243
assert my_solution.findMaximumNumber(**[157, 6]) == 316
assert my_solution.findMaximumNumber(**[201, 3]) == 212
assert my_solution.findMaximumNumber(**[268, 6]) == 555
assert my_solution.findMaximumNumber(**[281, 5]) == 531
assert my_solution.findMaximumNumber(**[283, 3]) == 274
assert my_solution.findMaximumNumber(**[309, 4]) == 364
assert my_solution.findMaximumNumber(**[363, 7]) == 746
assert my_solution.findMaximumNumber(**[409, 2]) == 220
assert my_solution.findMaximumNumber(**[456, 7]) == 967


# Find Beautiful Indices in the Given Array II

You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`.

An index `i` is **beautiful** if:

* `0 <= i <= s.length - a.length`
* `s[i..(i + a.length - 1)] == a`
* There exists an index `j` such that:
	+ `0 <= j <= s.length - b.length`
	+ `s[j..(j + b.length - 1)] == b`
	+ `|j - i| <= k`

Return *the array that contains beautiful indices in **sorted order from smallest to largest***.

**Example 1:**

```

**Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
**Output:** [16,33]
**Explanation:** There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.

```

**Example 2:**

```

**Input:** s = "abcd", a = "a", b = "a", k = 4
**Output:** [0]
**Explanation:** There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.

```

**Constraints:**

* `1 <= k <= s.length <= 5 * 105`
* `1 <= a.length, b.length <= 5 * 105`
* `s`, `a`, and `b` contain only lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
```

my_solution = Solution()

assert my_solution.beautifulIndices(**['isawsquirrelnearmysquirrelhouseohmy', 'my', 'squirrel', 15]) == [16, 33]
assert my_solution.beautifulIndices(**['abcd', 'a', 'a', 4]) == [0]
assert my_solution.beautifulIndices(**['a', 'a', 'a', 1]) == [0]
assert my_solution.beautifulIndices(**['aba', 'a', 'a', 1]) == [0, 2]
assert my_solution.beautifulIndices(**['nvnvt', 'eq', 'nv', 1]) == []
assert my_solution.beautifulIndices(**['npearbvede', 'myqpb', 'pearb', 9]) == []
assert my_solution.beautifulIndices(**['vatevavakz', 'va', 'lbda', 1]) == []
assert my_solution.beautifulIndices(**['ithhi', 't', 'hhi', 1]) == [1]
assert my_solution.beautifulIndices(**['osuv', 'osuv', 'wrn', 1]) == []
assert my_solution.beautifulIndices(**['dc', 'dreec', 'dc', 2]) == []
assert my_solution.beautifulIndices(**['jajrfw', 'rf', 'j', 3]) == [3]
assert my_solution.beautifulIndices(**['zcvx', 'kfdvv', 'tru', 1]) == []
assert my_solution.beautifulIndices(**['wltmqbxt', 'mqbxt', 'lt', 7]) == [3]
assert my_solution.beautifulIndices(**['gggsytwgzg', 'sytwg', 'g', 4]) == [3]
assert my_solution.beautifulIndices(**['diive', 'viw', 'lqqdn', 4]) == []
assert my_solution.beautifulIndices(**['ss', 'omkdt', 's', 1]) == []
assert my_solution.beautifulIndices(**['hfzoxcm', 'hfzo', 'ipelr', 1]) == []
assert my_solution.beautifulIndices(**['xllimtmil', 'imt', 'iwqx', 5]) == []
assert my_solution.beautifulIndices(**['vdyl', 'i', 'ir', 4]) == []
assert my_solution.beautifulIndices(**['ouwpaz', 'mxre', 'pa', 5]) == []


# Maximum Area of Longest Diagonal Rectangle

You are given a 2D **0-indexed** integer array `dimensions`.

For all indices `i`, `0 <= i < dimensions.length`, `dimensions[i][0]` represents the length and `dimensions[i][1]` represents the width of the rectangle `i`.

Return *the **area** of the rectangle having the **longest** diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the **maximum** area.*

**Example 1:**

```

**Input:** dimensions = [[9,3],[8,6]]
**Output:** 48
**Explanation:**
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.

```

**Example 2:**

```

**Input:** dimensions = [[3,4],[4,3]]
**Output:** 12
**Explanation:** Length of diagonal is the same for both which is 5, so maximum area = 12.

```

**Constraints:**

* `1 <= dimensions.length <= 100`
* `dimensions[i].length == 2`
* `1 <= dimensions[i][0], dimensions[i][1] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
```


# Maximum Size of a Set After Removals

You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`.

You must remove `n / 2` elements from `nums1` and `n / 2` elements from `nums2`. After the removals, you insert the remaining elements of `nums1` and `nums2` into a set `s`.

Return *the **maximum** possible size of the set* `s`.

**Example 1:**

```

**Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1]
**Output:** 2
**Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
It can be shown that 2 is the maximum possible size of the set s after the removals.

```

**Example 2:**

```

**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
**Output:** 5
**Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
It can be shown that 5 is the maximum possible size of the set s after the removals.

```

**Example 3:**

```

**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
**Output:** 6
**Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
It can be shown that 6 is the maximum possible size of the set s after the removals.

```

**Constraints:**

* `n == nums1.length == nums2.length`
* `1 <= n <= 2 * 104`
* `n` is even.
* `1 <= nums1[i], nums2[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.maximumSetSize(**[[1, 2, 1, 2], [1, 1, 1, 1]]) == 2
assert my_solution.maximumSetSize(**[[1, 2, 3, 4, 5, 6], [2, 3, 2, 3, 2, 3]]) == 5
assert my_solution.maximumSetSize(**[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6]]) == 6
assert my_solution.maximumSetSize(**[[1, 2, 1, 1], [1, 2, 3, 4]]) == 4
assert my_solution.maximumSetSize(**[[1, 1, 1, 1], [12, 23, 41, 9]]) == 3
assert my_solution.maximumSetSize(**[[12, 23, 41, 9], [1, 1, 1, 1]]) == 3
assert my_solution.maximumSetSize(**[[9, 8, 4, 7], [5, 5, 9, 5]]) == 4
assert my_solution.maximumSetSize(**[[8, 9], [4, 3]]) == 2
assert my_solution.maximumSetSize(**[[7, 1], [6, 10]]) == 2
assert my_solution.maximumSetSize(**[[10, 3], [5, 6]]) == 2
assert my_solution.maximumSetSize(**[[3, 6], [6, 6]]) == 2
assert my_solution.maximumSetSize(**[[5, 1], [6, 6]]) == 2
assert my_solution.maximumSetSize(**[[10, 7], [8, 4]]) == 2
assert my_solution.maximumSetSize(**[[10, 8, 7, 9], [7, 9, 9, 5]]) == 4
assert my_solution.maximumSetSize(**[[1, 10, 6, 5], [3, 7, 10, 10]]) == 4
assert my_solution.maximumSetSize(**[[5, 2, 8, 6], [7, 4, 1, 1]]) == 4
assert my_solution.maximumSetSize(**[[2, 4, 1, 4], [10, 2, 4, 10]]) == 4
assert my_solution.maximumSetSize(**[[5, 7], [3, 1]]) == 2
assert my_solution.maximumSetSize(**[[1, 10, 1, 2], [9, 5, 8, 5]]) == 4
assert my_solution.maximumSetSize(**[[9, 4], [5, 7]]) == 2


# Maximize the Number of Partitions After Operations

You are given a **0-indexed** string `s` and an integer `k`.

You are to perform the following partitioning operations until `s` is **empty**:

* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters.
* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order.

**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter.

Return *an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change.*

**Example 1:**

```

**Input:** s = "accca", k = 2
**Output:** 3
**Explanation:** In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'.
s becomes "acbca".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 2 distinct characters, "acbca".
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
- Choose the longest prefix containing at most 2 distinct characters, "bca".
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
- Choose the longest prefix containing at most 2 distinct characters, "a".
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
Hence, the answer is 3.
It can be shown that it is not possible to obtain more than 3 partitions.
```

**Example 2:**

```

**Input:** s = "aabaab", k = 3
**Output:** 1
**Explanation:** In this example, to maximize the number of resulting partitions we can leave s as it is.
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 3 distinct characters, "aabaab".
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
Hence, the answer is 1.
It can be shown that it is not possible to obtain more than 1 partition.

```

**Example 3:**

```

**Input:** s = "xxyz", k = 1
**Output:** 4
**Explanation:** In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'.
s becomes "xayz".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 1 distinct character, "xayz".
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
- Choose the longest prefix containing at most 1 distinct character, "ayz".
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
- Choose the longest prefix containing at most 1 distinct character, "yz".
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
- Choose the longest prefix containing at most 1 distinct character, "z".
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
Hence, the answer is 4.
It can be shown that it is not possible to obtain more than 4 partitions.

```

**Constraints:**

* `1 <= s.length <= 104`
* `s` consists only of lowercase English letters.
* `1 <= k <= 26`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
```

my_solution = Solution()

assert my_solution.maxPartitionsAfterOperations(**['accca', 2]) == 3
assert my_solution.maxPartitionsAfterOperations(**['aabaab', 3]) == 1
assert my_solution.maxPartitionsAfterOperations(**['xxyz', 1]) == 4
assert my_solution.maxPartitionsAfterOperations(**['c', 3]) == 1
assert my_solution.maxPartitionsAfterOperations(**['c', 5]) == 1
assert my_solution.maxPartitionsAfterOperations(**['h', 17]) == 1
assert my_solution.maxPartitionsAfterOperations(**['p', 13]) == 1
assert my_solution.maxPartitionsAfterOperations(**['ab', 5]) == 1
assert my_solution.maxPartitionsAfterOperations(**['ba', 1]) == 2
assert my_solution.maxPartitionsAfterOperations(**['ba', 3]) == 1
assert my_solution.maxPartitionsAfterOperations(**['ca', 1]) == 2
assert my_solution.maxPartitionsAfterOperations(**['fh', 8]) == 1
assert my_solution.maxPartitionsAfterOperations(**['abb', 1]) == 3
assert my_solution.maxPartitionsAfterOperations(**['aca', 2]) == 2
assert my_solution.maxPartitionsAfterOperations(**['acb', 2]) == 2
assert my_solution.maxPartitionsAfterOperations(**['acb', 4]) == 1
assert my_solution.maxPartitionsAfterOperations(**['bab', 3]) == 1
assert my_solution.maxPartitionsAfterOperations(**['cba', 1]) == 3
assert my_solution.maxPartitionsAfterOperations(**['cbb', 5]) == 1
assert my_solution.maxPartitionsAfterOperations(**['cca', 5]) == 1


# Smallest Missing Integer Greater Than Sequential Prefix Sum

You are given a **0-indexed** array of integers `nums`.

A prefix `nums[0..i]` is **sequential** if, for all `1 <= j <= i`, `nums[j] = nums[j - 1] + 1`. In particular, the prefix consisting only of `nums[0]` is **sequential**.

Return *the **smallest** integer* `x` *missing from* `nums` *such that* `x` *is greater than or equal to the sum of the **longest** sequential prefix.*

**Example 1:**

```

**Input:** nums = [1,2,3,2,5]
**Output:** 6
**Explanation:** The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.

```

**Example 2:**

```

**Input:** nums = [3,4,5,1,12,14,13]
**Output:** 15
**Explanation:** The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.

```

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def missingInteger(self, nums: List[int]) -> int:
```


# Minimum Number of Operations to Make Array XOR Equal to K

You are given a **0-indexed** integer array `nums` and a positive integer `k`.

You can apply the following operation on the array **any** number of times:

* Choose **any** element of the array and **flip** a bit in its **binary** representation. Flipping a bit means changing a `0` to `1` or vice versa.

Return *the **minimum** number of operations required to make the bitwise* `XOR` *of **all** elements of the final array equal to* `k`.

**Note** that you can flip leading zero bits in the binary representation of elements. For example, for the number `(101)2` you can flip the fourth bit and obtain `(1101)2`.

**Example 1:**

```

**Input:** nums = [2,1,3,4], k = 1
**Output:** 2
**Explanation:** We can do the following operations:
- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
It can be shown that we cannot make the XOR equal to k in less than 2 operations.

```

**Example 2:**

```

**Input:** nums = [2,0,2,0], k = 0
**Output:** 0
**Explanation:** The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.

```

**Constraints:**

* `1 <= nums.length <= 105`
* `0 <= nums[i] <= 106`
* `0 <= k <= 106`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
```

my_solution = Solution()

assert my_solution.minOperations(**[[2, 1, 3, 4], 1]) == 2
assert my_solution.minOperations(**[[2, 0, 2, 0], 0]) == 0
assert my_solution.minOperations(**[[4], 7]) == 2
assert my_solution.minOperations(**[[3, 13, 9, 8, 5, 18, 11, 10], 13]) == 2
assert my_solution.minOperations(**[[9, 7, 9, 14, 8, 6], 12]) == 3
assert my_solution.minOperations(**[[13, 9, 10, 16, 11, 8, 1], 17]) == 3
assert my_solution.minOperations(**[[12, 14], 1]) == 2
assert my_solution.minOperations(**[[18, 18], 20]) == 2
assert my_solution.minOperations(**[[3, 5, 1, 1], 19]) == 3
assert my_solution.minOperations(**[[7, 0, 0, 0], 8]) == 4
assert my_solution.minOperations(**[[13, 15, 19, 18, 2, 9, 18, 11, 0, 7], 6]) == 1
assert my_solution.minOperations(**[[9, 15, 19, 15, 10, 15, 14, 0, 2, 5], 20]) == 1
assert my_solution.minOperations(**[[19, 4, 19, 6, 3, 19, 14, 4, 16, 12], 4]) == 0
assert my_solution.minOperations(**[[2, 10, 5, 5, 12, 3, 14, 6, 11, 14], 3]) == 2
assert my_solution.minOperations(**[[11, 20], 10]) == 3
assert my_solution.minOperations(**[[10, 12, 5, 3, 16, 0], 1]) == 2
assert my_solution.minOperations(**[[0, 4, 4, 7, 14, 13], 1]) == 2
assert my_solution.minOperations(**[[16, 2, 20, 13, 15, 20, 13], 16]) == 3
assert my_solution.minOperations(**[[19, 11, 11, 0, 16, 2, 2, 0, 9], 4]) == 3
assert my_solution.minOperations(**[[10, 17, 19, 8, 15], 19]) == 3


# Minimum Number of Operations to Make X and Y Equal

You are given two positive integers `x` and `y`.

In one operation, you can do one of the four following operations:

1. Divide `x` by `11` if `x` is a multiple of `11`.
2. Divide `x` by `5` if `x` is a multiple of `5`.
3. Decrement `x` by `1`.
4. Increment `x` by `1`.

Return *the **minimum** number of operations required to make*  `x` *and* `y` equal.

**Example 1:**

```

**Input:** x = 26, y = 1
**Output:** 3
**Explanation:** We can make 26 equal to 1 by applying the following operations:
1. Decrement x by 1
2. Divide x by 5
3. Divide x by 5
It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.

```

**Example 2:**

```

**Input:** x = 54, y = 2
**Output:** 4
**Explanation:** We can make 54 equal to 2 by applying the following operations:
1. Increment x by 1
2. Divide x by 11
3. Divide x by 5
4. Increment x by 1
It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.

```

**Example 3:**

```

**Input:** x = 25, y = 30
**Output:** 5
**Explanation:** We can make 25 equal to 30 by applying the following operations:
1. Increment x by 1
2. Increment x by 1
3. Increment x by 1
4. Increment x by 1
5. Increment x by 1
It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.

```

**Constraints:**

* `1 <= x, y <= 104`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
```

my_solution = Solution()

assert my_solution.minimumOperationsToMakeEqual(**[26, 1]) == 3
assert my_solution.minimumOperationsToMakeEqual(**[54, 2]) == 4
assert my_solution.minimumOperationsToMakeEqual(**[25, 30]) == 5
assert my_solution.minimumOperationsToMakeEqual(**[1, 1]) == 0
assert my_solution.minimumOperationsToMakeEqual(**[1, 2]) == 1
assert my_solution.minimumOperationsToMakeEqual(**[1, 3]) == 2
assert my_solution.minimumOperationsToMakeEqual(**[1, 4]) == 3
assert my_solution.minimumOperationsToMakeEqual(**[1, 5]) == 4
assert my_solution.minimumOperationsToMakeEqual(**[1, 6]) == 5
assert my_solution.minimumOperationsToMakeEqual(**[1, 7]) == 6
assert my_solution.minimumOperationsToMakeEqual(**[1, 8]) == 7
assert my_solution.minimumOperationsToMakeEqual(**[1, 9]) == 8
assert my_solution.minimumOperationsToMakeEqual(**[1, 10]) == 9
assert my_solution.minimumOperationsToMakeEqual(**[1, 11]) == 10
assert my_solution.minimumOperationsToMakeEqual(**[1, 12]) == 11
assert my_solution.minimumOperationsToMakeEqual(**[1, 13]) == 12
assert my_solution.minimumOperationsToMakeEqual(**[1, 14]) == 13
assert my_solution.minimumOperationsToMakeEqual(**[1, 15]) == 14
assert my_solution.minimumOperationsToMakeEqual(**[1, 16]) == 15
assert my_solution.minimumOperationsToMakeEqual(**[1, 17]) == 16


# Count the Number of Powerful Integers

You are given three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer.

A **positive** integer `x` is called **powerful** if it ends with `s` (in other words, `s` is a **suffix** of `x`) and each digit in `x` is at most `limit`.

Return *the **total** number of powerful integers in the range* `[start..finish]`.

A string `x` is a suffix of a string `y` if and only if `x` is a substring of `y` that starts from some index (**including** `0`) in `y` and extends to the index `y.length - 1`. For example, `25` is a suffix of `5125` whereas `512` is not.

**Example 1:**

```

**Input:** start = 1, finish = 6000, limit = 4, s = "124"
**Output:** 5
**Explanation:** The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
It can be shown that there are only 5 powerful integers in this range.

```

**Example 2:**

```

**Input:** start = 15, finish = 215, limit = 6, s = "10"
**Output:** 2
**Explanation:** The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
It can be shown that there are only 2 powerful integers in this range.

```

**Example 3:**

```

**Input:** start = 1000, finish = 2000, limit = 4, s = "3000"
**Output:** 0
**Explanation:** All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.

```

**Constraints:**

* `1 <= start <= finish <= 1015`
* `1 <= limit <= 9`
* `1 <= s.length <= floor(log10(finish)) + 1`
* `s` only consists of numeric digits which are at most `limit`.
* `s` does not have leading zeros.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
```

my_solution = Solution()

assert my_solution.numberOfPowerfulInt(**[1, 6000, 4, '124']) == 5
assert my_solution.numberOfPowerfulInt(**[15, 215, 6, '10']) == 2
assert my_solution.numberOfPowerfulInt(**[1000, 2000, 4, '3000']) == 0
assert my_solution.numberOfPowerfulInt(**[141, 148, 9, '9']) == 0
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '17']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '27']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '41']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '47']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '61']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '66']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '71']) == 10
assert my_solution.numberOfPowerfulInt(**[1, 971, 9, '72']) == 9
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '20']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '24']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '32']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '33']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '40']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '41']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '42']) == 8
assert my_solution.numberOfPowerfulInt(**[20, 1159, 5, '43']) == 8


# Check if Bitwise OR Has Trailing Zeros

You are given an array of **positive** integers `nums`.

You have to check if it is possible to select **two or more** elements in the array such that the bitwise `OR` of the selected elements has **at least** one trailing zero in its binary representation.

For example, the binary representation of `5`, which is `"101"`, does not have any trailing zeros, whereas the binary representation of `4`, which is `"100"`, has two trailing zeros.

Return `true` *if it is possible to select two or more elements whose bitwise* `OR` *has trailing zeros, return* `false` *otherwise*.

**Example 1:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** true
**Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.

```

**Example 2:**

```

**Input:** nums = [2,4,8,16]
**Output:** true
**Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).

```

**Example 3:**

```

**Input:** nums = [1,3,5,7,9]
**Output:** false
**Explanation:** There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.

```

**Constraints:**

* `2 <= nums.length <= 100`
* `1 <= nums[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def hasTrailingZeros(self, nums: List[int]) -> bool:
```

my_solution = Solution()

assert my_solution.hasTrailingZeros(**[[1, 2, 3, 4, 5]]) == True
assert my_solution.hasTrailingZeros(**[[2, 4, 8, 16]]) == True
assert my_solution.hasTrailingZeros(**[[1, 3, 5, 7, 9]]) == False
assert my_solution.hasTrailingZeros(**[[1, 2]]) == False
assert my_solution.hasTrailingZeros(**[[1, 3]]) == False
assert my_solution.hasTrailingZeros(**[[1, 7]]) == False
assert my_solution.hasTrailingZeros(**[[2, 2]]) == True
assert my_solution.hasTrailingZeros(**[[3, 3]]) == False
assert my_solution.hasTrailingZeros(**[[3, 4]]) == False
assert my_solution.hasTrailingZeros(**[[3, 9]]) == False
assert my_solution.hasTrailingZeros(**[[4, 3]]) == False
assert my_solution.hasTrailingZeros(**[[4, 8]]) == True
assert my_solution.hasTrailingZeros(**[[4, 32]]) == True
assert my_solution.hasTrailingZeros(**[[5, 6]]) == False
assert my_solution.hasTrailingZeros(**[[6, 2]]) == True
assert my_solution.hasTrailingZeros(**[[6, 8]]) == True
assert my_solution.hasTrailingZeros(**[[7, 9]]) == False
assert my_solution.hasTrailingZeros(**[[7, 10]]) == False
assert my_solution.hasTrailingZeros(**[[8, 2]]) == True
assert my_solution.hasTrailingZeros(**[[8, 4]]) == True


# Find Longest Special Substring That Occurs Thrice I

You are given a string `s` that consists of lowercase English letters.

A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special.

Return *the length of the **longest special substring** of* `s` *which occurs **at least thrice***, *or* `-1` *if no special substring occurs at least thrice*.

A **substring** is a contiguous **non-empty** sequence of characters within a string.

**Example 1:**

```

**Input:** s = "aaaa"
**Output:** 2
**Explanation:** The longest special substring which occurs thrice is "aa": substrings "**aa**aa", "a**aa**a", and "aa**aa**".
It can be shown that the maximum length achievable is 2.

```

**Example 2:**

```

**Input:** s = "abcdef"
**Output:** -1
**Explanation:** There exists no special substring which occurs at least thrice. Hence return -1.

```

**Example 3:**

```

**Input:** s = "abcaba"
**Output:** 1
**Explanation:** The longest special substring which occurs thrice is "a": substrings "**a**bcaba", "abc**a**ba", and "abcab**a**".
It can be shown that the maximum length achievable is 1.

```

**Constraints:**

* `3 <= s.length <= 50`
* `s` consists of only lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumLength(self, s: str) -> int:
```

my_solution = Solution()

assert my_solution.maximumLength(**['aaaa']) == 2
assert my_solution.maximumLength(**['abcdef']) == -1
assert my_solution.maximumLength(**['abcaba']) == 1
assert my_solution.maximumLength(**['abcccccdddd']) == 3
assert my_solution.maximumLength(**['aaa']) == 1
assert my_solution.maximumLength(**['acc']) == -1
assert my_solution.maximumLength(**['cab']) == -1
assert my_solution.maximumLength(**['cad']) == -1
assert my_solution.maximumLength(**['cbc']) == -1
assert my_solution.maximumLength(**['ccc']) == 1
assert my_solution.maximumLength(**['dca']) == -1
assert my_solution.maximumLength(**['ddd']) == 1
assert my_solution.maximumLength(**['fff']) == 1
assert my_solution.maximumLength(**['ggg']) == 1
assert my_solution.maximumLength(**['hhh']) == 1
assert my_solution.maximumLength(**['kkk']) == 1
assert my_solution.maximumLength(**['lll']) == 1
assert my_solution.maximumLength(**['ooo']) == 1
assert my_solution.maximumLength(**['ppp']) == 1
assert my_solution.maximumLength(**['qqq']) == 1


# Find Longest Special Substring That Occurs Thrice II

You are given a string `s` that consists of lowercase English letters.

A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special.

Return *the length of the **longest special substring** of* `s` *which occurs **at least thrice***, *or* `-1` *if no special substring occurs at least thrice*.

A **substring** is a contiguous **non-empty** sequence of characters within a string.

**Example 1:**

```

**Input:** s = "aaaa"
**Output:** 2
**Explanation:** The longest special substring which occurs thrice is "aa": substrings "**aa**aa", "a**aa**a", and "aa**aa**".
It can be shown that the maximum length achievable is 2.

```

**Example 2:**

```

**Input:** s = "abcdef"
**Output:** -1
**Explanation:** There exists no special substring which occurs at least thrice. Hence return -1.

```

**Example 3:**

```

**Input:** s = "abcaba"
**Output:** 1
**Explanation:** The longest special substring which occurs thrice is "a": substrings "**a**bcaba", "abc**a**ba", and "abcab**a**".
It can be shown that the maximum length achievable is 1.

```

**Constraints:**

* `3 <= s.length <= 5 * 105`
* `s` consists of only lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumLength(self, s: str) -> int:
```

my_solution = Solution()

assert my_solution.maximumLength(**['aaaa']) == 2
assert my_solution.maximumLength(**['abcdef']) == -1
assert my_solution.maximumLength(**['abcaba']) == 1
assert my_solution.maximumLength(**['abcccccdddd']) == 3
assert my_solution.maximumLength(**['acd']) == -1
assert my_solution.maximumLength(**['bad']) == -1
assert my_solution.maximumLength(**['bbc']) == -1
assert my_solution.maximumLength(**['ccc']) == 1
assert my_solution.maximumLength(**['cda']) == -1
assert my_solution.maximumLength(**['dab']) == -1
assert my_solution.maximumLength(**['ddd']) == 1
assert my_solution.maximumLength(**['eee']) == 1
assert my_solution.maximumLength(**['fff']) == 1
assert my_solution.maximumLength(**['hhh']) == 1
assert my_solution.maximumLength(**['jjj']) == 1
assert my_solution.maximumLength(**['kkk']) == 1
assert my_solution.maximumLength(**['lll']) == 1
assert my_solution.maximumLength(**['mmm']) == 1
assert my_solution.maximumLength(**['nnn']) == 1
assert my_solution.maximumLength(**['ooo']) == 1


# Palindrome Rearrangement Queries

You are given a **0-indexed** string `s` having an **even** length `n`.

You are also given a **0-indexed** 2D integer array, `queries`, where `queries[i] = [ai, bi, ci, di]`.

For each query `i`, you are allowed to perform the following operations:

* Rearrange the characters within the **substring** `s[ai:bi]`, where `0 <= ai <= bi < n / 2`.
* Rearrange the characters within the **substring** `s[ci:di]`, where `n / 2 <= ci <= di < n`.

For each query, your task is to determine whether it is possible to make `s` a **palindrome** by performing the operations.

Each query is answered **independently** of the others.

Return *a **0-indexed** array* `answer`*, where* `answer[i] == true` *if it is possible to make* `s` *a palindrome by performing operations specified by the* `ith` *query, and* `false` *otherwise.*

* A **substring** is a contiguous sequence of characters within a string.
* `s[x:y]` represents the substring consisting of characters from the index `x` to index `y` in `s`, **both inclusive**.

**Example 1:**

```

**Input:** s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
**Output:** [true,true]
**Explanation:** In this example, there are two queries:
In the first query:
- a0 = 1, b0 = 1, c0 = 3, d0 = 5.
- So, you are allowed to rearrange s[1:1] => abcabc and s[3:5] => abcabc.
- To make s a palindrome, s[3:5] can be rearranged to become => abccba.
- Now, s is a palindrome. So, answer[0] = true.
In the second query:
- a1 = 0, b1 = 2, c1 = 5, d1 = 5.
- So, you are allowed to rearrange s[0:2] => abcabc and s[5:5] => abcabc.
- To make s a palindrome, s[0:2] can be rearranged to become => cbaabc.
- Now, s is a palindrome. So, answer[1] = true.

```

**Example 2:**

```

**Input:** s = "abbcdecbba", queries = [[0,2,7,9]]
**Output:** [false]
**Explanation:** In this example, there is only one query.
a0 = 0, b0 = 2, c0 = 7, d0 = 9.
So, you are allowed to rearrange s[0:2] => abbcdecbba and s[7:9] => abbcdecbba.
It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome.
So, answer[0] = false.
```

**Example 3:**

```

**Input:** s = "acbcab", queries = [[1,2,4,5]]
**Output:** [true]
**Explanation:** In this example, there is only one query.
a0 = 1, b0 = 2, c0 = 4, d0 = 5.
So, you are allowed to rearrange s[1:2] => acbcab and s[4:5] => acbcab.
To make s a palindrome s[1:2] can be rearranged to become abccab.
Then, s[4:5] can be rearranged to become abccba.
Now, s is a palindrome. So, answer[0] = true.
```

**Constraints:**

* `2 <= n == s.length <= 105`
* `1 <= queries.length <= 105`
* `queries[i].length == 4`
* `ai == queries[i][0], bi == queries[i][1]`
* `ci == queries[i][2], di == queries[i][3]`
* `0 <= ai <= bi < n / 2`
* `n / 2 <= ci <= di < n`
* `n` is even.
* `s` consists of only lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
```

my_solution = Solution()

assert my_solution.canMakePalindromeQueries(**['abcabc', [[1, 1, 3, 5], [0, 2, 5, 5]]]) == [True, True]
assert my_solution.canMakePalindromeQueries(**['abbcdecbba', [[0, 2, 7, 9]]]) == [False]
assert my_solution.canMakePalindromeQueries(**['acbcab', [[1, 2, 4, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['bb', [[0, 0, 1, 1]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['dd', [[0, 0, 1, 1]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['bdbd', [[0, 0, 2, 3]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['eeee', [[0, 1, 2, 3]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['bbccbb', [[0, 1, 4, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['bcdbdc', [[1, 2, 3, 3]]]) == [False]
assert my_solution.canMakePalindromeQueries(**['cababc', [[1, 2, 3, 4]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['cbbbbc', [[1, 1, 5, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['cdbdbc', [[1, 2, 3, 3]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['ceacea', [[0, 2, 3, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['daeaed', [[0, 2, 3, 3]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['ddaadd', [[0, 2, 3, 4]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['ddedde', [[0, 2, 4, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['ecbbce', [[0, 1, 3, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['eczecz', [[0, 0, 3, 5]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['mpepem', [[0, 2, 3, 4]]]) == [True]
assert my_solution.canMakePalindromeQueries(**['bccacacb', [[3, 3, 4, 7]]]) == [True]


# Minimum Number Game

You are given a **0-indexed** integer array `nums` of **even** length and there is also an empty array `arr`. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:

* Every round, first Alice will remove the **minimum** element from `nums`, and then Bob does the same.
* Now, first Bob will append the removed element in the array `arr`, and then Alice does the same.
* The game continues until `nums` becomes empty.

Return *the resulting array* `arr`.

**Example 1:**

```

**Input:** nums = [5,4,2,3]
**Output:** [3,2,5,4]
**Explanation:** In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].

```

**Example 2:**

```

**Input:** nums = [2,5]
**Output:** [5,2]
**Explanation:** In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].

```

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
* `nums.length % 2 == 0`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def numberGame(self, nums: List[int]) -> List[int]:
```


# Minimum Cost to Convert String I

You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English letters. You are also given two **0-indexed** character arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of changing the character `original[i]` to the character `changed[i]`.

You start with the string `source`. In one operation, you can pick a character `x` from the string and change it to the character `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`.

Return *the **minimum** cost to convert the string* `source` *to the string* `target` *using **any** number of operations. If it is impossible to convert* `source` *to* `target`, *return* `-1`.

**Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.

**Example 1:**

```

**Input:** source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
**Output:** 28
**Explanation:** To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.

```

**Example 2:**

```

**Input:** source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
**Output:** 12
**Explanation:** To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.

```

**Example 3:**

```

**Input:** source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
**Output:** -1
**Explanation:** It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.

```

**Constraints:**

* `1 <= source.length == target.length <= 105`
* `source`, `target` consist of lowercase English letters.
* `1 <= cost.length == original.length == changed.length <= 2000`
* `original[i]`, `changed[i]` are lowercase English letters.
* `1 <= cost[i] <= 106`
* `original[i] != changed[i]`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
```


# Minimum Cost to Convert String II

You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English characters. You are also given two **0-indexed** string arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of converting the string `original[i]` to the string `changed[i]`.

You start with the string `source`. In one operation, you can pick a **substring** `x` from the string, and change it to `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`. You are allowed to do **any** number of operations, but any pair of operations must satisfy **either** of these two conditions:

* The substrings picked in the operations are `source[a..b]` and `source[c..d]` with either `b < c` **or** `d < a`. In other words, the indices picked in both operations are **disjoint**.
* The substrings picked in the operations are `source[a..b]` and `source[c..d]` with `a == c` **and** `b == d`. In other words, the indices picked in both operations are **identical**.

Return *the **minimum** cost to convert the string* `source` *to the string* `target` *using **any** number of operations*. *If it is impossible to convert* `source` *to* `target`, *return* `-1`.

**Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.

**Example 1:**

```

**Input:** source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
**Output:** 28
**Explanation:** To convert "abcd" to "acbe", do the following operations:
- Change substring source[1..1] from "b" to "c" at a cost of 5.
- Change substring source[2..2] from "c" to "e" at a cost of 1.
- Change substring source[2..2] from "e" to "b" at a cost of 2.
- Change substring source[3..3] from "d" to "e" at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.

```

**Example 2:**

```

**Input:** source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5]
**Output:** 9
**Explanation:** To convert "abcdefgh" to "acdeeghh", do the following operations:
- Change substring source[1..3] from "bcd" to "cde" at a cost of 1.
- Change substring source[5..7] from "fgh" to "thh" at a cost of 3. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation.
- Change substring source[5..7] from "thh" to "ghh" at a cost of 5. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation, and identical with indices picked in the second operation.
The total cost incurred is 1 + 3 + 5 = 9.
It can be shown that this is the minimum possible cost.

```

**Example 3:**

```

**Input:** source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578]
**Output:** -1
**Explanation:** It is impossible to convert "abcdefgh" to "addddddd".
If you select substring source[1..3] as the first operation to change "abcdefgh" to "adddefgh", you cannot select substring source[3..7] as the second operation because it has a common index, 3, with the first operation.
If you select substring source[3..7] as the first operation to change "abcdefgh" to "abcddddd", you cannot select substring source[1..3] as the second operation because it has a common index, 3, with the first operation.

```

**Constraints:**

* `1 <= source.length == target.length <= 1000`
* `source`, `target` consist only of lowercase English characters.
* `1 <= cost.length == original.length == changed.length <= 100`
* `1 <= original[i].length == changed[i].length <= source.length`
* `original[i]`, `changed[i]` consist only of lowercase English characters.
* `original[i] != changed[i]`
* `1 <= cost[i] <= 106`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
```

hpd iyk qzd hpi aic znh cea fug wir kwu yjo rzi a n f q u w x i x s o u

iyk qzd hpi aic znh cea fug wir kwu yjo rzi jdc n f q u w x i x s o u d

shh wbs hup sab csp tel mhq ezp eap fqb iea cej b v g e d x q v g x u m u q z q n p

wbs hup sab csp tel mhq ezp eap fqb iea cej osr v g e d x q v g x u m u q m q n p l

bg xr cc ip vq po ym rh vw lf lo ee qv yr f w i u g a e f s r p j o g i u

xr cc ip vq po ym rh vw lf lo ee qv yr yt w i u g a e f s r p a o g i u p

kdz yyv cde oks fzu hkm dmb arh lix eij ksv t u f w b u v h o b o p z h w t p x y

yyv cde oks fzu hkm dmb arh lix eij ksv qaw u f w b u v h o b o p z a w t p x y o

h e y g q y t n r e i x iktg xwgv ddrp saxt rvdq moiy loln bkgj jjgi vatf

e y g q y t n r e i x g xwgv ddrp saxt rvdq moiy loln bkgj jjgi vatf srwc

bin pwo fwt xwi xal uqt lmp erq kac dgv qgh rei nbx i u b v c q p f q v t n b

pwo fwt xwi xal uqt lmp erq kac dgv qgh rei nbx hjv u b v c q p f q v t n b d

egbx hrbf twne snjd ysrf qzqg rcll ekvz inpr frxs xcww unsw vdug ycvs j v j y n q w a z g b d

hrbf twne snjd ysrf qzqg rcll ekvz inpr frxs xcww unsw vdug ycvs zhfc v j y n q w a z g b d e

j i q u y w d a h s i y w pyj qng lrn nrm tvn fei fpj qlw lrb ufu kll nqp

i q u y w d a h s i y w p qng lrn nrm tvn fei fpj qlw lrb ufu kll nqp qnf

x r a x c w s a n e q p v k o ial qzu owr kyq ukk gpq jdp dus eng btu cbp

r a x c w s a l e q p v k o q qzu owr kyq ukk gpq jdp dus eng btu cbp vqf

gpe owt wyv eba xgp uny ibc usb mzj wdo lyc eof oci p e p u h w i l

owt wyv eba xgp uny ibc usb mzj wdo lyc eof oci cng e p u h w i l x


# Count the Number of Incremovable Subarrays I

You are given a **0-indexed** array of **positive** integers `nums`.

A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing.

Return *the total number of **incremovable** subarrays of* `nums`.

**Note** that an empty array is considered strictly increasing.

A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

```

**Input:** nums = [1,2,3,4]
**Output:** 10
**Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.

```

**Example 2:**

```

**Input:** nums = [6,5,7,8]
**Output:** 7
**Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.

```

**Example 3:**

```

**Input:** nums = [8,7,6,6]
**Output:** 3
**Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.

```

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def incremovableSubarrayCount(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.incremovableSubarrayCount(**[[1, 2, 3, 4]]) == 10
assert my_solution.incremovableSubarrayCount(**[[6, 5, 7, 8]]) == 7
assert my_solution.incremovableSubarrayCount(**[[8, 7, 6, 6]]) == 3
assert my_solution.incremovableSubarrayCount(**[[1]]) == 1
assert my_solution.incremovableSubarrayCount(**[[2]]) == 1
assert my_solution.incremovableSubarrayCount(**[[3]]) == 1
assert my_solution.incremovableSubarrayCount(**[[4]]) == 1
assert my_solution.incremovableSubarrayCount(**[[5]]) == 1
assert my_solution.incremovableSubarrayCount(**[[6]]) == 1
assert my_solution.incremovableSubarrayCount(**[[7]]) == 1
assert my_solution.incremovableSubarrayCount(**[[8]]) == 1
assert my_solution.incremovableSubarrayCount(**[[9]]) == 1
assert my_solution.incremovableSubarrayCount(**[[10]]) == 1
assert my_solution.incremovableSubarrayCount(**[[1, 2]]) == 3
assert my_solution.incremovableSubarrayCount(**[[1, 4]]) == 3
assert my_solution.incremovableSubarrayCount(**[[1, 8]]) == 3
assert my_solution.incremovableSubarrayCount(**[[2, 10]]) == 3
assert my_solution.incremovableSubarrayCount(**[[3, 4]]) == 3
assert my_solution.incremovableSubarrayCount(**[[3, 8]]) == 3
assert my_solution.incremovableSubarrayCount(**[[3, 10]]) == 3


# Find Polygon With the Largest Perimeter

You are given an array of **positive** integers `nums` of length `n`.

A **polygon** is a closed plane figure that has at least `3` sides. The **longest side** of a polygon is **smaller** than the sum of its other sides.

Conversely, if you have `k` (`k >= 3`) **positive** real numbers `a1`, `a2`, `a3`, ..., `ak` where `a1 <= a2 <= a3 <= ... <= ak` **and** `a1 + a2 + a3 + ... + ak-1 > ak`, then there **always** exists a polygon with `k` sides whose lengths are `a1`, `a2`, `a3`, ..., `ak`.

The **perimeter** of a polygon is the sum of lengths of its sides.

Return *the **largest** possible **perimeter** of a **polygon** whose sides can be formed from* `nums`, *or* `-1` *if it is not possible to create a polygon*.

**Example 1:**

```

**Input:** nums = [5,5,5]
**Output:** 15
**Explanation:** The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.

```

**Example 2:**

```

**Input:** nums = [1,12,1,2,5,50,3]
**Output:** 12
**Explanation:** The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
It can be shown that the largest possible perimeter is 12.

```

**Example 3:**

```

**Input:** nums = [5,5,50]
**Output:** -1
**Explanation:** There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.

```

**Constraints:**

* `3 <= n <= 105`
* `1 <= nums[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def largestPerimeter(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.largestPerimeter(**[[5, 5, 5]]) == 15
assert my_solution.largestPerimeter(**[[1, 12, 1, 2, 5, 50, 3]]) == 12
assert my_solution.largestPerimeter(**[[5, 5, 50]]) == -1
assert my_solution.largestPerimeter(**[[1, 1, 1]]) == 3
assert my_solution.largestPerimeter(**[[1, 1, 2]]) == -1
assert my_solution.largestPerimeter(**[[1, 1, 3]]) == -1
assert my_solution.largestPerimeter(**[[1, 1, 4]]) == -1
assert my_solution.largestPerimeter(**[[1, 1, 5]]) == -1
assert my_solution.largestPerimeter(**[[1, 2, 1]]) == -1
assert my_solution.largestPerimeter(**[[1, 2, 2]]) == 5
assert my_solution.largestPerimeter(**[[1, 2, 3]]) == -1
assert my_solution.largestPerimeter(**[[1, 2, 4]]) == -1
assert my_solution.largestPerimeter(**[[1, 2, 5]]) == -1
assert my_solution.largestPerimeter(**[[1, 3, 1]]) == -1
assert my_solution.largestPerimeter(**[[1, 3, 2]]) == -1
assert my_solution.largestPerimeter(**[[1, 3, 3]]) == 7
assert my_solution.largestPerimeter(**[[1, 3, 4]]) == -1
assert my_solution.largestPerimeter(**[[1, 3, 5]]) == -1
assert my_solution.largestPerimeter(**[[1, 4, 1]]) == -1
assert my_solution.largestPerimeter(**[[1, 4, 2]]) == -1


# Count the Number of Incremovable Subarrays II

You are given a **0-indexed** array of **positive** integers `nums`.

A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing.

Return *the total number of **incremovable** subarrays of* `nums`.

**Note** that an empty array is considered strictly increasing.

A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

```

**Input:** nums = [1,2,3,4]
**Output:** 10
**Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.

```

**Example 2:**

```

**Input:** nums = [6,5,7,8]
**Output:** 7
**Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.

```

**Example 3:**

```

**Input:** nums = [8,7,6,6]
**Output:** 3
**Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.

```

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def incremovableSubarrayCount(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.incremovableSubarrayCount(**[[1, 2, 3, 4]]) == 10
assert my_solution.incremovableSubarrayCount(**[[6, 5, 7, 8]]) == 7
assert my_solution.incremovableSubarrayCount(**[[8, 7, 6, 6]]) == 3
assert my_solution.incremovableSubarrayCount(**[[1]]) == 1
assert my_solution.incremovableSubarrayCount(**[[2]]) == 1
assert my_solution.incremovableSubarrayCount(**[[3]]) == 1
assert my_solution.incremovableSubarrayCount(**[[4]]) == 1
assert my_solution.incremovableSubarrayCount(**[[5]]) == 1
assert my_solution.incremovableSubarrayCount(**[[6]]) == 1
assert my_solution.incremovableSubarrayCount(**[[7]]) == 1
assert my_solution.incremovableSubarrayCount(**[[8]]) == 1
assert my_solution.incremovableSubarrayCount(**[[9]]) == 1
assert my_solution.incremovableSubarrayCount(**[[10]]) == 1
assert my_solution.incremovableSubarrayCount(**[[4, 10]]) == 3
assert my_solution.incremovableSubarrayCount(**[[7, 3]]) == 3
assert my_solution.incremovableSubarrayCount(**[[8, 5]]) == 3
assert my_solution.incremovableSubarrayCount(**[[8, 10]]) == 3
assert my_solution.incremovableSubarrayCount(**[[9, 9]]) == 3
assert my_solution.incremovableSubarrayCount(**[[5, 5, 6]]) == 5
assert my_solution.incremovableSubarrayCount(**[[6, 7, 5]]) == 4


# Find Missing and Repeated Values

You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range `[1, n2]`. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`.

Return *a **0-indexed** integer array* `ans` *of size* `2` *where* `ans[0]` *equals to* `a` *and* `ans[1]` *equals to* `b`*.*

**Example 1:**

```

**Input:** grid = [[1,3],[2,2]]
**Output:** [2,4]
**Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4].

```

**Example 2:**

```

**Input:** grid = [[9,1,7],[8,9,2],[3,4,6]]
**Output:** [9,5]
**Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5].

```

**Constraints:**

* `2 <= n == grid.length == grid[i].length <= 50`
* `1 <= grid[i][j] <= n * n`
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members.
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members.
* For all `x` that `1 <= x <= n * n` except two of them there is exatly one pair of `i, j` that `0 <= i, j <= n - 1` and `grid[i][j] == x`.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
```

my_solution = Solution()

assert my_solution.findMissingAndRepeatedValues(**[[[1, 3], [2, 2]]]) == [2, 4]
assert my_solution.findMissingAndRepeatedValues(**[[[9, 1, 7], [8, 9, 2], [3, 4, 6]]]) == [9, 5]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 1], [3, 2]]]) == [1, 4]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 1], [3, 4]]]) == [1, 2]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 2], [1, 3]]]) == [1, 4]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 2], [1, 4]]]) == [1, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 2], [3, 3]]]) == [3, 4]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 2], [4, 1]]]) == [1, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 2], [4, 2]]]) == [2, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 2], [4, 4]]]) == [4, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 4], [1, 3]]]) == [1, 2]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 4], [2, 1]]]) == [1, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 4], [3, 1]]]) == [1, 2]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 4], [3, 4]]]) == [4, 2]
assert my_solution.findMissingAndRepeatedValues(**[[[1, 4], [4, 2]]]) == [4, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[2, 1], [4, 2]]]) == [2, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[2, 1], [4, 4]]]) == [4, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[2, 2], [3, 4]]]) == [2, 1]
assert my_solution.findMissingAndRepeatedValues(**[[[2, 2], [4, 1]]]) == [2, 3]
assert my_solution.findMissingAndRepeatedValues(**[[[2, 3], [2, 1]]]) == [2, 4]


# Divide Array Into Arrays With Max Difference

You are given an integer array `nums` of size `n` and a positive integer `k`.

Divide the array into one or more arrays of size `3` satisfying the following conditions:

* **Each** element of `nums` should be in **exactly** one array.
* The difference between **any** two elements in one array is less than or equal to `k`.

Return *a* **2D** *array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them.*

**Example 1:**

```

**Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2
**Output:** [[1,1,3],[3,4,5],[7,8,9]]
**Explanation:** We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
The difference between any two elements in each array is less than or equal to 2.
Note that the order of elements is not important.

```

**Example 2:**

```

**Input:** nums = [1,3,3,2,7,3], k = 3
**Output:** []
**Explanation:** It is not possible to divide the array satisfying all the conditions.

```

**Constraints:**

* `n == nums.length`
* `1 <= n <= 105`
* `n` is a multiple of `3`.
* `1 <= nums[i] <= 105`
* `1 <= k <= 105`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
```


# Minimum Cost to Make Array Equalindromic

You are given a **0-indexed** integer array `nums` having length `n`.

You are allowed to perform a special move **any** number of times (**including zero**) on `nums`. In one **special** **move** you perform the following steps **in order**:

* Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`.
* Add `|nums[i] - x|` to the total cost.
* Change the value of `nums[i]` to `x`.

A **palindromic number** is a positive integer that remains the same when its digits are reversed. For example, `121`, `2552` and `65756` are palindromic numbers whereas `24`, `46`, `235` are not palindromic numbers.

An array is considered **equalindromic** if all the elements in the array are equal to an integer `y`, where `y` is a **palindromic number** less than `109`.

Return *an integer denoting the **minimum** possible total cost to make* `nums` ***equalindromic** by performing any number of special moves.*

**Example 1:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** 6
**Explanation:** We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.

```

**Example 2:**

```

**Input:** nums = [10,12,13,14,15]
**Output:** 11
**Explanation:** We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.

```

**Example 3:**

```

**Input:** nums = [22,33,22,33,22]
**Output:** 22
**Explanation:** We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.

```

**Constraints:**

* `1 <= n <= 105`
* `1 <= nums[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumCost(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minimumCost(**[[1, 2, 3, 4, 5]]) == 6
assert my_solution.minimumCost(**[[10, 12, 13, 14, 15]]) == 11
assert my_solution.minimumCost(**[[22, 33, 22, 33, 22]]) == 22
assert my_solution.minimumCost(**[[1]]) == 0
assert my_solution.minimumCost(**[[2]]) == 0
assert my_solution.minimumCost(**[[3]]) == 0
assert my_solution.minimumCost(**[[4]]) == 0
assert my_solution.minimumCost(**[[5]]) == 0
assert my_solution.minimumCost(**[[2, 1]]) == 1
assert my_solution.minimumCost(**[[3, 1]]) == 2
assert my_solution.minimumCost(**[[3, 2]]) == 1
assert my_solution.minimumCost(**[[4, 1]]) == 3
assert my_solution.minimumCost(**[[4, 2]]) == 2
assert my_solution.minimumCost(**[[4, 3]]) == 1
assert my_solution.minimumCost(**[[5, 1]]) == 4
assert my_solution.minimumCost(**[[5, 2]]) == 3
assert my_solution.minimumCost(**[[5, 3]]) == 2
assert my_solution.minimumCost(**[[5, 4]]) == 1
assert my_solution.minimumCost(**[[3, 2, 1]]) == 2
assert my_solution.minimumCost(**[[4, 2, 1]]) == 3


# Apply Operations to Maximize Frequency Score

You are given a **0-indexed** integer array `nums` and an integer `k`.

You can perform the following operation on the array **at most** `k` times:

* Choose any index `i` from the array and **increase** or **decrease** `nums[i]` by `1`.

The score of the final array is the **frequency** of the most frequent element in the array.

Return *the **maximum** score you can achieve*.

The frequency of an element is the number of occurences of that element in the array.

**Example 1:**

```

**Input:** nums = [1,2,6,4], k = 3
**Output:** 3
**Explanation:** We can do the following operations on the array:
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
The element 2 is the most frequent in the final array so our score is 3.
It can be shown that we cannot achieve a better score.

```

**Example 2:**

```

**Input:** nums = [1,4,4,2,4], k = 0
**Output:** 3
**Explanation:** We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.

```

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`
* `0 <= k <= 1014`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maxFrequencyScore(self, nums: List[int], k: int) -> int:
```


# Count Tested Devices After Test Operations

You are given a **0-indexed** integer array `batteryPercentages` having length `n`, denoting the battery percentages of `n` **0-indexed** devices.

Your task is to test each device `i` **in order** from `0` to `n - 1`, by performing the following test operations:

* If `batteryPercentages[i]` is **greater** than `0`:
	+ **Increment** the count of tested devices.
	+ **Decrease** the battery percentage of all devices with indices `j` in the range `[i + 1, n - 1]` by `1`, ensuring their battery percentage **never goes below** `0`, i.e, `batteryPercentages[j] = max(0, batteryPercentages[j] - 1)`.
	+ Move to the next device.
* Otherwise, move to the next device without performing any test.

Return *an integer denoting the number of devices that will be tested after performing the test operations in order.*

**Example 1:**

```

**Input:** batteryPercentages = [1,1,2,1,3]
**Output:** 3
**Explanation:** Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
So, the answer is 3.

```

**Example 2:**

```

**Input:** batteryPercentages = [0,1,2]
**Output:** 2
**Explanation:** Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
So, the answer is 2.

```

**Constraints:**

* `1 <= n == batteryPercentages.length <= 100`
* `0 <= batteryPercentages[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def countTestedDevices(self, batteryPercentages: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.countTestedDevices(**[[1, 1, 2, 1, 3]]) == 3
assert my_solution.countTestedDevices(**[[0, 1, 2]]) == 2
assert my_solution.countTestedDevices(**[[0]]) == 0
assert my_solution.countTestedDevices(**[[1]]) == 1
assert my_solution.countTestedDevices(**[[0, 0]]) == 0
assert my_solution.countTestedDevices(**[[0, 1]]) == 1
assert my_solution.countTestedDevices(**[[0, 2]]) == 1
assert my_solution.countTestedDevices(**[[1, 0]]) == 1
assert my_solution.countTestedDevices(**[[1, 2]]) == 2
assert my_solution.countTestedDevices(**[[2, 1]]) == 1
assert my_solution.countTestedDevices(**[[2, 2]]) == 2
assert my_solution.countTestedDevices(**[[0, 0, 1]]) == 1
assert my_solution.countTestedDevices(**[[0, 0, 2]]) == 1
assert my_solution.countTestedDevices(**[[1, 1, 0]]) == 1
assert my_solution.countTestedDevices(**[[1, 2, 0]]) == 2
assert my_solution.countTestedDevices(**[[1, 3, 1]]) == 2
assert my_solution.countTestedDevices(**[[2, 0, 1]]) == 1
assert my_solution.countTestedDevices(**[[2, 2, 0]]) == 2
assert my_solution.countTestedDevices(**[[2, 2, 2]]) == 2
assert my_solution.countTestedDevices(**[[3, 0, 3]]) == 2


# Double Modular Exponentiation

You are given a **0-indexed** 2D array `variables` where `variables[i] = [ai, bi, ci, mi]`, and an integer `target`.

An index `i` is **good** if the following formula holds:

* `0 <= i < variables.length`
* `((aibi % 10)ci) % mi == target`

Return *an array consisting of **good** indices in **any order***.

**Example 1:**

```

**Input:** variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
**Output:** [0,2]
**Explanation:** For each index i in the variables array:
1) For the index 0, variables[0] = [2,3,3,10], (23 % 10)3 % 10 = 2.
2) For the index 1, variables[1] = [3,3,3,1], (33 % 10)3 % 1 = 0.
3) For the index 2, variables[2] = [6,1,1,4], (61 % 10)1 % 4 = 2.
Therefore we return [0,2] as the answer.

```

**Example 2:**

```

**Input:** variables = [[39,3,1000,1000]], target = 17
**Output:** []
**Explanation:** For each index i in the variables array:
1) For the index 0, variables[0] = [39,3,1000,1000], (393 % 10)1000 % 1000 = 1.
Therefore we return [] as the answer.

```

**Constraints:**

* `1 <= variables.length <= 100`
* `variables[i] == [ai, bi, ci, mi]`
* `1 <= ai, bi, ci, mi <= 103`
* `0 <= target <= 103`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]:
```


# Count Subarrays Where Max Element Appears at Least K Times

You are given an integer array `nums` and a **positive** integer `k`.

Return *the number of subarrays where the **maximum** element of* `nums` *appears **at least*** `k` *times in that subarray.*

A **subarray** is a contiguous sequence of elements within an array.

**Example 1:**

```

**Input:** nums = [1,3,2,3,3], k = 2
**Output:** 6
**Explanation:** The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].

```

**Example 2:**

```

**Input:** nums = [1,4,2,1], k = 3
**Output:** 0
**Explanation:** No subarray contains the element 4 at least 3 times.

```

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 106`
* `1 <= k <= 105`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
```


# Count the Number of Good Partitions

You are given a **0-indexed** array `nums` consisting of **positive** integers.

A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number.

Return *the **total number** of good partitions of* `nums`.

Since the answer may be large, return it **modulo** `109 + 7`.

**Example 1:**

```

**Input:** nums = [1,2,3,4]
**Output:** 8
**Explanation:** The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).

```

**Example 2:**

```

**Input:** nums = [1,1,1,1]
**Output:** 1
**Explanation:** The only possible good partition is: ([1,1,1,1]).

```

**Example 3:**

```

**Input:** nums = [1,2,1,3]
**Output:** 2
**Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).

```

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def numberOfGoodPartitions(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.numberOfGoodPartitions(**[[1, 2, 3, 4]]) == 8
assert my_solution.numberOfGoodPartitions(**[[1, 1, 1, 1]]) == 1
assert my_solution.numberOfGoodPartitions(**[[1, 2, 1, 3]]) == 2
assert my_solution.numberOfGoodPartitions(**[[1]]) == 1
assert my_solution.numberOfGoodPartitions(**[[100000]]) == 1
assert my_solution.numberOfGoodPartitions(**[[1000000000]]) == 1
assert my_solution.numberOfGoodPartitions(**[[1, 1, 1, 3, 2]]) == 4
assert my_solution.numberOfGoodPartitions(**[[1, 1, 1, 9, 7]]) == 4
assert my_solution.numberOfGoodPartitions(**[[1, 1, 5, 9, 2]]) == 8
assert my_solution.numberOfGoodPartitions(**[[1, 4, 1, 7, 5]]) == 4
assert my_solution.numberOfGoodPartitions(**[[1, 5, 1, 5, 6]]) == 2
assert my_solution.numberOfGoodPartitions(**[[1, 5, 1, 10, 8]]) == 4
assert my_solution.numberOfGoodPartitions(**[[1, 6, 8, 1, 5]]) == 2
assert my_solution.numberOfGoodPartitions(**[[1, 6, 9, 4, 10]]) == 16
assert my_solution.numberOfGoodPartitions(**[[1, 7, 1, 6, 8]]) == 4
assert my_solution.numberOfGoodPartitions(**[[1, 9, 1, 1, 7]]) == 2
assert my_solution.numberOfGoodPartitions(**[[2, 1, 6, 7, 5]]) == 16
assert my_solution.numberOfGoodPartitions(**[[2, 3, 2, 6, 9]]) == 4
assert my_solution.numberOfGoodPartitions(**[[2, 3, 2, 8, 8]]) == 2
assert my_solution.numberOfGoodPartitions(**[[2, 3, 9, 2, 6]]) == 2


# Find Common Elements Between Two Arrays

You are given two **0-indexed** integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively.

Consider calculating the following values:

* The number of indices `i` such that `0 <= i < n` and `nums1[i]` occurs **at least** once in `nums2`.
* The number of indices `i` such that `0 <= i < m` and `nums2[i]` occurs **at least** once in `nums1`.

Return *an integer array* `answer` *of size* `2` *containing the two values **in the above order***.

**Example 1:**

```

**Input:** nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
**Output:** [3,4]
**Explanation:** We calculate the values as follows:
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.

```

**Example 2:**

```

**Input:** nums1 = [3,4,2,3], nums2 = [1,5]
**Output:** [0,0]
**Explanation:** There are no common elements between the two arrays, so the two values will be 0.

```

**Constraints:**

* `n == nums1.length`
* `m == nums2.length`
* `1 <= n, m <= 100`
* `1 <= nums1[i], nums2[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
```


# Remove Adjacent Almost-Equal Characters

You are given a **0-indexed** string `word`.

In one operation, you can pick any index `i` of `word` and change `word[i]` to any lowercase English letter.

Return *the **minimum** number of operations needed to remove all adjacent **almost-equal** characters from* `word`.

Two characters `a` and `b` are **almost-equal** if `a == b` or `a` and `b` are adjacent in the alphabet.

**Example 1:**

```

**Input:** word = "aaaaa"
**Output:** 2
**Explanation:** We can change word into "a**c**a**c**a" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.

```

**Example 2:**

```

**Input:** word = "abddez"
**Output:** 2
**Explanation:** We can change word into "**y**bd**o**ez" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
```

**Example 3:**

```

**Input:** word = "zyxyxyz"
**Output:** 3
**Explanation:** We can change word into "z**a**x**a**x**a**z" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.

```

**Constraints:**

* `1 <= word.length <= 100`
* `word` consists only of lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def removeAlmostEqualCharacters(self, word: str) -> int:
```

my_solution = Solution()

assert my_solution.removeAlmostEqualCharacters(**['aaaaa']) == 2
assert my_solution.removeAlmostEqualCharacters(**['abddez']) == 2
assert my_solution.removeAlmostEqualCharacters(**['zyxyxyz']) == 3
assert my_solution.removeAlmostEqualCharacters(**['a']) == 0
assert my_solution.removeAlmostEqualCharacters(**['b']) == 0
assert my_solution.removeAlmostEqualCharacters(**['c']) == 0
assert my_solution.removeAlmostEqualCharacters(**['aa']) == 1
assert my_solution.removeAlmostEqualCharacters(**['ab']) == 1
assert my_solution.removeAlmostEqualCharacters(**['ac']) == 0
assert my_solution.removeAlmostEqualCharacters(**['ba']) == 1
assert my_solution.removeAlmostEqualCharacters(**['bb']) == 1
assert my_solution.removeAlmostEqualCharacters(**['bc']) == 1
assert my_solution.removeAlmostEqualCharacters(**['ca']) == 0
assert my_solution.removeAlmostEqualCharacters(**['cb']) == 1
assert my_solution.removeAlmostEqualCharacters(**['cc']) == 1
assert my_solution.removeAlmostEqualCharacters(**['aaa']) == 1
assert my_solution.removeAlmostEqualCharacters(**['aab']) == 1
assert my_solution.removeAlmostEqualCharacters(**['aac']) == 1
assert my_solution.removeAlmostEqualCharacters(**['aba']) == 1
assert my_solution.removeAlmostEqualCharacters(**['abb']) == 1