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 December 1, 2025 22:41
Maximum Running Time of N Computers

Question

Approach

I binary-search the maximum running time T. For a candidate T I check if the total available minutes (summing min(b, T) for every battery b) is at least n * T — because each computer needs T minutes and a single battery can contribute at most T minutes toward a given computer. If the check passes, T is feasible and I try larger values; otherwise I try smaller ones.

Implementation

class Solution:
    def maxRunTime(self, n: int, batteries: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created November 30, 2025 21:51
Make Sum Divisible by P

Question

Approach

I compute total % p = target. If target == 0 I return 0 immediately. Otherwise I scan the array building a running prefix sum modulo p. At each index i with current prefix modulo cur, I need a previous prefix modulo need = (cur - target) % p so that removing the subarray after that previous index up to i removes exactly target (mod p) from the total. I keep a hashmap mp mapping prefix modulo → last index where it appeared (initialized with mp[0] = -1) and update the answer with i - mp[need] whenever need exists. After checking I record mp[cur] = i. If the minimal length equals n (removing the whole array) I return -1, otherwise I return the minimal length.

Implementation

class Solution:
    def minSubarray(self, nums: List[int], p: int) -> int:
@Ifihan
Ifihan / main.md
Created November 29, 2025 05:44
Minimum Operations to Make Array Sum Divisible by K

Question

Approach

I observe that each decrement reduces the total sum by exactly 1. Let r = sum(nums) % k be the remainder we need to remove to make the sum divisible by k. I can always perform exactly r single-unit decrements across the elements (never needing any fancy distribution beyond reducing the total by r), so the minimum number of operations is r. If r == 0 no operations are needed.

Implementation

class Solution:
 def minOperations(self, nums: List[int], k: int) -> int:
@Ifihan
Ifihan / main.md
Created November 29, 2025 05:40
Maximum Number of K-Divisible Components
@Ifihan
Ifihan / main.md
Created November 27, 2025 17:37
Maximum Subarray Sum With Length Divisible by K

Question

Approach

I convert the array into prefix sums and observe that a subarray nums[l..r-1] has length divisible by k iff the prefix indices l and r have the same remainder modulo k (because r-l ≡ 0 (mod k)). So for each prefix index i I keep the smallest prefix sum seen so far among indices with remainder i % k. The best subarray ending at i with allowed length is prefix[i] - min_prefix[i % k]. I scan i from 0..n, update the answer using that formula, and then update the stored minimum for i % k.

Implementation

class Solution:
    def maxSubarraySum(self, nums: List[int], k: int) -> int:
@Ifihan
Ifihan / main.md
Created November 26, 2025 22:50
Paths in Matrix Whose Sum Is Divisible by K
@Ifihan
Ifihan / main.md
Created November 25, 2025 22:17
Smallest Integer Divisible by K

Question

Approach

I build the number consisting only of 1s by tracking its remainder modulo k (so I never construct huge integers). Starting with rem = 0, each step appends a 1 via rem = (rem * 10 + 1) % k. I count steps until rem == 0 and return the count. If k is divisible by 2 or 5 there is no such repunit (every repunit is odd and never divisible by 5), so I return -1. Also, by the pigeonhole principle, if we don't find rem == 0 within k iterations, remainders will cycle and no solution exists, so return -1.

Implementation

class Solution:
 def smallestRepunitDivByK(self, k: int) -> int:
@Ifihan
Ifihan / main.md
Last active November 24, 2025 22:05
Binary Prefix Divisible By 5

Question

Approach

To solve this, I avoid constructing the full binary number because it grows too fast, so I keep track of only its remainder mod 5. Each time I read a new bit, I update the running value using current = (current * 2 + bit) % 5, which reflects what happens when a bit is appended in binary. If the remainder becomes zero, then the entire prefix forms a number divisible by 5.

Implementation

class Solution:
 def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
@Ifihan
Ifihan / main.md
Created November 23, 2025 22:42
Greatest Sum Divisible by Three

Question

Approach

To solve this problem, I start by computing the total sum of the array. If the sum is already divisible by 3, then that’s the maximum answer. But if it’s not divisible by 3, I need to remove the smallest possible value that changes the remainder appropriately. So I group numbers based on their remainder when divided by 3—either 0, 1, or 2. If the total sum leaves remainder 1, I can either remove the smallest number with remainder 1, or remove two smallest numbers with remainder 2; I choose the option that removes the smaller total. Similarly, if the sum leaves remainder 2, I either remove the smallest remainder-2 number, or the two smallest remainder-1 numbers.

Implementation

class Solution:
    def maxSumDivThree(self, nums: List[int]) -> int:
@Ifihan
Ifihan / main.md
Created November 22, 2025 23:53
Find Minimum Operations to Make All Elements Divisible by Three

Question

Approach

For each number, I only care about its remainder mod 3:

  • If num % 3 == 0, cost = 0 (already divisible).
  • If num % 3 == 1, I can subtract 1 → cost = 1.
  • If num % 3 == 2, I can add 1 → cost = 1.