Skip to content

Instantly share code, notes, and snippets.

View Ifihan's full-sized avatar
🔧
Work in Progress

Ifihanagbara Olusheye Ifihan

🔧
Work in Progress
View GitHub Profile
@Ifihan
Ifihan / main.md
Created April 19, 2025 19:28
Count the Number of Fair Pairs

Question

Approach

Implementation

from bisect import bisect_left, bisect_right

class Solution:
 def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:
@Ifihan
Ifihan / main.md
Created April 18, 2025 22:47
Count and Say

Question

Approach

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.

Implementation

@Ifihan
Ifihan / main.md
Created April 17, 2025 21:17
Count Equal and Divisible Pairs in an Array

Question

Approach

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.

Implementation

class Solution:
    def countPairs(self, nums: List[int], k: int) -> int:
 n = len(nums)
@Ifihan
Ifihan / main.md
Created April 16, 2025 22:17
Count the Number of Good Subarrays

Question

Approach

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.

@Ifihan
Ifihan / main.md
Created April 15, 2025 22:38
Count Good Triplets in an Array

Question

Approach

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

@Ifihan
Ifihan / main.md
Created April 14, 2025 21:33
Count Good Triplets

Question

Approach

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.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created April 13, 2025 21:47
Count Good Numbers

Question

Approach

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.

Implementation

class Solution:
 def countGoodNumbers(self, n: int) -&gt; int:
@Ifihan
Ifihan / main.md
Created April 12, 2025 22:36
Find the Count of Good Integers
@Ifihan
Ifihan / main.md
Created April 11, 2025 22:45
Count Symmetric Integers

Question

Approach

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.

Implementation

class Solution:
 def countSymmetricIntegers(self, low: int, high: int) -&gt; int:
@Ifihan
Ifihan / main.md
Created April 10, 2025 21:34
Count the Number of Powerful Integers

Question

Approach

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.

image