# Find the Losers of the Circular Game

There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend.

The rules of the game are as follows:

`1st` friend receives the ball.

* After that, `1st` friend passes it to the friend who is `k` steps away from them in the **clockwise** direction.
* After that, the friend who receives the ball should pass it to the friend who is `2 * k` steps away from them in the **clockwise** direction.
* After that, the friend who receives the ball should pass it to the friend who is `3 * k` steps away from them in the **clockwise** direction, and so on and so forth.

In other words, on the `ith` turn, the friend holding the ball should pass it to the friend who is `i * k` steps away from them in the **clockwise** direction.

The game is finished when some friend receives the ball for the second time.

The **losers** of the game are friends who did not receive the ball in the entire game.

Given the number of friends, `n`, and an integer `k`, return *the array answer, which contains the losers of the game in the **ascending** order*.

**Example 1:**

```

**Input:** n = 5, k = 2
**Output:** [4,5]
**Explanation:** The game goes as follows:
1) Start at 1st friend and pass the ball to the friend who is 2 steps away from them - 3rd friend.
2) 3rd friend passes the ball to the friend who is 4 steps away from them - 2nd friend.
3) 2nd friend passes the ball to the friend who is 6 steps away from them  - 3rd friend.
4) The game ends as 3rd friend receives the ball for the second time.

```

**Example 2:**

```

**Input:** n = 4, k = 4
**Output:** [2,3,4]
**Explanation:** The game goes as follows:
1) Start at the 1st friend and pass the ball to the friend who is 4 steps away from them - 1st friend.
2) The game ends as 1st friend receives the ball for the second time.

```

**Constraints:**

* `1 <= k <= n <= 50`

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.circularGameLosers(**[5, 2]) == [4, 5]
assert my_solution.circularGameLosers(**[4, 4]) == [2, 3, 4]
assert my_solution.circularGameLosers(**[1, 1]) == []
assert my_solution.circularGameLosers(**[2, 1]) == []
assert my_solution.circularGameLosers(**[2, 2]) == [2]
assert my_solution.circularGameLosers(**[3, 1]) == [3]
assert my_solution.circularGameLosers(**[3, 2]) == [2]
assert my_solution.circularGameLosers(**[3, 3]) == [2, 3]
assert my_solution.circularGameLosers(**[4, 1]) == []
assert my_solution.circularGameLosers(**[4, 2]) == [2, 4]
assert my_solution.circularGameLosers(**[4, 3]) == []
assert my_solution.circularGameLosers(**[5, 1]) == [3, 5]
assert my_solution.circularGameLosers(**[5, 3]) == [2, 3]
assert my_solution.circularGameLosers(**[5, 4]) == [2, 4]
assert my_solution.circularGameLosers(**[5, 5]) == [2, 3, 4, 5]
assert my_solution.circularGameLosers(**[6, 1]) == [3, 5, 6]
assert my_solution.circularGameLosers(**[6, 2]) == [2, 4, 5, 6]
assert my_solution.circularGameLosers(**[6, 3]) == [2, 3, 5, 6]
assert my_solution.circularGameLosers(**[6, 4]) == [2, 3, 4, 6]
assert my_solution.circularGameLosers(**[6, 5]) == [2, 3, 5]


# Neighboring Bitwise XOR

A **0-indexed** array `derived` with length `n` is derived by computing the **bitwise XOR** (⊕) of adjacent values in a **binary array** `original` of length `n`.

Specifically, for each index `i` in the range `[0, n - 1]`:

* If `i = n - 1`, then `derived[i] = original[i] ⊕ original[0]`.
* Otherwise, `derived[i] = original[i] ⊕ original[i + 1]`.

Given an array `derived`, your task is to determine whether there exists a **valid binary array** `original` that could have formed `derived`.

Return ***true** if such an array exists or **false** otherwise.*

* A binary array is an array containing only **0's** and **1's**

**Example 1:**

```

**Input:** derived = [1,1,0]
**Output:** true
**Explanation:** A valid original array that gives derived is [0,1,0].
derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1
derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1
derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0

```

**Example 2:**

```

**Input:** derived = [1,1]
**Output:** true
**Explanation:** A valid original array that gives derived is [0,1].
derived[0] = original[0] ⊕ original[1] = 1
derived[1] = original[1] ⊕ original[0] = 1

```

**Example 3:**

```

**Input:** derived = [1,0]
**Output:** false
**Explanation:** There is no valid original array that gives derived.

```

**Constraints:**

* `n == derived.length`
* `1 <= n <= 105`
* The values in `derived` are either **0's** or **1's**

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.doesValidArrayExist(**[[1, 1, 0]]) == True
assert my_solution.doesValidArrayExist(**[[1, 1]]) == True
assert my_solution.doesValidArrayExist(**[[1, 0]]) == False
assert my_solution.doesValidArrayExist(**[[0]]) == True
assert my_solution.doesValidArrayExist(**[[1]]) == False
assert my_solution.doesValidArrayExist(**[[0, 0]]) == True
assert my_solution.doesValidArrayExist(**[[0, 1]]) == False
assert my_solution.doesValidArrayExist(**[[0, 0, 0]]) == True
assert my_solution.doesValidArrayExist(**[[0, 0, 1]]) == False
assert my_solution.doesValidArrayExist(**[[0, 1, 0]]) == False
assert my_solution.doesValidArrayExist(**[[0, 1, 1]]) == True
assert my_solution.doesValidArrayExist(**[[1, 0, 0]]) == False
assert my_solution.doesValidArrayExist(**[[1, 0, 1]]) == True
assert my_solution.doesValidArrayExist(**[[1, 1, 1]]) == False
assert my_solution.doesValidArrayExist(**[[0, 0, 0, 0]]) == True
assert my_solution.doesValidArrayExist(**[[0, 0, 0, 1]]) == False
assert my_solution.doesValidArrayExist(**[[0, 0, 1, 0]]) == False
assert my_solution.doesValidArrayExist(**[[0, 0, 1, 1]]) == True
assert my_solution.doesValidArrayExist(**[[0, 1, 0, 0]]) == False
assert my_solution.doesValidArrayExist(**[[0, 1, 0, 1]]) == True


# Number of Senior Citizens

You are given a **0-indexed** array of strings `details`. Each element of `details` provides information about a given passenger compressed into a string of length `15`. The system is such that:

* The first ten characters consist of the phone number of passengers.
* The next character denotes the gender of the person.
* The following two characters are used to indicate the age of the person.
* The last two characters determine the seat allotted to that person.

Return *the number of passengers who are **strictly** **more than 60 years old**.*

**Example 1:**

```

**Input:** details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
**Output:** 2
**Explanation:** The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.

```

**Example 2:**

```

**Input:** details = ["1313579440F2036","2921522980M5644"]
**Output:** 0
**Explanation:** None of the passengers are older than 60.

```

**Constraints:**

* `1 <= details.length <= 100`
* `details[i].length == 15`
* `details[i] consists of digits from '0' to '9'.`
* `details[i][10] is either 'M' or 'F' or 'O'.`
* The phone numbers and seat numbers of the passengers are distinct.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def countSeniors(self, details: List[str]) -> int:
```


# Sum in a Matrix

You are given a **0-indexed** 2D integer array `nums`. Initially, your score is `0`. Perform the following operations until the matrix becomes empty:

1. From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen.
2. Identify the highest number amongst all those removed in step 1. Add that number to your **score**.

Return *the final **score**.*

**Example 1:**

```

**Input:** nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
**Output:** 15
**Explanation:** In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.

```

**Example 2:**

```

**Input:** nums = [[1]]
**Output:** 1
**Explanation:** We remove 1 and add it to the answer. We return 1.
```

**Constraints:**

* `1 <= nums.length <= 300`
* `1 <= nums[i].length <= 500`
* `0 <= nums[i][j] <= 103`

Please make sure your answer follows the type signature below:

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


# Maximum OR

You are given a **0-indexed** integer array `nums` of length `n` and an integer `k`. In an operation, you can choose an element and multiply it by `2`.

Return *the maximum possible value of* `nums[0] | nums[1] | ... | nums[n - 1]` *that can be obtained after applying the operation on nums at most* `k` *times*.

Note that `a | b` denotes the **bitwise or** between two integers `a` and `b`.

**Example 1:**

```

**Input:** nums = [12,9], k = 1
**Output:** 30
**Explanation:** If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.

```

**Example 2:**

```

**Input:** nums = [8,1,2], k = 2
**Output:** 35
**Explanation:** If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Power of Heroes

You are given a **0-indexed** integer array `nums` representing the strength of some heroes. The **power** of a group of heroes is defined as follows:

* Let `i0`, `i1`, ... ,`ik` be the indices of the heroes in a group. Then, the power of this group is `max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik])`.

Return *the sum of the **power** of all **non-empty** groups of heroes possible.* Since the sum could be very large, return it **modulo** `109 + 7`.

**Example 1:**

```

**Input:** nums = [2,1,4]
**Output:** 141
**Explanation:**
1st group: [2] has power = 22 * 2 = 8.
2nd group: [1] has power = 12 * 1 = 1.
3rd group: [4] has power = 42 * 4 = 64.
4th group: [2,1] has power = 22 * 1 = 4.
5th group: [2,4] has power = 42 * 2 = 32.
6th group: [1,4] has power = 42 * 1 = 16.
7th group: [2,1,4] has power = 42 * 1 = 16.
The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.

```

**Example 2:**

