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 29, 2025 17:07
Count Subarrays Where Max Element Appears at Least K Times

Question

Approach

I iterated through the array with a right pointer and maintained a left pointer to denote the start of the current window. As I moved the right pointer, I tracked how many times the current max value appeared in the window.

Each time the count of the max value reached at least k, I knew that all subarrays ending at this right and starting from any index ≤ left would be valid. So I added (left + 1) to the result.

If the count of the max fell below k, I shrank the window from the left until the condition was satisfied again.

@Ifihan
Ifihan / main.md
Created April 28, 2025 21:36
Count Subarrays With Score Less Than K
@Ifihan
Ifihan / main.md
Created April 27, 2025 22:30
Count Subarrays of Length Three With a Condition

Question

Approach

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.

Implementation

@Ifihan
Ifihan / main.md
Created April 26, 2025 22:09
Count Subarrays With Fixed Bounds

Question

Approach

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:

  • The last index where the number was out of range (out_idx), meaning it was less than minK or greater than maxK.
  • The last index where I saw minK (min_idx).
  • The last index where I saw maxK (max_idx).
@Ifihan
Ifihan / maind.md
Created April 25, 2025 21:43
Count of Interesting Subarrays

Question

Approach

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.

Implementation

class Solution:
    def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
@Ifihan
Ifihan / main.md
Created April 24, 2025 22:29
Count Complete Subarrays in an Array

Question

Approach

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).

@Ifihan
Ifihan / main.md
Created April 23, 2025 19:07
Count Largest Group

Question

Approach

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.

Implementation

@Ifihan
Ifihan / main.md
Created April 22, 2025 19:16
Count the Number of Ideal Arrays
@Ifihan
Ifihan / main.md
Created April 21, 2025 22:38
Count the Hidden Sequences

Question

Approach

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

@Ifihan
Ifihan / main.md
Created April 20, 2025 22:24
Rabbits in Forest

Question

Approach

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:

  • I grouped the rabbits in groups of size x + 1.
  • The number of such groups is the ceiling of count / (x + 1).
  • Each group adds x + 1 rabbits to the total.