# Category Archives: Advanced Data Structure

## Querying the number of distinct colors in a subtree of a colored tree using BIT

Prerequisites : BIT, DFS Given a rooted tree T, with ‘n’ nodes, each node has a color denoted by the array color[](color[i] denotes the color… Read More »

## Longest Common Extension / LCE | Set 2 ( Reduction to RMQ)

Prerequisites : Suffix Array | Set 2 kasai’s algorithm The Longest Common Extension (LCE) problem considers a string s and computes, for each pair (L… Read More »

## Two Dimensional Binary Indexed Tree or Fenwick Tree

Prerequisite – Fenwick Tree We know that to answer range sum queries on a 1-D array efficiently, binary indexed tree (or Fenwick Tree) is the… Read More »

## Counting Triangles in a Rectangular space using BIT

Pre-requisite : BIT(Binary Indexed Tree or Fenwick Tree), 2D BIT Given a 2D plane, respond to Q queries, each of the following type: Insert x… Read More »

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.

## Longest Common Prefix | Set 5 (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 (Java Implementation)

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:

## 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 »