from bisect import bisect_left, bisect_right
class Solution:
def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:
I started with the base case: "1". Then for each step from 2 to n, I generated the next term by reading the previous string and counting consecutive identical digits. For each run of characters, I appended the count followed by the digit to build the next term. I repeated this until I reached the nth term.
I went through all possible pairs (i, j) where i < j, and for each pair, I checked if the elements at those indices were equal. If they were, I then checked whether i * j was divisible by k. If both conditions were true, I counted that pair as valid. After checking all pairs, I returned the total count.
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n = len(nums)
I started with two pointers — left and right — and used a sliding window approach. As I moved the right pointer forward, I kept track of how many pairs I could form with each number using a frequency map. Every time I added a number to the window, I updated the pair count.
When the number of pairs in the current window reached or exceeded k, I knew that all subarrays starting at left and ending at or after right would be good. So I counted those subarrays as len(nums) - right.
To optimize further, I then shrank the window from the left while still maintaining the condition of having at least k pairs. I continued this process until I had counted all possible good subarrays.
I first mapped every number in nums2
to its index, so I could easily find the position of any number. Then, I created a new array by replacing each value in nums1
with its index from nums2
. This way, I transformed the problem into counting the number of increasing triplets in this new array.
Next, I needed an efficient way to count how many smaller elements appeared before each index, and how many larger elements appeared after. To do that, I used a Fenwick Tree (or Binary Indexed Tree).
I scanned the array from left to right to count the number of smaller elements before each position, and then from right to left to count the number of greater elements after each position. Finally, for each index acting as the "middle" of the triplet, I multiplied the number of smaller elements before it with the number of greater elements after it, and summed all these products to get
To solve this problem, I looped through every possible triplet of indices (i < j < k), but I made sure to add some early checks to avoid unnecessary computations. First, I checked whether (|arr[i] - arr[j]|) was less than or equal to a
. If not, I skipped checking for k
entirely, since the triplet would never be valid. Only when that condition passed did I move on to the innermost loop for k
, where I checked the remaining two conditions: (|arr[j] - arr[k]| \leq b) and (|arr[i] - arr[k]| \leq c) before counting the triplet.
class Solution:
I figured out how many positions in the string need even digits and how many need prime digits. I then raised 5 to the power of even positions and 4 to the power of odd positions and multiplied them. Since n can be very large, I used fast exponentiation to keep the computation efficient and within the modulo range.
class Solution:
def countGoodNumbers(self, n: int) -> int:
I looped from low to high and, for each number, converted it to a string to split the digits easily. If the length was even, I compared the sum of the first half with the second half. If they matched, I counted it as a symmetric integer. I returned the total count at the end.
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
I tired two approaches. First was brute foce (first check for the powerful integer, check if it's greater than the limit, then check if it's a suffix. This works but it doesn't scale. Then the next method I came up with was using a Queue and BFS for "prepend" and ge the candidate numbers and check. That didn't pass all testcases so I just ran to the editorial to see what they have and it was pretty interesting. Combination was my go to.
