Used editiorial today for the Hard questions.

Used editiorial today for the Hard questions.
When solving this, I noticed that I only needed to check all subarrays of length 3. So I iterated over the array, looking at every group of three consecutive numbers. For each triplet, I checked if the sum of the first and third numbers was exactly half of the second number. If the condition was satisfied, I incremented my answer counter.
When solving this, I realized I needed to find subarrays where the minimum is exactly minK
and the maximum is exactly maxK
.
To do this efficiently, I tracked three things while scanning nums
:
out_idx
), meaning it was less than minK
or greater than maxK
.min_idx
).max_idx
).To solve the problem, I kept track of how many numbers so far satisfy nums[i] % modulo == k
using a prefix count. At each position, I calculated the current prefix modulo and checked how many previous prefix sums would form an interesting subarray ending at the current index. I used a hashmap to record the frequency of each prefix modulo. By rearranging the modulo condition, I quickly found how many matches existed without checking every subarray. I updated the answer at each step and continued this process across the array.
class Solution:
def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
First, I computed the number of distinct elements in the entire array. This gave me the target count that each "complete" subarray must satisfy.
Then, I used a sliding window approach: I moved the left and right pointers to track subarrays, using a hash map to count the frequency of elements in the current window. Each time the number of distinct elements in the window matched the total distinct count, I knew that all subarrays starting from the current left and ending anywhere from the right to the end would also be valid, so I counted them accordingly.
To simplify the problem even more, I actually iterated over all subarrays and used a set to track distinct elements within each subarray (acceptable since n ≤ 1000).
I grouped each number from 1 to n
based on the sum of its digits. I used a dictionary to keep track of how many numbers belonged to each digit-sum group. For each number in the range, I calculated the digit sum by converting the number to a string, summing the integer values of its characters, and incremented the count in the corresponding group.
After processing all numbers, I looked at the values in the dictionary to determine the size of the largest group — this was just the maximum frequency among all groups. Then, I counted how many groups had this maximum size. That gave me the number of groups with the largest size, which I returned as the final answer.
I started with an initial number x
, each subsequent number in the sequence is just x
plus the running total (or prefix sum) of the differences. So, the sequence becomes x
, x + diff1
, x + diff1 + diff2
, and so on.
To ensure the entire hidden sequence stays within the bounds of [lower, upper]
, I tracked the minimum and maximum values that the prefix sums could reach. These represent how low or high the hidden sequence can potentially go relative to the starting number. To guarantee that the entire sequence remains within the valid range, the starting number x
must be large enough to lift the lowest point above lower
, and small enough to keep the highest point below upper
.
So I calculated the valid range of starting values using those bounds. The number of valid sequences then came down to counting how many integers x
satisfy that condition. If the range wa
I started by counting how many times each answer appeared. Each answer x
means the rabbit believes there are x + 1
rabbits of the same color (including itself). Multiple rabbits can share the same group only if they don’t exceed x + 1
in total.
So, for each unique answer x
:
x + 1
.count / (x + 1)
.x + 1
rabbits to the total.from bisect import bisect_left, bisect_right
class Solution:
def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int: