bowang0911/leetcode huggingface dataset:
Largest Positive Integer That Exists With Its Negative

Given an integer array `nums` that **does not contain** any zeros, find **the largest positive** integer `k` such that `-k` also exists in the array.

Return _the positive integer_ `k`. If there is no such integer, return `-1`.

**Example 1:**

**Input:** nums = \[-1,2,-3,3\]
**Output:** 3
**Explanation:** 3 is the only valid k we can find in the array.

**Example 2:**

**Input:** nums = \[-1,10,6,7,-7,1\]
**Output:** 7
**Explanation:** Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.

**Example 3:**

**Input:** nums = \[-10,8,6,7,-2,-3\]
**Output:** -1
**Explanation:** There is no a single valid k, we return -1.

**Constraints:**

*   `1 <= nums.length <= 1000`
*   `-1000 <= nums[i] <= 1000`
*   `nums[i] != 0`
Count Number of Distinct Integers After Reverse Operations

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

You have to take each integer in the array, **reverse its digits**, and add it to the end of the array. You should apply this operation to the original integers in `nums`.

Return _the number of **distinct** integers in the final array_.

**Example 1:**

**Input:** nums = \[1,13,10,12,31\]
**Output:** 6
**Explanation:** After including the reverse of each number, the resulting array is \[1,13,10,12,31,1,31,1,21,13\].
The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1.
The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).

**Example 2:**

