## While these algorithms might not be the most prevalent, they have the potential to be incredibly beneficial to you in your coding endeavours.

During my college years, I was deeply immersed in the world of competitive coding, investing countless hours in honing my skills and tackling challenging problems. In the realm of competitive programming, it’s imperative to have a strong grasp on a core set of algorithms, almost as if they were second nature. This repertoire includes fundamental algorithms like Binary Search, Breadth-First Search (BFS), Depth-First Search (DFS), and techniques for converting to and from adjacency lists.

However, beyond these well-known and extensively used algorithms, there existed a subset of algorithms that, while not as widely recognized, held a special place in my coding arsenal.

I found these algorithms fascinating, and they often provided unique and elegant solutions to complex problems.

Let’s have a brief discussion on some of these lesser-known algorithms and uncover what makes them so intriguing and valuable.

**Square root decomposition**

Square Root Decomposition is an algorithmic technique that follows the divide-and-conquer paradigm, breaking down the problem into more manageable subproblems to optimize performance and reduce computational complexity.

The central idea of this algorithm is to divide an array of size N into smaller blocks, each of which has a size of √N, allowing for simultaneous updates and queries on these blocks.

I have explained this algorithm here Square Root Decomposition – Skilled Coder (theskilledcoder.com)

What sets this algorithm apart is its ability to efficiently handle problems where traditional O(n log n) algorithms falter or become impractical.

Instead of iterating over the entire array for each query or update, Square Root Decomposition allows us to locate the relevant block — from the √N possible blocks — and perform the necessary operations within that block, which also has a size of √N. This adjustment slashes the time complexity from O(N) or O(n log n) to a much more manageable O(√N), offering significant performance improvements, especially with large datasets.

This algorithm shines in scenarios that involve range updates and range queries, providing a way to return cumulative or aggregate results over a specified range. While O(n log n) algorithms typically excel when dealing with sorted data structures or require the construction of complex data structures like Segment Trees or Fenwick Trees (Binary Indexed Trees), Square Root Decomposition proves to be exceptionally efficient with unsorted arrays, offering a smoother and simpler alternative.

It is worth noting that while Square Root Decomposition is versatile and powerful, it is not a one-size-fits-all solution and may not be applicable to every problem. Nevertheless, it is a fascinating and valuable algorithm worth mastering, as it adds an interesting and effective tool to a programmer’s toolkit, especially when dealing with range queries and updates in large datasets.

## Disjoint set Union

This is also known as the union-find algorithm. Again very interesting algorithm to know.

In general, whenever you have a problem that involves managing disjoint sets, keeping track of connectivity, and answering queries related to set membership or connectivity, Disjoint Set Union is a data structure worth considering. Its ability to perform union and find operations in nearly constant time makes it highly efficient for these types of problems.

Here is a general guide on when to consider using DSU:

**1. Connectivity Queries:**

- When you need to efficiently answer queries about whether two elements are in the same connected component or set.
- Example: In a network, are two computers directly or indirectly connected?

**2. Component Maintenance:**

- When you have a collection of disjoint sets and you need to perform operations like merging sets or finding the representative of a set.
- Example: In a social network, merge friend groups when two people become friends, or find the leader of a friend group.

**3. Dynamic Connectivity:**

- When the connections between elements can change over time (edges being added or removed), you need to maintain up-to-date information about the connectivity of the elements.
- Example: In a network, handle dynamic updates (additions or removals of connections) and connectivity queries.

**4. Avoiding Cycles in Graphs:**

- When constructing a graph you want to avoid creating cycles, particularly in algorithms for finding the minimum spanning tree.
- Example: In Kruskal’s algorithm for finding the minimum spanning tree of a graph.

**5. Offline Query Processing:**

- When you have a set of queries that you can process offline, DSU can help efficiently handle queries related to connectivity and set membership.

**6. Percolation Problems:**

- When dealing with problems that involve the flow of substances through a network or grid, you need to determine connectivity and flow paths.
- Example: In a grid representing a porous material, does water percolate from the top to the bottom?

**7. Equivalence Class Partitioning:**

- When you have a set of elements and an equivalence relation, and you want to partition the set into equivalence classes.
- Example: In a set of equations, find groups of variables that are equal to each other.

**8. Image Processing:**

- For tasks like connected component labelling, where you need to identify and label connected regions in an image.

Here is the article to learn more about it Disjoint Set Union Algorithm – Skilled Coder (theskilledcoder.com)

## Z Algorithm

The Z Algorithm, though not as renowned as some other string processing algorithms, is a hidden gem that I found particularly intriguing during my college days immersed in competitive coding. It’s a testament to the algorithm’s versatility and power, offering a fresh perspective and efficient solutions in the world of string manipulations.

The Z Algorithm excels in pattern searching and string matching, where it determines the occurrences of a pattern within a text in linear time. What sets it apart is its ability to preprocess the string in a way that allows for incredibly fast queries later on, significantly cutting down on the time complexity.

**How It Works**: The algorithm preprocesses the input string and constructs a Z-array, where each entry at a position ‘i’ represents the length of the longest substring starting from ‘i’ that is also a prefix of the string. This preprocessing is the secret sauce that enables lightning-fast pattern matching.

It’s immensely useful in various string processing problems, such as finding the number of occurrences of a pattern in a text, string similarity, and for more advanced applications like string compression and bioinformatics.

In competitive programming, where time efficiency is paramount, the Z Algorithm proves invaluable, especially in problems that involve intricate string manipulations and pattern matching. Its ability to preprocess and provide quick answers to queries makes it a powerful tool in the coder’s toolkit.

Beyond competitions, the algorithm has practical applications in text editors, search engines, and even in computational biology for DNA sequence analysis.

On my **Twitter** and **Instagram** accounts, I frequently share my programming journey and development experiences.

Thanks for reading 🙂