Given an empty set initially and a number of queries on it, each possibly of the following types: Insert – Insert a new element ‘x’.… Read More »

## Find the number of Islands | Set 2 (Using Disjoint Set)

Given a boolean 2D matrix, find the number of islands. A group of connected 1s forms an island. For example, the below matrix contains 5… Read More »

## Find last unique URL from long list of URLs in single traversal

Given a very long list of URLs, find out last unique URL. Only one traversal of all URLs is allowed. Examples: Input: https://www.geeksforgeeks.org http://quiz.geeksforgeeks.org http://qa.geeksforgeeks.org… Read More »

## Longest Common Prefix using Trie

Given a set of strings, find the longest common prefix. Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : “gee” Input : {“apple”, “ape”, “april”} Output… Read More »

## Smallest Subarray with given GCD

Given an array arr[] of n numbers and an integer k, find length of the minimum sub-array with gcd equals to k. Example: Input: arr[]… Read More »

## GCDs of given index ranges in an array

Given an array a[0 . . . n-1]. We should be able to efficiently find the GCD from index qs (query start) to qe (query… Read More »

## Disjoint Set Data Structures

Consider a situation with a number of persons and following tasks to be performed on them. Add a new friendship relation, i.e., a person x… Read More »

## Tarjan’s off-line lowest common ancestors algorithm

Prerequisite : LCA basics, Disjoint Set Union by Rank and Path Compression We are given a tree(can be extended to a DAG) and we have… Read More »

## Cartesian Tree Sorting

Prerequisites : Cartesian Tree Cartesian Sort is an Adaptive Sorting as it sorts the data faster if data is partially sorted. In fact, there are… Read More »

## Cartesian Tree

A Cartesian tree is a tree data structure created from a set of data that obeys the  following structural invariants: The tree obeys in the min (or max) heap property – each node is less (or greater) than its children. An inorder traversal of the nodes yields the values in the same order in which they appear in the initial sequence. Suppose we have an input array- {5,10,40,30,28}. Then the max-heap Cartesian Tree would be. A min-heap Cartesian… Read More »

## Sparse Set

How to do the following operations efficiently if there are large number of queries for them. Insertion Deletion Searching Clearing/Removing all the elements. One solution… Read More »

## Persistent data structures

All the data structures discussed here so far are non-persistent (or ephermal). A persistent data structure is a data structure that always preserves the previous… Read More »

## Centroid Decomposition of Tree

Background : What is centroid of Tree? Centroid of a Tree is a node which if removed from the tree would split it into a… Read More »

## Gomory-Hu Tree | Set 1 (Introduction)

Background : In a flow network, an s-t cut is a cut that requires the source ‘s’ and the sink ‘t’ to be in different… Read More »

## ­­kasai’s Algorithm for Construction of LCP array from Suffix Array

Background Suffix Array : A suffix array is a sorted array of all suffixes of a given string. Let the given string be “banana”. 0… Read More »