**Input:** nums = \[2,2,2\]
**Output:** 1
**Explanation:** After including the reverse of each number, the resulting array is \[2,2,2,2,2,2\].
The number of distinct integers in this array is 1 (The number 2).

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 106`
Sum of Number and Its Reverse

Given a **non-negative** integer `num`, return `true` _if_ `num` _can be expressed as the sum of any **non-negative** integer and its reverse, or_ `false` _otherwise._

**Example 1:**

**Input:** num = 443
**Output:** true
**Explanation:** 172 + 271 = 443 so we return true.

**Example 2:**

**Input:** num = 63
**Output:** false
**Explanation:** 63 cannot be expressed as the sum of a non-negative integer and its reverse so we return false.

**Example 3:**

**Input:** num = 181
**Output:** true
**Explanation:** 140 + 041 = 181 so we return true. Note that when a number is reversed, there may be leading zeros.

**Constraints:**

*   `0 <= num <= 105`
Count Subarrays With Fixed Bounds

You are given an integer array `nums` and two integers `minK` and `maxK`.

A **fixed-bound subarray** of `nums` is a subarray that satisfies the following conditions:

*   The **minimum** value in the subarray is equal to `minK`.
*   The **maximum** value in the subarray is equal to `maxK`.

Return _the **number** of fixed-bound subarrays_.

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

**Example 1:**

**Input:** nums = \[1,3,5,2,7,5\], minK = 1, maxK = 5
**Output:** 2
**Explanation:** The fixed-bound subarrays are \[1,3,5\] and \[1,3,5,2\].

**Example 2:**

**Input:** nums = \[1,1,1,1\], minK = 1, maxK = 1
**Output:** 10
**Explanation:** Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.

**Constraints:**

*   `2 <= nums.length <= 105`
*   `1 <= nums[i], minK, maxK <= 106`
Number of Nodes With Value One

There is an **undirected** connected tree with `n` nodes labeled from `1` to `n` and `n - 1` edges. You are given the integer `n`. The parent node of a node with a label `v` is the node with the label `floor (v / 2)`. The root of the tree is the node with the label `1`.

*   For example, if `n = 7`, then the node with the label `3` has the node with the label `floor(3 / 2) = 1` as its parent, and the node with the label `7` has the node with the label `floor(7 / 2) = 3` as its parent.

You are also given an integer array `queries`. Initially, every node has a value `0` on it. For each query `queries[i]`, you should flip all values in the subtree of the node with the label `queries[i]`.

Return _the total number of nodes with the value_ `1` _**after processing all the queries**_.

**Note** that:

*   Flipping the value of a node means that the node with the value `0` becomes `1` and vice versa.
*   `floor(x)` is equivalent to rounding `x` down to the nearest integer.

**Example 1:**

**Input:** n = 5 , queries = \[1,2,5\]
**Output:** 3
**Explanation:** The diagram above shows the tree structure and its status after performing the queries. The blue node represents the value 0, and the red node represents the value 1.
After processing the queries, there are three red nodes (nodes with value 1): 1, 3, and 5.

**Example 2:**

**Input:** n = 3, queries = \[2,3,3\]
**Output:** 1
**Explanation:** The diagram above shows the tree structure and its status after performing the queries. The blue node represents the value 0, and the red node represents the value 1.
After processing the queries, there are one red node (node with value 1): 2.

**Constraints:**

*   `1 <= n <= 105`
*   `1 <= queries.length <= 105`
*   `1 <= queries[i] <= n`
Determine if Two Events Have Conflict

You are given two arrays of strings that represent two inclusive events that happened **on the same day**, `event1` and `event2`, where:

*   `event1 = [startTime1, endTime1]` and
*   `event2 = [startTime2, endTime2]`.

Event times are valid 24 hours format in the form of `HH:MM`.

A **conflict** happens when two events have some non-empty intersection (i.e., some moment is common to both events).

Return `true` _if there is a conflict between two events. Otherwise, return_ `false`.

**Example 1:**

**Input:** event1 = \[ "01:15 ", "02:00 "\], event2 = \[ "02:00 ", "03:00 "\]
**Output:** true
**Explanation:** The two events intersect at time 2:00.

**Example 2:**

**Input:** event1 = \[ "01:00 ", "02:00 "\], event2 = \[ "01:20 ", "03:00 "\]
**Output:** true
**Explanation:** The two events intersect starting from 01:20 to 02:00.

**Example 3:**

**Input:** event1 = \[ "10:00 ", "11:00 "\], event2 = \[ "14:00 ", "15:00 "\]
**Output:** false
**Explanation:** The two events do not intersect.

**Constraints:**

*   `evnet1.length == event2.length == 2.`
*   `event1[i].length == event2[i].length == 5`
*   `startTime1 <= endTime1`
*   `startTime2 <= endTime2`
*   All the event times follow the `HH:MM` format.
Number of Subarrays With GCD Equal to K

Given an integer array `nums` and an integer `k`, return _the number of **subarrays** of_ `nums` _where the greatest common divisor of the subarray's elements is_ `k`.

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

The **greatest common divisor of an array** is the largest integer that evenly divides all the array elements.

**Example 1:**

**Input:** nums = \[9,3,1,2,6,3\], k = 3
**Output:** 4
**Explanation:** The subarrays of nums where 3 is the greatest common divisor of all the subarray's elements are:
- \[9,**3**,1,2,6,3\]
- \[9,3,1,2,6,**3**\]
- \[**9,3**,1,2,6,3\]
- \[9,3,1,2,**6,3**\]

**Example 2:**

**Input:** nums = \[4\], k = 7
**Output:** 0
**Explanation:** There are no subarrays of nums where 7 is the greatest common divisor of all the subarray's elements.

**Constraints:**

*   `1 <= nums.length <= 1000`
*   `1 <= nums[i], k <= 109`
Minimum Cost to Make Array Equal

You are given two **0-indexed** arrays `nums` and `cost` consisting each of `n` **positive** integers.

You can do the following operation **any** number of times:

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

The cost of doing one operation on the `ith` element is `cost[i]`.

Return _the **minimum** total cost such that all the elements of the array_ `nums` _become **equal**_.

**Example 1:**

**Input:** nums = \[1,3,5,2\], cost = \[2,3,1,14\]
**Output:** 8
**Explanation:** We can make all the elements equal to 2 in the following way:
- Increase the 0th element one time. The cost is 2.
- Decrease the 1st element one time. The cost is 3.
- Decrease the 2nd element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.

**Example 2:**

**Input:** nums = \[2,2,2,2,2\], cost = \[4,2,8,1,3\]
**Output:** 0
**Explanation:** All the elements are already equal, so no operations are needed.

**Constraints:**

*   `n == nums.length == cost.length`
*   `1 <= n <= 105`
*   `1 <= nums[i], cost[i] <= 106`
Minimum Number of Operations to Make Arrays Similar

You are given two positive integer arrays `nums` and `target`, of the same length.

In one operation, you can choose any two **distinct** indices `i` and `j` where `0 <= i, j < nums.length` and:

*   set `nums[i] = nums[i] + 2` and
*   set `nums[j] = nums[j] - 2`.

Two arrays are considered to be **similar** if the frequency of each element is the same.

Return _the minimum number of operations required to make_ `nums` _similar to_ `target`. The test cases are generated such that `nums` can always be similar to `target`.

**Example 1:**

**Input:** nums = \[8,12,6\], target = \[2,14,10\]
**Output:** 2
**Explanation:** It is possible to make nums similar to target in two operations:
- Choose i = 0 and j = 2, nums = \[10,12,4\].
- Choose i = 1 and j = 2, nums = \[10,14,2\].
It can be shown that 2 is the minimum number of operations needed.

**Example 2:**

**Input:** nums = \[1,2,5\], target = \[4,1,3\]
**Output:** 1
**Explanation:** We can make nums similar to target in one operation:
- Choose i = 1 and j = 2, nums = \[1,4,3\].

**Example 3:**

**Input:** nums = \[1,1,1,1,1\], target = \[1,1,1,1,1\]
**Output:** 0
**Explanation:** The array nums is already similiar to target.

**Constraints:**

*   `n == nums.length == target.length`
*   `1 <= n <= 105`
*   `1 <= nums[i], target[i] <= 106`
*   It is possible to make `nums` similar to `target`.
Number of Distinct Binary Strings After Applying Operations

You are given a **binary** string `s` and a positive integer `k`.

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

*   Choose any substring of size `k` from `s` and **flip** all its characters, that is, turn all `1`'s into `0`'s, and all `0`'s into `1`'s.

Return _the number of **distinct** strings you can obtain_. Since the answer may be too large, return it **modulo** `109 + 7`.

**Note** that:

*   A binary string is a string that consists **only** of the characters `0` and `1`.
*   A substring is a contiguous part of a string.

**Example 1:**

**Input:** s =  "1001 ", k = 3
**Output:** 4
**Explanation:** We can obtain the following strings:
- Applying no operation on the string gives s =  "1001 ".
- Applying one operation on the substring starting at index 0 gives s =  "**011**1 ".
- Applying one operation on the substring starting at index 1 gives s =  "1**110** ".
- Applying one operation on both the substrings starting at indices 0 and 1 gives s =  "**0000** ".
It can be shown that we cannot obtain any other string, so the answer is 4.

**Example 2:**

**Input:** s =  "10110 ", k = 5
**Output:** 2
**Explanation:** We can obtain the following strings:
- Applying no operation on the string gives s =  "10110 ".
- Applying one operation on the whole string gives s =  "01001 ".
It can be shown that we cannot obtain any other string, so the answer is 2.

**Constraints:**

*   `1 <= k <= s.length <= 105`
*   `s[i]` is either `0` or `1`.
Odd String Difference

You are given an array of equal-length strings `words`. Assume that the length of each string is `n`.

Each string `words[i]` can be converted into a **difference integer array** `difference[i]` of length `n - 1` where `difference[i][j] = words[i][j+1] - words[i][j]` where `0 <= j <= n - 2`. Note that the difference between two letters is the difference between their **positions** in the alphabet i.e. the position of `'a'` is `0`, `'b'` is `1`, and `'z'` is `25`.

*   For example, for the string `"acb "`, the difference integer array is `[2 - 0, 1 - 2] = [2, -1]`.

All the strings in words have the same difference integer array, **except one**. You should find that string.

Return _the string in_ `words` _that has different **difference integer array**._

**Example 1:**

**Input:** words = \[ "adc ", "wzy ", "abc "\]
**Output:**  "abc "
**Explanation:** 
- The difference integer array of  "adc " is \[3 - 0, 2 - 3\] = \[3, -1\].
- The difference integer array of  "wzy " is \[25 - 22, 24 - 25\]= \[3, -1\].
- The difference integer array of  "abc " is \[1 - 0, 2 - 1\] = \[1, 1\]. 
The odd array out is \[1, 1\], so we return the corresponding string,  "abc ".

**Example 2:**

**Input:** words = \[ "aaa ", "bob ", "ccc ", "ddd "\]
**Output:**  "bob "
**Explanation:** All the integer arrays are \[0, 0\] except for  "bob ", which corresponds to \[13, -13\].

**Constraints:**

*   `3 <= words.length <= 100`
*   `n == words[i].length`
*   `2 <= n <= 20`
*   `words[i]` consists of lowercase English letters.
Words Within Two Edits of Dictionary

You are given two string arrays, `queries` and `dictionary`. All words in each array comprise of lowercase English letters and have the same length.

In one **edit** you can take a word from `queries`, and change any letter in it to any other letter. Find all words from `queries` that, after a **maximum** of two edits, equal some word from `dictionary`.

Return _a list of all words from_ `queries`_,_ _that match with some word from_ `dictionary` _after a maximum of **two edits**_. Return the words in the **same order** they appear in `queries`.

**Example 1:**

**Input:** queries = \[ "word ", "note ", "ants ", "wood "\], dictionary = \[ "wood ", "joke ", "moat "\]
**Output:** \[ "word ", "note ", "wood "\]
**Explanation:**
- Changing the 'r' in  "word " to 'o' allows it to equal the dictionary word  "wood ".
- Changing the 'n' to 'j' and the 't' to 'k' in  "note " changes it to  "joke ".
- It would take more than 2 edits for  "ants " to equal a dictionary word.
-  "wood " can remain unchanged (0 edits) and match the corresponding dictionary word.
Thus, we return \[ "word ", "note ", "wood "\].

**Example 2:**

**Input:** queries = \[ "yes "\], dictionary = \[ "not "\]
**Output:** \[\]
**Explanation:**
Applying any two edits to  "yes " cannot make it equal to  "not ". Thus, we return an empty array.

**Constraints:**

*   `1 <= queries.length, dictionary.length <= 100`
*   `n == queries[i].length == dictionary[j].length`
*   `1 <= n <= 100`
*   All `queries[i]` and `dictionary[j]` are composed of lowercase English letters.
Destroy Sequential Targets

You are given a **0-indexed** array `nums` consisting of positive integers, representing targets on a number line. You are also given an integer `space`.

You have a machine which can destroy targets. **Seeding** the machine with some `nums[i]` allows it to destroy all targets with values that can be represented as `nums[i] + c * space`, where `c` is any non-negative integer. You want to destroy the **maximum** number of targets in `nums`.

Return _the **minimum value** of_ `nums[i]` _you can seed the machine with to destroy the maximum number of targets._

**Example 1:**

**Input:** nums = \[3,7,8,1,1,5\], space = 2
**Output:** 1
**Explanation:** If we seed the machine with nums\[3\], then we destroy all targets equal to 1,3,5,7,9,... 
In this case, we would destroy 5 total targets (all except for nums\[2\]). 
It is impossible to destroy more than 5 targets, so we return nums\[3\].

**Example 2:**

**Input:** nums = \[1,3,5,2,4,6\], space = 2
**Output:** 1
**Explanation:** Seeding the machine with nums\[0\], or nums\[3\] destroys 3 targets. 
It is not possible to destroy more than 3 targets.
Since nums\[0\] is the minimal integer that can destroy 3 targets, we return 1.

**Example 3:**

**Input:** nums = \[6,2,5\], space = 100
**Output:** 2
**Explanation:** Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums\[1\].

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 109`
*   `1 <= space <= 109`
Next Greater Element IV