```

**Input:** nums = [1,1,1]
**Output:** 7
**Explanation:** A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.sumOfPower(**[[2, 1, 4]]) == 141
assert my_solution.sumOfPower(**[[1, 1, 1]]) == 7
assert my_solution.sumOfPower(**[[4, 4]]) == 192
assert my_solution.sumOfPower(**[[8, 4]]) == 832
assert my_solution.sumOfPower(**[[5, 3]]) == 227
assert my_solution.sumOfPower(**[[6, 1, 4]]) == 513
assert my_solution.sumOfPower(**[[3, 2, 3]]) == 143
assert my_solution.sumOfPower(**[[7, 4, 9]]) == 2547
assert my_solution.sumOfPower(**[[4, 6]]) == 424
assert my_solution.sumOfPower(**[[2, 6]]) == 296
assert my_solution.sumOfPower(**[[9, 8, 5]]) == 3144
assert my_solution.sumOfPower(**[[3, 1, 3]]) == 109
assert my_solution.sumOfPower(**[[8, 3, 9]]) == 2594
assert my_solution.sumOfPower(**[[8, 2]]) == 648
assert my_solution.sumOfPower(**[[3, 5]]) == 227
assert my_solution.sumOfPower(**[[8, 4, 7]]) == 2075
assert my_solution.sumOfPower(**[[3, 4]]) == 139
assert my_solution.sumOfPower(**[[8, 7]]) == 1303
assert my_solution.sumOfPower(**[[6, 6]]) == 648
assert my_solution.sumOfPower(**[[2, 4, 6]]) == 608


# Find the Distinct Difference Array

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

The **distinct difference** array of `nums` is an array `diff` of length `n` such that `diff[i]` is equal to the number of distinct elements in the suffix `nums[i + 1, ..., n - 1]` **subtracted from** the number of distinct elements in the prefix `nums[0, ..., i]`.

Return *the **distinct difference** array of* `nums`.

Note that `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j` inclusive. Particularly, if `i > j` then `nums[i, ..., j]` denotes an empty subarray.

**Example 1:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** [-3,-1,1,3,5]
**Explanation:** For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3.
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1.
For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3.
For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5.

```

**Example 2:**

```

**Input:** nums = [3,2,3,4,2]
**Output:** [-2,-1,0,2,3]
**Explanation:** For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2.
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0.
For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2.
For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.distinctDifferenceArray(**[[1, 2, 3, 4, 5]]) == [-3, -1, 1, 3, 5]
assert my_solution.distinctDifferenceArray(**[[3, 2, 3, 4, 2]]) == [-2, -1, 0, 2, 3]
assert my_solution.distinctDifferenceArray(**[[16]]) == [1]
assert my_solution.distinctDifferenceArray(**[[16, 16]]) == [0, 1]
assert my_solution.distinctDifferenceArray(**[[13, 13, 13]]) == [0, 0, 1]
assert my_solution.distinctDifferenceArray(**[[36, 36, 36, 36]]) == [0, 0, 0, 1]
assert my_solution.distinctDifferenceArray(**[[40, 40, 40, 40, 40]]) == [0, 0, 0, 0, 1]
assert my_solution.distinctDifferenceArray(**[[37, 37, 37, 37, 33]]) == [-1, -1, -1, 0, 2]
assert my_solution.distinctDifferenceArray(**[[42, 42, 42, 42, 47]]) == [-1, -1, -1, 0, 2]
assert my_solution.distinctDifferenceArray(**[[9, 9, 9, 9, 31]]) == [-1, -1, -1, 0, 2]
assert my_solution.distinctDifferenceArray(**[[14, 14, 14, 14, 5]]) == [-1, -1, -1, 0, 2]
assert my_solution.distinctDifferenceArray(**[[41, 41, 41, 17]]) == [-1, -1, 0, 2]
assert my_solution.distinctDifferenceArray(**[[9, 9, 9, 30, 9]]) == [-1, -1, -1, 1, 2]
assert my_solution.distinctDifferenceArray(**[[21, 21, 21, 4, 4]]) == [-1, -1, 0, 1, 2]
assert my_solution.distinctDifferenceArray(**[[38, 38, 38, 14, 19]]) == [-2, -2, -1, 1, 3]
assert my_solution.distinctDifferenceArray(**[[11, 11, 11, 7, 13]]) == [-2, -2, -1, 1, 3]
assert my_solution.distinctDifferenceArray(**[[36, 36, 36, 18, 27]]) == [-2, -2, -1, 1, 3]
assert my_solution.distinctDifferenceArray(**[[37, 37, 37, 47]]) == [-1, -1, 0, 2]
assert my_solution.distinctDifferenceArray(**[[13, 13, 13, 34, 13]]) == [-1, -1, -1, 1, 2]
assert my_solution.distinctDifferenceArray(**[[39, 39, 39, 38, 1]]) == [-2, -2, -1, 1, 3]


# Number of Adjacent Elements With the Same Color

There is a **0-indexed** array `nums` of length `n`. Initially, all elements are **uncolored** (has a value of `0`).

You are given a 2D integer array `queries` where `queries[i] = [indexi, colori]`.

For each query, you color the index `indexi` with the color `colori` in the array `nums`.

Return *an array* `answer` *of the same length as* `queries` *where* `answer[i]` *is the number of adjacent elements with the same color **after** the* `ith` *query*.

More formally, `answer[i]` is the number of indices `j`, such that `0 <= j < n - 1` and `nums[j] == nums[j + 1]` and `nums[j] != 0` after the `ith` query.

**Example 1:**

```

**Input:** n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
**Output:** [0,1,1,0,2]
**Explanation:** Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
- After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
- After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
- After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
- After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
- After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.

```

**Example 2:**

```

**Input:** n = 1, queries = [[0,100000]]
**Output:** [0]
**Explanation:** Initially array nums = [0], where 0 denotes uncolored elements of the array.
- After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0.

```

**Constraints:**

* `1 <= n <= 105`
* `1 <= queries.length <= 105`
* `queries[i].length == 2`
* `0 <= indexi <= n - 1`
* `1 <=  colori <= 105`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
```


# Determine the Winner of a Bowling Game

You are given two **0-indexed** integer arrays `player1` and `player2`, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.

The bowling game consists of `n` turns, and the number of pins in each turn is exactly `10`.

Assume a player hit `xi` pins in the `ith` turn. The value of the `ith` turn for the player is:

* `2xi` if the player hit `10` pins in any of the previous two turns.
* Otherwise, It is `xi`.

The score of the player is the sum of the values of their `n` turns.

Return

* `1` *if the score of player 1 is more than the score of player 2,*
* `2` *if the score of player 2 is more than the score of player 1, and*
* `0` *in case of a draw.*

**Example 1:**

```

**Input:** player1 = [4,10,7,9], player2 = [6,5,2,3]
**Output:** 1
**Explanation:** The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
The score of player2 is 6 + 5 + 2 + 3 = 16.
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.

```

**Example 2:**

```

**Input:** player1 = [3,5,7,6], player2 = [8,10,10,2]
**Output:** 2
**Explanation:** The score of player1 is 3 + 5 + 7 + 6 = 21.
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.

```

**Example 3:**

```

**Input:** player1 = [2,3], player2 = [4,1]
**Output:** 0
**Explanation:** The score of player1 is 2 + 3 = 5
The score of player2 is 4 + 1 = 5
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.

```

**Constraints:**

* `n == player1.length == player2.length`
* `1 <= n <= 1000`
* `0 <= player1[i], player2[i] <= 10`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def isWinner(self, player1: List[int], player2: List[int]) -> int:
```


# Minimum Cost of a Path With Special Roads

You are given an array `start` where `start = [startX, startY]` represents your initial position `(startX, startY)` in a 2D space. You are also given the array `target` where `target = [targetX, targetY]` represents your target position `(targetX, targetY)`.

The cost of going from a position `(x1, y1)` to any other position in the space `(x2, y2)` is `|x2 - x1| + |y2 - y1|`.

There are also some special roads. You are given a 2D array `specialRoads` where `specialRoads[i] = [x1i, y1i, x2i, y2i, costi]` indicates that the `ith` special road can take you from `(x1i, y1i)` to `(x2i, y2i)` with a cost equal to `costi`. You can use each special road any number of times.

Return *the minimum cost required to go from* `(startX, startY)` to `(targetX, targetY)`.

**Example 1:**

```

