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 June 18, 2025 19:26
Divide Array Into Arrays With Max Difference

Question

Approach

I first sort the array nums so that elements that are closer in value are grouped together. After sorting, I iterate through the array in chunks of 3 elements since each group must contain exactly 3 numbers. For every group of three consecutive elements, I check if the difference between the maximum and minimum element in that group is less than or equal to k. If all such groups satisfy the condition, I collect them into the result. If I find any group where the difference exceeds k, I immediately return an empty array, since it’s impossible to divide nums as required.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 17, 2025 02:13
Count the Number of Arrays with K Matching Adjacent Elements
@Ifihan
Ifihan / main.md
Created June 17, 2025 02:05
Maximum Difference Between Increasing Elements

Question

Approach

I keep track of the smallest value seen so far as I iterate through the array. For each element, I check if it’s greater than this minimum value. If it is, I compute the difference and update the maximum difference found so far. If it’s not, I update the minimum value.

Implementation

@Ifihan
Ifihan / main.md
Created June 15, 2025 21:32
Max Difference You Can Get From Changing an Integer

Question

Approach

To solve this problem, I try to get the maximum and minimum numbers possible by remapping one digit in the original number. To get the maximum number, I find the first digit that isn’t 9 and replace all its occurrences with 9, maximizing the number. For the minimum number, I handle it carefully to avoid leading zeros: if the first digit isn’t 1, I replace all its occurrences with 1; otherwise, I look for the next digit (excluding 0 and 1) to replace with 0, minimizing the number while keeping it valid. After applying these two strategies independently, I compute the difference between the maximum and minimum numbers and return that value.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 14, 2025 03:09
Maximum Difference by Remapping a Digit

Question

Approach

I convert the number into a string so I can easily manipulate digits. To maximize the number, I choose the first digit that is not 9 and replace all occurrences of it with 9. This gives me the highest possible value. To minimize the number, I choose the first digit that is not 0 and replace all occurrences of it with 0.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 14, 2025 02:55
Maximum Difference by Remapping a Digit

Question

Approach

I start by sorting the array nums because closer numbers are more likely to form pairs with smaller differences. Then I binary search on the minimum maximum allowed difference (let’s call it maxDiff). For each maxDiff candidate, I greedily count how many disjoint pairs I can form such that each pair has a difference ≤ maxDiff. If I can form at least p such pairs, I reduce the search space. Otherwise, I increase it.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 12, 2025 21:31
Maximum Difference Between Adjacent Elements in a Circular Array

Question

Approach

Since the array is circular, I iterate through each element and compute the absolute difference between every pair of adjacent elements, including the last and the first. I keep track of the maximum difference encountered during this iteration.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 11, 2025 06:30
Maximum Difference Between Even and Odd Frequency II
@Ifihan
Ifihan / main.md
Created June 10, 2025 22:47
Maximum Difference Between Even and Odd Frequency I

Question

Approach

To solve this, I first count the frequency of each character in the string using a frequency map. Then, I look for characters that have odd frequencies and track the maximum among them, since I'm trying to maximize the result. Simultaneously, I look for characters that have even frequencies and track the minimum among them, because subtracting a smaller even count gives a larger difference. Finally, I return the difference between the maximum odd frequency and the minimum even frequency.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 9, 2025 22:46
K-th Smallest in Lexicographical Order