You are given a **0-indexed** array of non-negative integers `nums`. For each integer in `nums`, you must find its respective **second greater** integer.

The **second greater** integer of `nums[i]` is `nums[j]` such that:

*   `j > i`
*   `nums[j] > nums[i]`
*   There exists **exactly one** index `k` such that `nums[k] > nums[i]` and `i < k < j`.

If there is no such `nums[j]`, the second greater integer is considered to be `-1`.

*   For example, in the array `[1, 2, 4, 3]`, the second greater integer of `1` is `4`, `2` is `3`, and that of `3` and `4` is `-1`.

Return _an integer array_ `answer`_, where_ `answer[i]` _is the second greater integer of_ `nums[i]`_._

**Example 1:**

**Input:** nums = \[2,4,0,9,6\]
**Output:** \[9,6,6,-1,-1\]
**Explanation:**
0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
Thus, we return \[9,6,6,-1,-1\].

**Example 2:**

**Input:** nums = \[3,3\]
**Output:** \[-1,-1\]
**Explanation:**
We return \[-1,-1\] since neither integer has any integer greater than it.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 109`
Average Value of Even Numbers That Are Divisible by Three

Given an integer array `nums` of **positive** integers, return _the average value of all even integers that are divisible by_ `3`_._

Note that the **average** of `n` elements is the **sum** of the `n` elements divided by `n` and **rounded down** to the nearest integer.

**Example 1:**

**Input:** nums = \[1,3,6,10,12,15\]
**Output:** 9
**Explanation:** 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.

**Example 2:**

**Input:** nums = \[1,2,4,7,10\]
**Output:** 0
**Explanation:** There is no single number that satisfies the requirement, so return 0.

**Constraints:**

*   `1 <= nums.length <= 1000`
*   `1 <= nums[i] <= 1000`
Most Popular Video Creator

You are given two string arrays `creators` and `ids`, and an integer array `views`, all of length `n`. The `ith` video on a platform was created by `creator[i]`, has an id of `ids[i]`, and has `views[i]` views.

The **popularity** of a creator is the **sum** of the number of views on **all** of the creator's videos. Find the creator with the **highest** popularity and the id of their **most** viewed video.

*   If multiple creators have the highest popularity, find all of them.
*   If multiple videos have the highest view count for a creator, find the lexicographically **smallest** id.

Return _a 2D array of strings_ `answer` _where_ `answer[i] = [creatori, idi]` _means that_ `creatori` _has the **highest** popularity and_ `idi` _is the id of their most popular video._ The answer can be returned in any order.

**Example 1:**

**Input:** creators = \[ "alice ", "bob ", "alice ", "chris "\], ids = \[ "one ", "two ", "three ", "four "\], views = \[5,10,5,4\]
**Output:** \[\[ "alice ", "one "\],\[ "bob ", "two "\]\]
**Explanation:**
The popularity of alice is 5 + 5 = 10.
The popularity of bob is 10.
The popularity of chris is 4.
alice and bob are the most popular creators.
For bob, the video with the highest view count is  "two ".
For alice, the videos with the highest view count are  "one " and  "three ". Since  "one " is lexicographically smaller than  "three ", it is included in the answer.

**Example 2:**

**Input:** creators = \[ "alice ", "alice ", "alice "\], ids = \[ "a ", "b ", "c "\], views = \[1,2,2\]
**Output:** \[\[ "alice ", "b "\]\]
**Explanation:**
The videos with id  "b " and  "c " have the highest view count.
Since  "b " is lexicographically smaller than  "c ", it is included in the answer.

**Constraints:**

*   `n == creators.length == ids.length == views.length`
*   `1 <= n <= 105`
*   `1 <= creators[i].length, ids[i].length <= 5`
*   `creators[i]` and `ids[i]` consist only of lowercase English letters.
*   `0 <= views[i] <= 105`
Minimum Addition to Make Integer Beautiful

You are given two positive integers `n` and `target`.

An integer is considered **beautiful** if the sum of its digits is less than or equal to `target`.

Return the _minimum **non-negative** integer_ `x` _such that_ `n + x` _is beautiful_. The input will be generated such that it is always possible to make `n` beautiful.

**Example 1:**

**Input:** n = 16, target = 6
**Output:** 4
**Explanation:** Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.

**Example 2:**

**Input:** n = 467, target = 6
**Output:** 33
**Explanation:** Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.

**Example 3:**

**Input:** n = 1, target = 1
**Output:** 0
**Explanation:** Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.

**Constraints:**

*   `1 <= n <= 1012`
*   `1 <= target <= 150`
*   The input will be generated such that it is always possible to make `n` beautiful.
Height of Binary Tree After Subtree Removal Queries

You are given the `root` of a **binary tree** with `n` nodes. Each node is assigned a unique value from `1` to `n`. You are also given an array `queries` of size `m`.

You have to perform `m` **independent** queries on the tree where in the `ith` query you do the following:

*   **Remove** the subtree rooted at the node with the value `queries[i]` from the tree. It is **guaranteed** that `queries[i]` will **not** be equal to the value of the root.

Return _an array_ `answer` _of size_ `m` _where_ `answer[i]` _is the height of the tree after performing the_ `ith` _query_.

**Note**:

*   The queries are independent, so the tree returns to its **initial** state after each query.
*   The height of a tree is the **number of edges in the longest simple path** from the root to some node in the tree.

**Example 1:**

**Input:** root = \[1,3,4,2,null,6,5,null,null,null,null,null,7\], queries = \[4\]
**Output:** \[2\]
**Explanation:** The diagram above shows the tree after removing the subtree rooted at node with value 4.
The height of the tree is 2 (The path 1 -> 3 -> 2).

**Example 2:**

**Input:** root = \[5,8,9,2,1,3,7,4,6\], queries = \[3,2,4,8\]
**Output:** \[3,2,3,2\]
**Explanation:** We have the following queries:
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).

**Constraints:**

*   The number of nodes in the tree is `n`.
*   `2 <= n <= 105`
*   `1 <= Node.val <= n`
*   All the values in the tree are **unique**.
*   `m == queries.length`
*   `1 <= m <= min(n, 104)`
*   `1 <= queries[i] <= n`
*   `queries[i] != root.val`