**Input:** start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
**Output:** 5
**Explanation:** The optimal path from (1,1) to (4,5) is the following:
- (1,1) -> (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1.
- (1,2) -> (3,3). This move uses the first special edge, the cost is 2.
- (3,3) -> (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1.
- (3,4) -> (4,5). This move uses the second special edge, the cost is 1.
So the total cost is 1 + 2 + 1 + 1 = 5.
It can be shown that we cannot achieve a smaller total cost than 5.

```

**Example 2:**

```

**Input:** start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
**Output:** 7
**Explanation:** It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.

```

**Constraints:**

* `start.length == target.length == 2`
* `1 <= startX <= targetX <= 105`
* `1 <= startY <= targetY <= 105`
* `1 <= specialRoads.length <= 200`
* `specialRoads[i].length == 5`
* `startX <= x1i, x2i <= targetX`
* `startY <= y1i, y2i <= targetY`
* `1 <= costi <= 105`

Please make sure your answer follows the type signature below:

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


# Lexicographically Smallest Beautiful String

A string is **beautiful** if:

* It consists of the first `k` letters of the English lowercase alphabet.
* It does not contain any substring of length `2` or more which is a palindrome.

You are given a beautiful string `s` of length `n` and a positive integer `k`.

Return *the lexicographically smallest string of length* `n`*, which is larger than* `s` *and is **beautiful***. If there is no such string, return an empty string.

A string `a` is lexicographically larger than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly larger than the corresponding character in `b`.

* For example, `"abcd"` is lexicographically larger than `"abcc"` because the first position they differ is at the fourth character, and `d` is greater than `c`.

**Example 1:**

```

**Input:** s = "abcz", k = 26
**Output:** "abda"
**Explanation:** The string "abda" is beautiful and lexicographically larger than the string "abcz".
It can be proven that there is no string that is lexicographically larger than the string "abcz", beautiful, and lexicographically smaller than the string "abda".

```

**Example 2:**

```

**Input:** s = "dc", k = 4
**Output:** ""
**Explanation:** It can be proven that there is no string that is lexicographically larger than the string "dc" and is beautiful.

```

**Constraints:**

* `1 <= n == s.length <= 105`
* `4 <= k <= 26`
* `s` is a beautiful string.

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.smallestBeautifulString(**['abcz', 26]) == abda
assert my_solution.smallestBeautifulString(**['dc', 4]) ==
assert my_solution.smallestBeautifulString(**['abc', 8]) == abd
assert my_solution.smallestBeautifulString(**['dfa', 6]) == dfb
assert my_solution.smallestBeautifulString(**['dca', 4]) == dcb
assert my_solution.smallestBeautifulString(**['da', 8]) == db
assert my_solution.smallestBeautifulString(**['feda', 7]) == fedb
assert my_solution.smallestBeautifulString(**['b', 6]) == c
assert my_solution.smallestBeautifulString(**['ced', 6]) == cef
assert my_solution.smallestBeautifulString(**['gfc', 8]) == gfd
assert my_solution.smallestBeautifulString(**['facf', 7]) == facg
assert my_solution.smallestBeautifulString(**['cegaf', 7]) == cegba
assert my_solution.smallestBeautifulString(**['edg', 7]) == efa
assert my_solution.smallestBeautifulString(**['acbac', 5]) == acbad
assert my_solution.smallestBeautifulString(**['fedc', 6]) == fedf
assert my_solution.smallestBeautifulString(**['cd', 4]) == da
assert my_solution.smallestBeautifulString(**['fadce', 7]) == fadcf
assert my_solution.smallestBeautifulString(**['cfb', 6]) == cfd
assert my_solution.smallestBeautifulString(**['dfce', 6]) == dfea
assert my_solution.smallestBeautifulString(**['ebga', 8]) == ebgc


# Maximum Sum With Exactly K Elements

You are given a **0-indexed** integer array `nums` and an integer `k`. Your task is to perform the following operation **exactly** `k` times in order to maximize your score:

1. Select an element `m` from `nums`.
2. Remove the selected element `m` from the array.
3. Add a new element with a value of `m + 1` to the array.
4. Increase your score by `m`.

Return *the maximum score you can achieve after performing the operation exactly* `k` *times.*

**Example 1:**

```

**Input:** nums = [1,2,3,4,5], k = 3
**Output:** 18
**Explanation:** We need to choose exactly 3 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
So, we will return 18.
It can be proven, that 18 is the maximum answer that we can achieve.

```

**Example 2:**

```

**Input:** nums = [5,5,5], k = 2
**Output:** 11
**Explanation:** We need to choose exactly 2 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
So, we will return 11.
It can be proven, that 11 is the maximum answer that we can achieve.

```

**Constraints:**

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

.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
}
.spoiler {overflow:hidden;}
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}

Please make sure your answer follows the type signature below:

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


# Find the Prefix Common Array of Two Arrays

You are given two **0-indexed** integerpermutations `A` and `B` of length `n`.

A **prefix common array** of `A` and `B` is an array `C` such that `C[i]` is equal to the count of numbers that are present at or before the index `i` in both `A` and `B`.

Return *the **prefix common array** of* `A` *and* `B`.

A sequence of `n` integers is called a **permutation** if it contains all integers from `1` to `n` exactly once.

**Example 1:**

```

**Input:** A = [1,3,2,4], B = [3,1,2,4]
**Output:** [0,2,3,4]
**Explanation:** At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.

```

**Example 2:**

```

**Input:** A = [2,3,1], B = [3,1,2]
**Output:** [0,1,3]
**Explanation:** At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.

```

**Constraints:**

* `1 <= A.length == B.length == n <= 50`
* `1 <= A[i], B[i] <= n`
* `It is guaranteed that A and B are both a permutation of n integers.`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
```

my_solution = Solution()

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


# Make Array Empty

You are given an integer array `nums` containing **distinct** numbers, and you can perform the following operations **until the array is empty**:

* If the first element has the **smallest** value, remove it
* Otherwise, put the first element at the **end** of the array.

Return *an integer denoting the number of operations it takes to make* `nums` *empty.*

**Example 1:**

```

**Input:** nums = [3,4,-1]
**Output:** 5

```

| Operation | Array |
| --- | --- |
| 1 | [4, -1, 3] |
| 2 | [-1, 3, 4] |
| 3 | [3, 4] |
| 4 | [4] |
| 5 | [] |

**Example 2:**

```

**Input:** nums = [1,2,4,3]
**Output:** 5

```

| Operation | Array |
| --- | --- |
| 1 | [2, 4, 3] |
| 2 | [4, 3] |
| 3 | [3, 4] |
| 4 | [4] |
| 5 | [] |

**Example 3:**

```

**Input:** nums = [1,2,3]
**Output:** 3

```

| Operation | Array |
| --- | --- |
| 1 | [2, 3] |
| 2 | [3] |
| 3 | [] |

**Constraints:**

* `1 <= nums.length <= 105`
* `-109<= nums[i] <= 109`
* All values in `nums` are **distinct**.

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.countOperationsToEmptyArray(**[[3, 4, -1]]) == 5
assert my_solution.countOperationsToEmptyArray(**[[1, 2, 4, 3]]) == 5
assert my_solution.countOperationsToEmptyArray(**[[1, 2, 3]]) == 3
assert my_solution.countOperationsToEmptyArray(**[[-18]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[-17]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[-13]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[-8]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[-6]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[3]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[14]]) == 1
assert my_solution.countOperationsToEmptyArray(**[[-20, -6]]) == 2
assert my_solution.countOperationsToEmptyArray(**[[-19, -11]]) == 2
assert my_solution.countOperationsToEmptyArray(**[[-17, -18]]) == 3
assert my_solution.countOperationsToEmptyArray(**[[-10, -1]]) == 2
assert my_solution.countOperationsToEmptyArray(**[[-10, 10]]) == 2
assert my_solution.countOperationsToEmptyArray(**[[-9, -10]]) == 3
assert my_solution.countOperationsToEmptyArray(**[[-3, -4]]) == 3
assert my_solution.countOperationsToEmptyArray(**[[1, -4]]) == 3
assert my_solution.countOperationsToEmptyArray(**[[1, 0]]) == 3
assert my_solution.countOperationsToEmptyArray(**[[5, 18]]) == 2


# Calculate Delayed Arrival Time

You are given a positive integer `arrivalTime` denoting the arrival time of a train in hours, and another positive integer `delayedTime` denoting the amount of delay in hours.

Return *the time when the train will arrive at the station.*

Note that the time in this problem is in 24-hours format.

**Example 1:**

```

**Input:** arrivalTime = 15, delayedTime = 5
**Output:** 20
**Explanation:** Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours).

```

**Example 2:**

```

**Input:** arrivalTime = 13, delayedTime = 11
**Output:** 0
**Explanation:** Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0).

```

**Constraints:**

* `1 <= arrivaltime < 24`
* `1 <= delayedTime <= 24`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
```

my_solution = Solution()

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


# Sum Multiples

Given a positive integer `n`, find the sum of all integers in the range `[1, n]` **inclusive** that are divisible by `3`, `5`, or `7`.

Return *an integer denoting the sum of all numbers in the given range satisfying the constraint.*

**Example 1:**

```

**Input:** n = 7
**Output:** 21
**Explanation:** Numbers in the range [1, 7] that are divisible by 3, 5, or 7 are 3, 5, 6, 7. The sum of these numbers is 21.

```

**Example 2:**

```

**Input:** n = 10
**Output:** 40
**Explanation:** Numbers in the range [1, 10] that are divisible by 3, 5, or 7 are 3, 5, 6, 7, 9, 10. The sum of these numbers is 40.

```

**Example 3:**

```

**Input:** n = 9
**Output:** 30
**Explanation:** Numbers in the range [1, 9] that are divisible by 3, 5, or 7 are 3, 5, 6, 7, 9. The sum of these numbers is 30.

```

**Constraints:**

* `1 <= n <= 103`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def sumOfMultiples(self, n: int) -> int:
```

my_solution = Solution()

assert my_solution.sumOfMultiples(**[7]) == 21
assert my_solution.sumOfMultiples(**[10]) == 40
assert my_solution.sumOfMultiples(**[9]) == 30
assert my_solution.sumOfMultiples(**[1]) == 0
assert my_solution.sumOfMultiples(**[2]) == 0
assert my_solution.sumOfMultiples(**[3]) == 3
assert my_solution.sumOfMultiples(**[4]) == 3
assert my_solution.sumOfMultiples(**[5]) == 8
assert my_solution.sumOfMultiples(**[6]) == 14
assert my_solution.sumOfMultiples(**[8]) == 21
assert my_solution.sumOfMultiples(**[11]) == 40
assert my_solution.sumOfMultiples(**[12]) == 52
assert my_solution.sumOfMultiples(**[13]) == 52
assert my_solution.sumOfMultiples(**[14]) == 66
assert my_solution.sumOfMultiples(**[15]) == 81
assert my_solution.sumOfMultiples(**[16]) == 81
assert my_solution.sumOfMultiples(**[17]) == 81
assert my_solution.sumOfMultiples(**[18]) == 99
assert my_solution.sumOfMultiples(**[19]) == 99
assert my_solution.sumOfMultiples(**[20]) == 119


# Sliding Subarray Beauty

Given an integer array `nums` containing `n` integers, find the **beauty** of each subarray of size `k`.

The **beauty** of a subarray is the `xth` **smallest integer** in the subarray if it is **negative**, or `0` if there are fewer than `x` negative integers.

Return *an integer array containing* `n - k + 1` *integers, which denote the* **beauty** *of the subarrays **in order** from the first index in the array.*

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

**Example 1:**

```

**Input:** nums = [1,-1,-3,-2,3], k = 3, x = 2
**Output:** [-1,-2,-2]
**Explanation:** There are 3 subarrays with size k = 3.
The first subarray is [1, -1, -3] and the 2nd smallest negative integer is -1.
The second subarray is [-1, -3, -2] and the 2nd smallest negative integer is -2.
The third subarray is [-3, -2, 3] and the 2nd smallest negative integer is -2.
```

**Example 2:**

```

**Input:** nums = [-1,-2,-3,-4,-5], k = 2, x = 2
**Output:** [-1,-2,-3,-4]
**Explanation:** There are 4 subarrays with size k = 2.
For [-1, -2], the 2nd smallest negative integer is -1.
For [-2, -3], the 2nd smallest negative integer is -2.
For [-3, -4], the 2nd smallest negative integer is -3.
For [-4, -5], the 2nd smallest negative integer is -4.
```

**Example 3:**

```

**Input:** nums = [-3,1,2,-3,0,-3], k = 2, x = 1
**Output:** [-3,0,-3,-3,-3]
**Explanation:** There are 5 subarrays with size k = 2**.**
For [-3, 1], the 1st smallest negative integer is -3.
For [1, 2], there is no negative integer so the beauty is 0.
For [2, -3], the 1st smallest negative integer is -3.
For [-3, 0], the 1st smallest negative integer is -3.
For [0, -3], the 1st smallest negative integer is -3.
```

**Constraints:**

* `n == nums.length`
* `1 <= n <= 105`
* `1 <= k <= n`
* `1 <= x <= k`
* `-50 <= nums[i] <= 50`

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.getSubarrayBeauty(**[[1, -1, -3, -2, 3], 3, 2]) == [-1, -2, -2]
assert my_solution.getSubarrayBeauty(**[[-1, -2, -3, -4, -5], 2, 2]) == [-1, -2, -3, -4]
assert my_solution.getSubarrayBeauty(**[[-3, 1, 2, -3, 0, -3], 2, 1]) == [-3, 0, -3, -3, -3]
assert my_solution.getSubarrayBeauty(**[[-43], 1, 1]) == [-43]
assert my_solution.getSubarrayBeauty(**[[-26], 1, 1]) == [-26]
assert my_solution.getSubarrayBeauty(**[[-22], 1, 1]) == [-22]
assert my_solution.getSubarrayBeauty(**[[-20], 1, 1]) == [-20]
assert my_solution.getSubarrayBeauty(**[[-19], 1, 1]) == [-19]
assert my_solution.getSubarrayBeauty(**[[-18], 1, 1]) == [-18]
assert my_solution.getSubarrayBeauty(**[[-17], 1, 1]) == [-17]
assert my_solution.getSubarrayBeauty(**[[-15], 1, 1]) == [-15]
assert my_solution.getSubarrayBeauty(**[[-2], 1, 1]) == [-2]
assert my_solution.getSubarrayBeauty(**[[5], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[7], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[14], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[16], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[19], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[20], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[23], 1, 1]) == [0]
assert my_solution.getSubarrayBeauty(**[[27], 1, 1]) == [0]


# Minimum Number of Operations to Make All Array Elements Equal to 1

You are given a **0-indexed** array `nums` consisiting of **positive** integers. You can do the following operation on the array **any** number of times:

* Select an index `i` such that `0 <= i < n - 1` and replace either of `nums[i]` or `nums[i+1]` with their gcd value.

Return *the **minimum** number of operations to make all elements of* `nums` *equal to* `1`. If it is impossible, return `-1`.

The gcd of two integers is the greatest common divisor of the two integers.

**Example 1:**

```

**Input:** nums = [2,6,3,4]
**Output:** 4
**Explanation:** We can do the following operations:
- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4].
- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4].
- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4].
- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].

```

**Example 2:**

```

**Input:** nums = [2,10,6,14]
**Output:** -1
**Explanation:** It can be shown that it is impossible to make all the elements equal to 1.

```

**Constraints:**

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

**Follow-up:**

The `O(n)` time complexity solution works, but could you find an `O(1)` constant time complexity solution?

Please make sure your answer follows the type signature below:

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


# Row With Maximum Ones

Given a `m x n` binary matrix `mat`, find the **0-indexed** position of the row that contains the **maximum** count of **ones,** and the number of ones in that row.

In case there are multiple rows that have the maximum count of ones, the row with the **smallest row number** should be selected.

Return *an array containing the index of the row, and the number of ones in it.*

**Example 1:**

```

**Input:** mat = [[0,1],[1,0]]
**Output:** [0,1]
**Explanation:** Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1). So, the answer is [0,1].

```

**Example 2:**

```

**Input:** mat = [[0,0,0],[0,1,1]]
**Output:** [1,2]
**Explanation:** The row indexed 1 has the maximum count of ones (2). So we return its index, 1, and the count. So, the answer is [1,2].

```

**Example 3:**

```

**Input:** mat = [[0,0],[1,1],[0,0]]
**Output:** [1,2]
**Explanation:** The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].

```

**Constraints:**

* `m == mat.length`
* `n == mat[i].length`
* `1 <= m, n <= 100`
* `mat[i][j]` is either `0` or `1`.

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

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


# Minimum Additions to Make Valid String

Given a string `word` to which you can insert letters "a", "b" or "c" anywhere and any number of times, return *the minimum number of letters that must be inserted so that `word` becomes **valid**.*

A string is called **valid** if it can be formed by concatenating the string "abc" several times.

**Example 1:**

```

**Input:** word = "b"
**Output:** 2
**Explanation:** Insert the letter "a" right before "b", and the letter "c" right next to "a" to obtain the valid string "**a**b**c**".

```

**Example 2:**

```

**Input:** word = "aaa"
**Output:** 6
**Explanation:** Insert letters "b" and "c" next to each "a" to obtain the valid string "a**bc**a**bc**a**bc**".

```

**Example 3:**

```

**Input:** word = "abc"
**Output:** 0
**Explanation:** word is already valid. No modifications are needed.

```

**Constraints:**

* `1 <= word.length <= 50`
* `word` consists of letters "a", "b" and "c" only.

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.addMinimum(**['b']) == 2
assert my_solution.addMinimum(**['aaa']) == 6
assert my_solution.addMinimum(**['abc']) == 0
assert my_solution.addMinimum(**['a']) == 2
assert my_solution.addMinimum(**['aa']) == 4
assert my_solution.addMinimum(**['aaaa']) == 8
assert my_solution.addMinimum(**['aaaaa']) == 10
assert my_solution.addMinimum(**['aaaaaa']) == 12
assert my_solution.addMinimum(**['aaaaab']) == 9
assert my_solution.addMinimum(**['aaaaac']) == 9
assert my_solution.addMinimum(**['aaaab']) == 7
assert my_solution.addMinimum(**['aaaaba']) == 9
assert my_solution.addMinimum(**['aaaabb']) == 9
assert my_solution.addMinimum(**['aaaabc']) == 6
assert my_solution.addMinimum(**['aaaac']) == 7
assert my_solution.addMinimum(**['aaaaca']) == 9
assert my_solution.addMinimum(**['aaaacb']) == 9
assert my_solution.addMinimum(**['aaaacc']) == 9
assert my_solution.addMinimum(**['aaab']) == 5
assert my_solution.addMinimum(**['aaaba']) == 7


# Find the Width of Columns of a Grid

You are given a **0-indexed** `m x n` integer matrix `grid`. The width of a column is the maximum **length** of its integers.

* For example, if `grid = [[-10], [3], [12]]`, the width of the only column is `3` since `-10` is of length `3`.

Return *an integer array* `ans` *of size* `n` *where* `ans[i]` *is the width of the* `ith` *column*.

The **length** of an integer `x` with `len` digits is equal to `len` if `x` is non-negative, and `len + 1` otherwise.

**Example 1:**

```

**Input:** grid = [[1],[22],[333]]
**Output:** [3]
**Explanation:** In the 0th column, 333 is of length 3.

```

**Example 2:**

```

**Input:** grid = [[-15,1,3],[15,7,12],[5,6,-2]]
**Output:** [3,1,2]
**Explanation:**
In the 0th column, only -15 is of length 3.
In the 1st column, all integers are of length 1.
In the 2nd column, both 12 and -2 are of length 2.

```

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 100`
* `-109 <= grid[r][c] <= 109`

Please make sure your answer follows the type signature below:

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


# Find the Score of All Prefixes of an Array

We define the **conversion array** `conver` of an array `arr` as follows:

* `conver[i] = arr[i] + max(arr[0..i])` where `max(arr[0..i])` is the maximum value of `arr[j]` over `0 <= j <= i`.

We also define the **score** of an array `arr` as the sum of the values of the conversion array of `arr`.

Given a **0-indexed** integer array `nums` of length `n`, return *an array* `ans` *of length* `n` *where* `ans[i]` *is the score of the prefix* `nums[0..i]`.

**Example 1:**

```

**Input:** nums = [2,3,7,5,10]
**Output:** [4,10,24,36,56]
**Explanation:**
For the prefix [2], the conversion array is [4] hence the score is 4
For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56

```

**Example 2:**

```

**Input:** nums = [1,1,2,4,8,16]
**Output:** [2,4,8,16,32,64]
**Explanation:**
For the prefix [1], the conversion array is [2] hence the score is 2
For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Sum of Distances

You are given a **0-indexed** integer array `nums`. There exists an array `arr` of length `nums.length`, where `arr[i]` is the sum of `|i - j|` over all `j` such that `nums[j] == nums[i]` and `j != i`. If there is no such `j`, set `arr[i]` to be `0`.

Return *the array* `arr`*.*

**Example 1:**

```

**Input:** nums = [1,3,1,1,2]
**Output:** [5,0,3,4,0]
**Explanation:**
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
When i = 1, arr[1] = 0 because there is no other index with value 3.
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
When i = 4, arr[4] = 0 because there is no other index with value 2.

```

**Example 2:**

```

**Input:** nums = [0,5,3]
**Output:** [0,0,0]
**Explanation:** Since each element in nums is distinct, arr[i] = 0 for all i.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Minimize the Maximum Difference of Pairs

You are given a **0-indexed** integer array `nums` and an integer `p`. Find `p` pairs of indices of `nums` such that the **maximum** difference amongst all the pairs is **minimized**. Also, ensure no index appears more than once amongst the `p` pairs.

Note that for a pair of elements at the index `i` and `j`, the difference of this pair is `|nums[i] - nums[j]|`, where `|x|` represents the **absolute** **value** of `x`.

Return *the **minimum** **maximum** difference among all* `p` *pairs.* We define the maximum of an empty set to be zero.

**Example 1:**

```

**Input:** nums = [10,1,2,7,1,3], p = 2
**Output:** 1
**Explanation:** The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.

```

**Example 2:**

```

**Input:** nums = [4,2,1,2], p = 1
**Output:** 0
**Explanation:** Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.

```

**Constraints:**

* `1 <= nums.length <= 105`
* `0 <= nums[i] <= 109`
* `0 <= p <= (nums.length)/2`

Please make sure your answer follows the type signature below:

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


# Find the Longest Balanced Substring of a Binary String

You are given a binary string `s` consisting only of zeroes and ones.

A substring of `s` is considered balanced if **all zeroes are before ones** and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.

Return *the length of the longest balanced substring of* `s`.

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

**Example 1:**

```

**Input:** s = "01000111"
**Output:** 6
**Explanation:** The longest balanced substring is "000111", which has length 6.

```

**Example 2:**

```

**Input:** s = "00111"
**Output:** 4
**Explanation:** The longest balanced substring is "0011", which has length 4.

```

**Example 3:**

```

**Input:** s = "111"
**Output:** 0
**Explanation:** There is no balanced substring except the empty substring, so the answer is 0.

```

**Constraints:**

* `1 <= s.length <= 50`
* `'0' <= s[i] <= '1'`

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.findTheLongestBalancedSubstring(**['01000111']) == 6
assert my_solution.findTheLongestBalancedSubstring(**['0011']) == 4
assert my_solution.findTheLongestBalancedSubstring(**['111']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['0']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['1']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['00']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['01']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['10']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['11']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['000']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['001']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['010']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['011']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['100']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['101']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['110']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['0000']) == 0
assert my_solution.findTheLongestBalancedSubstring(**['0001']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['0010']) == 2
assert my_solution.findTheLongestBalancedSubstring(**['0100']) == 2


# Convert an Array Into a 2D Array With Conditions

You are given an integer array `nums`. You need to create a 2D array from `nums` satisfying the following conditions:

* The 2D array should contain **only** the elements of the array `nums`.
* Each row in the 2D array contains **distinct** integers.
* The number of rows in the 2D array should be **minimal**.

Return *the resulting array*. If there are multiple answers, return any of them.

**Note** that the 2D array can have a different number of elements on each row.

**Example 1:**

```

**Input:** nums = [1,3,4,1,2,3,1]
**Output:** [[1,3,4,2],[1,3],[1]]
**Explanation:** We can create a 2D array that contains the following rows:
- 1,3,4,2
- 1,3
- 1
All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
It can be shown that we cannot have less than 3 rows in a valid array.
```

**Example 2:**

```

**Input:** nums = [1,2,3,4]
**Output:** [[4,3,2,1]]
**Explanation:** All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Mice and Cheese

There are two mice and `n` different types of cheese, each type of cheese should be eaten by exactly one mouse.

A point of the cheese with index `i` (**0-indexed**) is:

* `reward1[i]` if the first mouse eats it.
* `reward2[i]` if the second mouse eats it.

You are given a positive integer array `reward1`, a positive integer array `reward2`, and a non-negative integer `k`.

Return ***the maximum** points the mice can achieve if the first mouse eats exactly* `k` *types of cheese.*

**Example 1:**

```

**Input:** reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2
**Output:** 15
**Explanation:** In this example, the first mouse eats the 2nd (0-indexed) and the 3rd types of cheese, and the second mouse eats the 0th and the 1st types of cheese.
The total points are 4 + 4 + 3 + 4 = 15.
It can be proven that 15 is the maximum total points that the mice can achieve.

```

**Example 2:**

```

**Input:** reward1 = [1,1], reward2 = [1,1], k = 2
**Output:** 2
**Explanation:** In this example, the first mouse eats the 0th (0-indexed) and 1st types of cheese, and the second mouse does not eat any cheese.
The total points are 1 + 1 = 2.
It can be proven that 2 is the maximum total points that the mice can achieve.

```

**Constraints:**

* `1 <= n == reward1.length == reward2.length <= 105`
* `1 <= reward1[i], reward2[i] <= 1000`
* `0 <= k <= n`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def miceAndCheese(self, reward1: List[int], reward2: List[int], k: int) -> int:
```

my_solution = Solution()

assert my_solution.miceAndCheese(**[[1, 1, 3, 4], [4, 4, 1, 1], 2]) == 15
assert my_solution.miceAndCheese(**[[1, 1], [1, 1], 2]) == 2
assert my_solution.miceAndCheese(**[[2, 1], [1, 2], 1]) == 4
assert my_solution.miceAndCheese(**[[3, 3], [3, 5], 1]) == 8
assert my_solution.miceAndCheese(**[[4, 1, 5, 3, 3], [3, 4, 4, 5, 2], 3]) == 21
assert my_solution.miceAndCheese(**[[1, 4, 4, 6, 4], [6, 5, 3, 6, 1], 1]) == 24
assert my_solution.miceAndCheese(**[[1, 2, 1, 2, 1, 2], [2, 1, 1, 2, 2, 1], 0]) == 9
assert my_solution.miceAndCheese(**[[2, 2, 1, 2], [1, 2, 1, 2], 2]) == 7
assert my_solution.miceAndCheese(**[[1], [4], 1]) == 1
assert my_solution.miceAndCheese(**[[1, 2, 2], [2, 1, 2], 2]) == 6
assert my_solution.miceAndCheese(**[[3, 1, 1, 3], [3, 2, 3, 1], 3]) == 10
assert my_solution.miceAndCheese(**[[4], [5], 0]) == 5
assert my_solution.miceAndCheese(**[[2, 3], [3, 3], 2]) == 5
assert my_solution.miceAndCheese(**[[1, 1], [1, 1], 1]) == 2
assert my_solution.miceAndCheese(**[[1, 3], [5, 4], 1]) == 8
assert my_solution.miceAndCheese(**[[2, 1, 4], [1, 4, 2], 0]) == 7
assert my_solution.miceAndCheese(**[[3, 4, 2, 1, 5], [2, 3, 5, 2, 4], 0]) == 16
assert my_solution.miceAndCheese(**[[5, 2], [3, 1], 2]) == 7
assert my_solution.miceAndCheese(**[[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], 5]) == 5
assert my_solution.miceAndCheese(**[[2, 2, 1, 2, 1], [1, 1, 2, 1, 1], 0]) == 6


# Minimum Reverse Operations

You are given an integer `n` and an integer `p` in the range `[0, n - 1]`. Representing a **0-indexed** array `arr` of length `n` where all positions are set to `0`'s, except position `p` which is set to `1`.

You are also given an integer array `banned` containing some positions from the array. For the **i****th** position in `banned`, `arr[banned[i]] = 0`, and `banned[i] != p`.

You can perform **multiple** operations on `arr`. In an operation, you can choose a **subarray** with size `k` and **reverse** the subarray. However, the `1` in `arr` should never go to any of the positions in `banned`. In other words, after each operation `arr[banned[i]]` **remains** `0`.

*Return an array* `ans` *where* *for each* `i` *from* `[0, n - 1]`, `ans[i]` *is the **minimum** number of reverse operations needed to bring the* `1` *to position* `i` *in arr*, *or* `-1` *if it is impossible*.

* A **subarray** is a contiguous **non-empty** sequence of elements within an array.
* The values of `ans[i]` are independent for all `i`'s.
* The **reverse** of an array is an array containing the values in **reverse order**.

**Example 1:**

```

**Input:** n = 4, p = 0, banned = [1,2], k = 4
**Output:** [0,-1,-1,1]
**Explanation:** In this case k = 4 so there is only one possible reverse operation we can perform, which is reversing the whole array. Initially, 1is placed at position 0 so the amount of operations we need for position 0 is 0. We can never place a 1 on the banned positions, so the answer for positions 1 and 2 is -1. Finally, with one reverse operation we can bring the 1 to index 3, so the answer for position 3 is 1.

```

**Example 2:**

```

**Input:** n = 5, p = 0, banned = [2,4], k = 3
**Output:** [0,-1,-1,-1,-1]
**Explanation:** In this case the 1 is initially at position 0, so the answer for that position is 0. We can perform reverse operations of size 3. The 1 is currently located at position 0, so we need to reverse the subarray [0, 2] for it to leave that position, but reversing that subarray makes position 2 have a 1, which shouldn't happen. So, we can't move the 1 from position 0, making the result for all the other positions -1.

```

**Example 3:**

```

**Input:** n = 4, p = 2, banned = [0,1,3], k = 1
**Output:** [-1,-1,0,-1]
**Explanation:** In this case we can only perform reverse operations of size 1.So the 1 never changes its position.

```

**Constraints:**

* `1 <= n <= 105`
* `0 <= p <= n - 1`
* `0 <= banned.length <= n - 1`
* `0 <= banned[i] <= n - 1`
* `1 <= k <= n`
* `banned[i] != p`
* all values in `banned` are **unique**

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minReverseOperations(self, n: int, p: int, banned: List[int], k: int) -> List[int]:
```

my_solution = Solution()

assert my_solution.minReverseOperations(**[4, 0, [1, 2], 4]) == [0, -1, -1, 1]
assert my_solution.minReverseOperations(**[5, 0, [2, 4], 3]) == [0, -1, -1, -1, -1]
assert my_solution.minReverseOperations(**[4, 2, [0, 1, 3], 1]) == [-1, -1, 0, -1]
assert my_solution.minReverseOperations(**[5, 0, [], 2]) == [0, 1, 2, 3, 4]
assert my_solution.minReverseOperations(**[1, 0, [], 1]) == [0]
assert my_solution.minReverseOperations(**[2, 0, [], 1]) == [0, -1]
assert my_solution.minReverseOperations(**[2, 0, [], 2]) == [0, 1]
assert my_solution.minReverseOperations(**[2, 1, [], 1]) == [-1, 0]
assert my_solution.minReverseOperations(**[2, 1, [], 2]) == [1, 0]
assert my_solution.minReverseOperations(**[2, 0, [1], 1]) == [0, -1]
assert my_solution.minReverseOperations(**[2, 0, [1], 2]) == [0, -1]
assert my_solution.minReverseOperations(**[2, 1, [0], 1]) == [-1, 0]
assert my_solution.minReverseOperations(**[2, 1, [0], 2]) == [-1, 0]
assert my_solution.minReverseOperations(**[3, 0, [], 1]) == [0, -1, -1]
assert my_solution.minReverseOperations(**[3, 0, [], 2]) == [0, 1, 2]
assert my_solution.minReverseOperations(**[3, 0, [], 3]) == [0, -1, 1]
assert my_solution.minReverseOperations(**[3, 1, [], 1]) == [-1, 0, -1]
assert my_solution.minReverseOperations(**[3, 1, [], 2]) == [1, 0, 1]
assert my_solution.minReverseOperations(**[3, 1, [], 3]) == [-1, 0, -1]
assert my_solution.minReverseOperations(**[3, 2, [], 1]) == [-1, -1, 0]


# Form Smallest Number From Two Digit Arrays

Given two arrays of **unique** digits `nums1` and `nums2`, return *the **smallest** number that contains **at least** one digit from each array*.

**Example 1:**

```

**Input:** nums1 = [4,1,3], nums2 = [5,7]
**Output:** 15
**Explanation:** The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.

```

**Example 2:**

```

**Input:** nums1 = [3,5,2,6], nums2 = [3,1,7]
**Output:** 3
**Explanation:** The number 3 contains the digit 3 which exists in both arrays.

```

**Constraints:**

* `1 <= nums1.length, nums2.length <= 9`
* `1 <= nums1[i], nums2[i] <= 9`
* All digits in each array are **unique**.

Please make sure your answer follows the type signature below:

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


# Find the Substring With Maximum Cost

You are given a string `s`, a string `chars` of **distinct** characters and an integer array `vals` of the same length as `chars`.

The **cost of the substring** is the sum of the values of each character in the substring. The cost of an empty string is considered `0`.

The **value of the character** is defined in the following way:

* If the character is not in the string `chars`, then its value is its corresponding position **(1-indexed)** in the alphabet.
	+ For example, the value of `'a'` is `1`, the value of `'b'` is `2`, and so on. The value of `'z'` is `26`.
* Otherwise, assuming `i` is the index where the character occurs in the string `chars`, then its value is `vals[i]`.

Return *the maximum cost among all substrings of the string* `s`.

**Example 1:**

```

**Input:** s = "adaa", chars = "d", vals = [-1000]
**Output:** 2
**Explanation:** The value of the characters "a" and "d" is 1 and -1000 respectively.
The substring with the maximum cost is "aa" and its cost is 1 + 1 = 2.
It can be proven that 2 is the maximum cost.

```

**Example 2:**

```

**Input:** s = "abc", chars = "abc", vals = [-1,-1,-1]
**Output:** 0
**Explanation:** The value of the characters "a", "b" and "c" is -1, -1, and -1 respectively.
The substring with the maximum cost is the empty substring "" and its cost is 0.
It can be proven that 0 is the maximum cost.

```

**Constraints:**

* `1 <= s.length <= 105`
* `s` consist of lowercase English letters.
* `1 <= chars.length <= 26`
* `chars` consist of **distinct** lowercase English letters.
* `vals.length == chars.length`
* `-1000 <= vals[i] <= 1000`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumCostSubstring(self, s: str, chars: str, vals: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.maximumCostSubstring(**['adaa', 'd', [-1000]]) == 2
assert my_solution.maximumCostSubstring(**['abc', 'abc', [-1, -1, -1]]) == 0
assert my_solution.maximumCostSubstring(**['hghhghgghh', 'hg', [2, 3]]) == 24
assert my_solution.maximumCostSubstring(**['ddzddzd', 'zfd', [-4, 2, -2]]) == 0
assert my_solution.maximumCostSubstring(**['kqqqqqkkkq', 'kq', [-6, 6]]) == 30
assert my_solution.maximumCostSubstring(**['pprpf', 'frep', [-5, -4, -4, 3]]) == 6
assert my_solution.maximumCostSubstring(**['eggggeee', 'ge', [5, 3]]) == 32
assert my_solution.maximumCostSubstring(**['ssss', 's', [3]]) == 12
assert my_solution.maximumCostSubstring(**['ffff', 'f', [1]]) == 4
assert my_solution.maximumCostSubstring(**['xuusmmums', 'sxmu', [-6, 5, 0, 5]]) == 15
assert my_solution.maximumCostSubstring(**['axaaxxaxa', 'ax', [-6, 1]]) == 2
assert my_solution.maximumCostSubstring(**['ffkfuft', 'utkf', [3, -6, 3, 6]]) == 30
assert my_solution.maximumCostSubstring(**['ll', 'il', [3, -6]]) == 0
assert my_solution.maximumCostSubstring(**['hwwqwwqqqh', 'wihq', [-2, -5, -4, 4]]) == 12
assert my_solution.maximumCostSubstring(**['qjjjqqq', 'zqj', [-4, 3, 4]]) == 24
assert my_solution.maximumCostSubstring(**['talaqlt', 'tqla', [4, 3, 3, -2]]) == 13
assert my_solution.maximumCostSubstring(**['qqqqqqqqqq', 'q', [0]]) == 0
assert my_solution.maximumCostSubstring(**['tt', 't', [-2]]) == 0
assert my_solution.maximumCostSubstring(**['zox', 'zoxr', [2, -5, -4, -5]]) == 2
assert my_solution.maximumCostSubstring(**['ddddddd', 'd', [-3]]) == 0


# Make K-Subarray Sums Equal

You are given a **0-indexed** integer array `arr` and an integer `k`. The array `arr` is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.

You can do the following operation any number of times:

* Pick any element from `arr` and increase or decrease it by `1`.

Return *the minimum number of operations such that the sum of each **subarray** of length* `k` *is equal*.

A **subarray** is a contiguous part of the array.

**Example 1:**

```

**Input:** arr = [1,4,1,3], k = 2
**Output:** 1
**Explanation:** we can do one operation on index 1 to make its value equal to 3.
The array after the operation is [1,3,1,3]
- Subarray starts at index 0 is [1, 3], and its sum is 4
- Subarray starts at index 1 is [3, 1], and its sum is 4
- Subarray starts at index 2 is [1, 3], and its sum is 4
- Subarray starts at index 3 is [3, 1], and its sum is 4

```

**Example 2:**

```

**Input:** arr = [2,5,5,7], k = 3
**Output:** 5
**Explanation:** we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
The array after the operations is [5,5,5,5]
- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
- Subarray starts at index 3 is [5, 5, 5], and its sum is 15

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.makeSubKSumEqual(**[[1, 4, 1, 3], 2]) == 1
assert my_solution.makeSubKSumEqual(**[[2, 5, 5, 7], 3]) == 5
assert my_solution.makeSubKSumEqual(**[[9], 1]) == 0
assert my_solution.makeSubKSumEqual(**[[1, 10], 1]) == 9
assert my_solution.makeSubKSumEqual(**[[4, 6], 2]) == 0
assert my_solution.makeSubKSumEqual(**[[2, 10, 9], 1]) == 8
assert my_solution.makeSubKSumEqual(**[[10, 3, 8], 2]) == 7
assert my_solution.makeSubKSumEqual(**[[3, 3, 9], 3]) == 0
assert my_solution.makeSubKSumEqual(**[[1, 7, 9, 6], 1]) == 9
assert my_solution.makeSubKSumEqual(**[[7, 3, 10, 6], 2]) == 6
assert my_solution.makeSubKSumEqual(**[[1, 5, 8, 10], 3]) == 12
assert my_solution.makeSubKSumEqual(**[[2, 6, 7, 2], 4]) == 0
assert my_solution.makeSubKSumEqual(**[[2, 4, 2, 10, 3], 1]) == 10
assert my_solution.makeSubKSumEqual(**[[9, 1, 5, 10, 6], 2]) == 13
assert my_solution.makeSubKSumEqual(**[[10, 9, 1, 10, 5], 3]) == 14
assert my_solution.makeSubKSumEqual(**[[9, 7, 2, 6, 4], 4]) == 10
assert my_solution.makeSubKSumEqual(**[[10, 5, 10, 2, 9], 5]) == 0
assert my_solution.makeSubKSumEqual(**[[5, 8, 2, 6, 9, 1], 1]) == 15
assert my_solution.makeSubKSumEqual(**[[8, 2, 5, 9, 8, 10], 2]) == 11
assert my_solution.makeSubKSumEqual(**[[9, 4, 8, 8, 5, 8], 3]) == 2


# K Items With the Maximum Sum

There is a bag that consists of items, each item has a number `1`, `0`, or `-1` written on it.

You are given four **non-negative** integers `numOnes`, `numZeros`, `numNegOnes`, and `k`.

The bag initially contains:

* `numOnes` items with `1`s written on them.
* `numZeroes` items with `0`s written on them.
* `numNegOnes` items with `-1`s written on them.

We want to pick exactly `k` items among the available items. Return *the **maximum** possible sum of numbers written on the items*.

**Example 1:**

```

**Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
**Output:** 2
**Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
It can be proven that 2 is the maximum possible sum.

```

**Example 2:**

```

**Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
**Output:** 3
**Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
It can be proven that 3 is the maximum possible sum.

```

**Constraints:**

* `0 <= numOnes, numZeros, numNegOnes <= 50`
* `0 <= k <= numOnes + numZeros + numNegOnes`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:
```

my_solution = Solution()

assert my_solution.kItemsWithMaximumSum(**[3, 2, 0, 2]) == 2
assert my_solution.kItemsWithMaximumSum(**[3, 2, 0, 4]) == 3
assert my_solution.kItemsWithMaximumSum(**[4, 0, 1, 2]) == 2
assert my_solution.kItemsWithMaximumSum(**[1, 4, 1, 3]) == 1
assert my_solution.kItemsWithMaximumSum(**[6, 6, 6, 13]) == 5
assert my_solution.kItemsWithMaximumSum(**[2, 1, 2, 1]) == 1
assert my_solution.kItemsWithMaximumSum(**[8, 4, 0, 8]) == 8
assert my_solution.kItemsWithMaximumSum(**[0, 3, 1, 1]) == 0
assert my_solution.kItemsWithMaximumSum(**[2, 4, 4, 0]) == 0
assert my_solution.kItemsWithMaximumSum(**[3, 7, 4, 8]) == 3
assert my_solution.kItemsWithMaximumSum(**[3, 3, 5, 11]) == -2
assert my_solution.kItemsWithMaximumSum(**[1, 2, 0, 2]) == 1
assert my_solution.kItemsWithMaximumSum(**[1, 6, 4, 4]) == 1
assert my_solution.kItemsWithMaximumSum(**[1, 1, 2, 2]) == 1
assert my_solution.kItemsWithMaximumSum(**[3, 0, 5, 8]) == -2
assert my_solution.kItemsWithMaximumSum(**[1, 0, 3, 4]) == -2
assert my_solution.kItemsWithMaximumSum(**[2, 0, 4, 2]) == 2
assert my_solution.kItemsWithMaximumSum(**[1, 3, 3, 5]) == 0
assert my_solution.kItemsWithMaximumSum(**[4, 3, 2, 1]) == 1
assert my_solution.kItemsWithMaximumSum(**[4, 4, 0, 5]) == 4


# Prime Subtraction Operation

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

You can perform the following operation as many times as you want:

* Pick an index `i` that you haven’t picked before, and pick a prime `p` **strictly less than** `nums[i]`, then subtract `p` from `nums[i]`.

Return *true if you can make `nums` a strictly increasing array using the above operation and false otherwise.*

A **strictly increasing array** is an array whose each element is strictly greater than its preceding element.

**Example 1:**

```

**Input:** nums = [4,9,6,10]
**Output:** true
**Explanation:** In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
After the second operation, nums is sorted in strictly increasing order, so the answer is true.
```

**Example 2:**

```

**Input:** nums = [6,8,11,12]
**Output:** true
**Explanation:** Initially nums is sorted in strictly increasing order, so we don't need to make any operations.
```

**Example 3:**

```

**Input:** nums = [5,8,3]
**Output:** false
**Explanation:** It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.
```

**Constraints:**

* `1 <= nums.length <= 1000`
* `1 <= nums[i] <= 1000`
* `nums.length == n`

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

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


# Minimum Operations to Make All Array Elements Equal

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

You are also given an integer array `queries` of size `m`. For the `ith` query, you want to make all of the elements of `nums` equal to `queries[i]`. You can perform the following operation on the array **any** number of times:

* **Increase** or **decrease** an element of the array by `1`.

Return *an array* `answer` *of size* `m` *where* `answer[i]` *is the **minimum** number of operations to make all elements of* `nums` *equal to* `queries[i]`.

**Note** that after each query the array is reset to its original state.

**Example 1:**

```

**Input:** nums = [3,1,6,8], queries = [1,5]
**Output:** [14,10]
**Explanation:** For the first query we can do the following operations:
- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
So the total number of operations for the first query is 2 + 5 + 7 = 14.
For the second query we can do the following operations:
- Increase nums[0] 2 times, so that nums = [5,1,6,8].
- Increase nums[1] 4 times, so that nums = [5,5,6,8].
- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.

```

**Example 2:**

```

**Input:** nums = [2,9,6,3], queries = [10]
**Output:** [20]
**Explanation:** We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.

```

**Constraints:**

* `n == nums.length`
* `m == queries.length`
* `1 <= n, m <= 105`
* `1 <= nums[i], queries[i] <= 109`

Please make sure your answer follows the type signature below:

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


# Number of Even and Odd Bits

You are given a **positive** integer `n`.

Let `even` denote the number of even indices in the binary representation of `n` (**0-indexed**) with value `1`.

Let `odd` denote the number of odd indices in the binary representation of `n` (**0-indexed**) with value `1`.

Return *an integer array* `answer` *where* `answer = [even, odd]`.

**Example 1:**

```

**Input:** n = 17
**Output:** [2,0]
**Explanation:** The binary representation of 17 is 10001.
It contains 1 on the 0th and 4th indices.
There are 2 even and 0 odd indices.

```

**Example 2:**

```

**Input:** n = 2
**Output:** [0,1]
**Explanation:** The binary representation of 2 is 10.
It contains 1 on the 1st index.
There are 0 even and 1 odd indices.

```

**Constraints:**

* `1 <= n <= 1000`

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

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


# The Number of Beautiful Subsets

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

A subset of `nums` is **beautiful** if it does not contain two integers with an absolute difference equal to `k`.

Return *the number of **non-empty beautiful** subsets of the array* `nums`.

A **subset** of `nums` is an array that can be obtained by deleting some (possibly none) elements from `nums`. Two subsets are different if and only if the chosen indices to delete are different.

**Example 1:**

```

**Input:** nums = [2,4,6], k = 2
**Output:** 4
**Explanation:** The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].

```

**Example 2:**

```

**Input:** nums = [1], k = 1
**Output:** 1
**Explanation:** The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].

```

**Constraints:**

* `1 <= nums.length <= 20`
* `1 <= nums[i], k <= 1000`

Please make sure your answer follows the type signature below:

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


# Smallest Missing Non-negative Integer After Operations

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

In one operation, you can add or subtract `value` from any element of `nums`.

* For example, if `nums = [1,2,3]` and `value = 2`, you can choose to subtract `value` from `nums[0]` to make `nums = [-1,2,3]`.

The MEX (minimum excluded) of an array is the smallest missing **non-negative** integer in it.

* For example, the MEX of `[-1,2,3]` is `0` while the MEX of `[1,0,3]` is `2`.

Return *the maximum MEX of* `nums` *after applying the mentioned operation **any number of times***.

**Example 1:**

```

**Input:** nums = [1,-10,7,13,6,8], value = 5
**Output:** 4
**Explanation:** One can achieve this result by applying the following operations:
- Add value to nums[1] twice to make nums = [1,**0**,7,13,6,8]
- Subtract value from nums[2] once to make nums = [1,0,**2**,13,6,8]
- Subtract value from nums[3] twice to make nums = [1,0,2,**3**,6,8]
The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.

```

**Example 2:**

```

**Input:** nums = [1,-10,7,13,6,8], value = 7
**Output:** 2
**Explanation:** One can achieve this result by applying the following operation:
- subtract value from nums[2] once to make nums = [1,-10,**0**,13,6,8]
The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Distribute Money to Maximum Children

You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to.

You have to distribute the money according to the following rules:

* All money must be distributed.
* Everyone must receive at least `1` dollar.
* Nobody receives `4` dollars.

Return *the **maximum** number of children who may receive **exactly*** `8` *dollars if you distribute the money according to the aforementioned rules*. If there is no way to distribute the money, return `-1`.

**Example 1:**

```

**Input:** money = 20, children = 3
**Output:** 1
**Explanation:**
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
- 8 dollars to the first child.
- 9 dollars to the second child.
- 3 dollars to the third child.
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.

```

**Example 2:**

```

**Input:** money = 16, children = 2
**Output:** 2
**Explanation:** Each child can be given 8 dollars.

```

**Constraints:**

* `1 <= money <= 200`
* `2 <= children <= 30`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def distMoney(self, money: int, children: int) -> int:
```

my_solution = Solution()

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


# Maximize Greatness of an Array

You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing.

We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`.

Return *the **maximum** possible greatness you can achieve after permuting* `nums`.

**Example 1:**

```

**Input:** nums = [1,3,5,2,1,3,1]
**Output:** 4
**Explanation:** One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.
```

**Example 2:**

```

**Input:** nums = [1,2,3,4]
**Output:** 3
**Explanation:** We can prove the optimal perm is [2,3,4,1].
At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.maximizeGreatness(**[[1, 3, 5, 2, 1, 3, 1]]) == 4
assert my_solution.maximizeGreatness(**[[1, 2, 3, 4]]) == 3
assert my_solution.maximizeGreatness(**[[31, 25, 72, 79]]) == 3
assert my_solution.maximizeGreatness(**[[65, 84, 91, 18, 59, 27, 9, 81, 33, 17]]) == 9
assert my_solution.maximizeGreatness(**[[42, 8, 75, 28, 35, 21, 13, 21]]) == 6
assert my_solution.maximizeGreatness(**[[35, 52, 74, 92, 25, 65, 77, 1, 73]]) == 8
assert my_solution.maximizeGreatness(**[[43, 68, 8, 100]]) == 3
assert my_solution.maximizeGreatness(**[[14, 88, 42, 53, 98, 69, 64, 40, 60, 23]]) == 9
assert my_solution.maximizeGreatness(**[[21]]) == 0
assert my_solution.maximizeGreatness(**[[34, 99, 63, 23, 70, 18, 64, 12, 21, 21]]) == 8
assert my_solution.maximizeGreatness(**[[36, 58, 88, 58, 99, 26, 92, 91, 53, 10]]) == 8
assert my_solution.maximizeGreatness(**[[25, 20, 92]]) == 2
assert my_solution.maximizeGreatness(**[[63, 51, 65, 87, 6, 17, 32, 14, 42, 46]]) == 9
assert my_solution.maximizeGreatness(**[[43, 9, 75, 76, 25, 96, 46, 85, 19]]) == 8
assert my_solution.maximizeGreatness(**[[88, 2, 5, 24]]) == 3
assert my_solution.maximizeGreatness(**[[26, 76, 24, 96, 82, 97, 97, 72]]) == 6
assert my_solution.maximizeGreatness(**[[21, 77, 82, 30, 94]]) == 4
assert my_solution.maximizeGreatness(**[[76, 94, 51, 82, 3, 89, 52]]) == 6
assert my_solution.maximizeGreatness(**[[27, 59, 57, 97, 6, 46, 88, 41, 52]]) == 8
assert my_solution.maximizeGreatness(**[[4, 17, 2, 95, 6, 62]]) == 5


# Find Score of an Array After Marking All Elements

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

Starting with `score = 0`, apply the following algorithm:

* Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
* Add the value of the chosen integer to `score`.
* Mark **the chosen element and its two adjacent elements if they exist**.
* Repeat until all the array elements are marked.

Return *the score you get after applying the above algorithm*.

**Example 1:**

```

**Input:** nums = [2,1,3,4,5,2]
**Output:** 7
**Explanation:** We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2].
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2].
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2].
Our score is 1 + 2 + 4 = 7.

```

**Example 2:**

```

**Input:** nums = [2,3,5,1,3,2]
**Output:** 5
**Explanation:** We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2].
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [2,3,5,1,3,2].
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2].
Our score is 1 + 2 + 2 = 5.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Minimum Time to Repair Cars

You are given an integer array `ranks` representing the **ranks** of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank `r` can repair n cars in `r * n2` minutes.

You are also given an integer `cars` representing the total number of cars waiting in the garage to be repaired.

Return *the **minimum** time taken to repair all the cars.*

**Note:** All the mechanics can repair the cars simultaneously.

**Example 1:**

```

**Input:** ranks = [4,2,3,1], cars = 10
**Output:** 16
**Explanation:**
- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
It can be proved that the cars cannot be repaired in less than 16 minutes.

```

**Example 2:**

```

**Input:** ranks = [5,1,8], cars = 6
**Output:** 16
**Explanation:**
- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
It can be proved that the cars cannot be repaired in less than 16 minutes.

```

**Constraints:**

* `1 <= ranks.length <= 105`
* `1 <= ranks[i] <= 100`
* `1 <= cars <= 106`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def repairCars(self, ranks: List[int], cars: int) -> int:
```


# Count the Number of Vowel Strings in Range

You are given a **0-indexed** array of string `words` and two integers `left` and `right`.

A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

Return *the number of vowel strings* `words[i]` *where* `i` *belongs to the inclusive range* `[left, right]`.

**Example 1:**

```

**Input:** words = ["are","amy","u"], left = 0, right = 2
**Output:** 2
**Explanation:**
- "are" is a vowel string because it starts with 'a' and ends with 'e'.
- "amy" is not a vowel string because it does not end with a vowel.
- "u" is a vowel string because it starts with 'u' and ends with 'u'.
The number of vowel strings in the mentioned range is 2.

```

**Example 2:**

```

**Input:** words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
**Output:** 3
**Explanation:**
- "aeo" is a vowel string because it starts with 'a' and ends with 'o'.
- "mu" is not a vowel string because it does not start with a vowel.
- "ooo" is a vowel string because it starts with 'o' and ends with 'o'.
- "artro" is a vowel string because it starts with 'a' and ends with 'o'.
The number of vowel strings in the mentioned range is 3.

```

**Constraints:**

* `1 <= words.length <= 1000`
* `1 <= words[i].length <= 10`
* `words[i]` consists of only lowercase English letters.
* `0 <= left <= right < words.length`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
```

my_solution = Solution()

assert my_solution.vowelStrings(**[['are', 'amy', 'u'], 0, 2]) == 2
assert my_solution.vowelStrings(**[['hey', 'aeo', 'mu', 'ooo', 'artro'], 1, 4]) == 3
assert my_solution.vowelStrings(**[['si'], 0, 0]) == 0
assert my_solution.vowelStrings(**[['ce', 'ai'], 1, 1]) == 1
assert my_solution.vowelStrings(**[['n', 'a', 'o'], 0, 2]) == 2
assert my_solution.vowelStrings(**[['v', 'ab', 'fu', 'nu'], 1, 3]) == 0
assert my_solution.vowelStrings(**[['vo', 'j', 'i', 's', 'i'], 0, 3]) == 1
assert my_solution.vowelStrings(**[['o'], 0, 0]) == 1
assert my_solution.vowelStrings(**[['e', 'ey'], 0, 1]) == 1
assert my_solution.vowelStrings(**[['m', 'qi', 'ae'], 1, 1]) == 0
assert my_solution.vowelStrings(**[['p', 'o', 'ud', 'eq'], 0, 0]) == 0
assert my_solution.vowelStrings(**[['e', 'a', 'o', 'i', 'u'], 0, 0]) == 1
assert my_solution.vowelStrings(**[['f'], 0, 0]) == 0
assert my_solution.vowelStrings(**[['i', 'cz'], 0, 1]) == 1
assert my_solution.vowelStrings(**[['a', 'ui', 'ap'], 1, 2]) == 1
assert my_solution.vowelStrings(**[['r', 'sa', 'oa', 'v'], 2, 3]) == 1
assert my_solution.vowelStrings(**[['ez', 'i', 'z', 'ka', 'oi'], 0, 2]) == 1
assert my_solution.vowelStrings(**[['p', 'b'], 0, 1]) == 0
assert my_solution.vowelStrings(**[['gs', 'dt', 'v'], 0, 2]) == 0
assert my_solution.vowelStrings(**[['i', 'or', 'au', 'ii'], 1, 3]) == 2


# Rearrange Array to Maximize Prefix Score

You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order).

Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearranging it. The **score** of `nums` is the number of positive integers in the array `prefix`.

Return *the maximum score you can achieve*.

**Example 1:**

```

**Input:** nums = [2,-1,0,1,-3,3,-3]
**Output:** 6
**Explanation:** We can rearrange the array into nums = [2,3,1,-1,-3,0,-3].
prefix = [2,5,6,5,2,2,-1], so the score is 6.
It can be shown that 6 is the maximum score we can obtain.

```

**Example 2:**

```

**Input:** nums = [-2,-3,0]
**Output:** 0
**Explanation:** Any rearrangement of the array will result in a score of 0.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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


# Count the Number of Beautiful Subarrays

You are given a **0-indexed** integer array `nums`. In one operation, you can:

* Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`.
* Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`.
* Subtract `2k` from `nums[i]` and `nums[j]`.

A subarray is **beautiful** if it is possible to make all of its elements equal to `0` after applying the above operation any number of times.

Return *the number of **beautiful subarrays** in the array* `nums`.

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

**Example 1:**

```

**Input:** nums = [4,3,1,2,4]
**Output:** 2
**Explanation:** There are 2 beautiful subarrays in nums: [4,3,1,2,4] and [4,3,1,2,4].
- We can make all elements in the subarray [3,1,2] equal to 0 in the following way:
  - Choose [3, 1, 2] and k = 1. Subtract 21 from both numbers. The subarray becomes [1, 1, 0].
  - Choose [1, 1, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 0, 0].
- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:
  - Choose [4, 3, 1, 2, 4] and k = 2. Subtract 22 from both numbers. The subarray becomes [0, 3, 1, 2, 0].
  - Choose [0, 3, 1, 2, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 2, 0, 2, 0].
  - Choose [0, 2, 0, 2, 0] and k = 1. Subtract 21 from both numbers. The subarray becomes [0, 0, 0, 0, 0].

```

**Example 2:**

```

**Input:** nums = [1,10,4]
**Output:** 0
**Explanation:** There are no beautiful subarrays in nums.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

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

my_solution = Solution()

assert my_solution.beautifulSubarrays(**[[4, 3, 1, 2, 4]]) == 2
assert my_solution.beautifulSubarrays(**[[1, 10, 4]]) == 0
assert my_solution.beautifulSubarrays(**[[0]]) == 1
assert my_solution.beautifulSubarrays(**[[1]]) == 0
assert my_solution.beautifulSubarrays(**[[0, 0]]) == 3
assert my_solution.beautifulSubarrays(**[[0, 1]]) == 1
assert my_solution.beautifulSubarrays(**[[1, 0]]) == 1
assert my_solution.beautifulSubarrays(**[[1, 1]]) == 1
assert my_solution.beautifulSubarrays(**[[0, 0, 0]]) == 6
assert my_solution.beautifulSubarrays(**[[1, 0, 0]]) == 3
assert my_solution.beautifulSubarrays(**[[0, 1, 0]]) == 2
assert my_solution.beautifulSubarrays(**[[1, 1, 0]]) == 3
assert my_solution.beautifulSubarrays(**[[0, 0, 1]]) == 3
assert my_solution.beautifulSubarrays(**[[1, 0, 1]]) == 2
assert my_solution.beautifulSubarrays(**[[0, 1, 1]]) == 3
assert my_solution.beautifulSubarrays(**[[1, 1, 1]]) == 2
assert my_solution.beautifulSubarrays(**[[0, 0, 0, 0]]) == 10
assert my_solution.beautifulSubarrays(**[[1, 0, 0, 0]]) == 6
assert my_solution.beautifulSubarrays(**[[0, 1, 0, 0]]) == 4
assert my_solution.beautifulSubarrays(**[[1, 1, 0, 0]]) == 6