Posts

Showing posts from May, 2025

Miller-Rabin Primality Test and Caching III

Image
Another application of MRP with caching. The caveat here is that using BigInteger implementation of MRP Test leads to TLE. Converting to Long does the trick. There is still usage of BigInteger for modular exponentiation (otherwise you'd have to implement your own, which can be a little tricky), but that doesn't seem to add much overhead to the solution. It is also interesting to use OpenAI ChatGPT 4.0 model to ask and learn for the detailed time complexity of this algorithm. It is amazing to see how fast MPR is - an impressive LogN. Answer from the bot is down below, as well s my code. Cheers, ACC. Analysis from OpenAI ChatGPT Model 4.0: Time Complexity: 🔸 Worst Case: O(n² log m) (where m is the largest number formed from the substring) 🔸 With caching and typical inputs, it behaves closer to O(n²) . Sum of Largest Prime Substrings - LeetCode Given a string  s , find the sum of the  3 largest unique  prime numbers  that can be formed using any of its   subs...

Prefix Sum VIII

Image
In this example a simple prefix sum approach does the job. Some caveats to keep in mind. First, there is no reason to use a hash table - in this case two simple prefix sum arrays work. Second, watch for overflow, since you're going to be adding up numbers that can grow up to 10^10, use long instead of int. Finally, the calculation of prefix sum is usually straightforward, following the pattern: PrefixSum[i] == PrefixSum[Last] - PrefixSum[i] Code is down below, cheers, keep coding. ACC Equal Sum Grid Partition I - LeetCode You are given an  m x n  matrix  grid  of positive integers. Your task is to determine if it is possible to make  either one horizontal or one vertical cut  on the grid such that: Each of the two resulting sections formed by the cut is  non-empty . The sum of the elements in both sections is  equal . Return  true  if such a partition exists; otherwise return  false .   Example 1: Input:   grid = [[1,4],[2...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. 🔍 Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...