Manacher’s Algorithm is an algorithm used to find all palindromic substrings of a string in linear time. It is mainly applied to problems involving palindromes, especially when fast processing is needed.
It is commonly used to solve:
- Longest Palindromic Substring - Find the longest contiguous substring that is a palindrome in O(n) time.
- Multiple Palindrome Queries - After O(n) preprocessing, answer queries like “Is s[i..j] a palindrome?” in O(1) per query.
Traditional methods like brute-force or dynamic programming take O(n²) time and Rabin Karp (Rolling Hash) take O(n × log n) time which is inefficient for large strings or multiple queries. Manacher’s Algorithm solves these problems efficiently in O(n) time.
Why Brute-Force, DP, Center Expansion or Hashing Are Not Efficient Enough
Before diving into Manacher’s Algorithm, let’s briefly look at common approaches used for palindrome-related problems and why they fall short in terms of efficiency.
Brute-Force
- Check every possible substring and test if it's a palindrome by comparing characters.
- Total substrings = O(n²), and checking each takes up to O(n) time.
- Time Complexity: O(n³)
- Drawback: Very slow even for moderate input sizes.
Dynamic Programming (DP)
- Use a 2D table where dp[i][j] stores whether s[i..j] is a palindrome.
- Build the table using previously computed results.
- Time Complexity: O(n²)
- Space Complexity: O(n²)
- Drawback: Still too slow and heavy in space for large strings.
Center Expansion
- Treat each character and each gap between characters as a potential center
- For each center:
- Expand outward while s[left] == s[right]
- Track the longest palindrome found during expansions
- Time Complexity: O(n²)
- Space Complexity: O(1)
- Drawbacks: Still quadratic in time in the worst case, Less efficient than Manacher’s Algorithm for long strings
Rabin-Karp (Hashing) + Binary Search
- Use rolling hash to compare reversed substrings with the original.
- Combine with binary search to find the longest palindrome centered at each position.
- Time Complexity: O(n log n) due to binary search on each center.
- Space Complexity: O(n) for prefix hashes.
- Drawback: Much faster than DP but still not optimal. Also, care needed to avoid hash collisions.
Why Manacher’s is Better:
All the above methods are slower than linear time. Manacher’s Algorithm solves the same problems in O(n) time using a clever technique based on symmetry and center expansion, with constant space (excluding the result array).
Preprocessing the String
Why Preprocessing is Needed ?
In a normal string, palindromic substrings can be of odd or even length:
- Odd-length: "aba", "racecar"
- Even-length: "abba", "noon"
Handling these two cases separately makes the implementation complex. To simplify the logic and treat all palindromes uniformly, Manacher’s Algorithm modifies the original string so that:
- All palindromes become odd-length
- Each character and the spaces between characters become potential centers of palindromes
This avoids writing separate code for odd and even lengths and allows a single unified expansion logic.
How It’s Done in Code:
- Start with @: Sentinel to handle left boundary and avoid bounds checking during expansion.
- Insert # between characters
=> This makes sure that:
- Original characters stay separated
- Even-length palindromes like "abba" now look like: "#a#b#b#a#"
- So, both even and odd cases are treated as odd-length in the transformed string. - End with $: Sentinel to handle the right boundary safely.
Example:
For input string: s = "abba"
Transformed string ms: "@#a#b#b#a#$"
Each character (including #) can now be treated as a potential center for a palindrome. This transformation simplifies the core logic of the algorithm and ensures consistent behavior.
Core Algorithm
After preprocessing, the algorithm runs a single loop over the transformed string ms to compute the array p[], where p[i] stores the radius (number of characters on one side) of the longest palindrome centered at position i.
This logic is implemented in the runManacher() function.
- Key Variables: int l = 0, r = 0; l and r represent the left and right boundaries of the rightmost palindrome found so far.
- Loop Through the Transformed String
=> Start from index 1 (since index 0 is @, the sentinel)
=> End before n - 1 (index of $, the ending sentinel) - Mirror Optimization: p[i] = max(0, min(r - i, p[r + l - i]));
=> If i is within the current palindrome [l, r], we use the mirror of i about the center to initialize p[i].
=> Mirror position: mirror = l + r - i
=> We take the minimum between p[mirror] and the remaining span (r - i) to avoid going out of bounds.
=> This avoids redundant re-computation , a key idea that makes the algorithm linear. - Expand Palindrome Centered at i -
=> Try to expand the palindrome centered at i by comparing characters on both sides.
=> Continue expanding as long as characters match.
C++
while (ms[i + 1 + p[i]] == ms[i - 1 - p[i]]) {
++p[i];
}
- Update Rightmost Boundary: If the palindrome centered at i goes beyond the current right boundary r, we update l and r to reflect this new, longer palindrome.
C++
if (i + p[i] > r) {
l = i - p[i];
r = i + p[i];
}
- Final Result: The array p[i] for each index i in ms contains the maximum radius of palindrome centered at i.
Mirror Symmetry & Case-by-Case Connection
We now explain how code handles the three mirror symmetry cases:
When i < r → i is within current palindrome:
int mirror = l + r - i;
p[i] = min(r - i, p[mirror]);
This mirrors the idea of computing the palindrome at i by referencing its mirror position j = 2*c – i.
We are essentially handling Case 1 and Case 2:
Case 1: (p[mirror] < r - i): Mirror is fully inside current palindrome → safe to copy full value.
Case 2: (p[mirror] > r - i): Mirror extends beyond r, so copy only up to boundary r.
p[i] = min(r - i, p[mirror]);
which correctly handles both cases using min.
When p[mirror] == r - i (Case 3):
We still set p[i] = r - i via the min(...) logic above, but...
We continue expanding manually with:
C++
while (ms[i + 1 + p[i]] == ms[i - 1 - p[i]]{
++p[i];
}
This is the implementation of Case 3, where the mirrored palindrome just touches the boundary, and you try to expand beyond it.
Update of [l, r] interval: Once expansion at i goes past r, we update the rightmost palindrome. This keeps l and r as the bounds of the longest palindrome seen so far, required for correct mirror computations in the next iterations.
C++
if (i + p[i] > r) {
l = i - p[i];
r = i + p[i];
}
Why Manacher’s Algorithm Works
To understand why Manacher’s Algorithm correctly computes all palindromic substrings in linear time, we need to look at the core idea of symmetry and how it avoids redundant work using mirror positions.
Symmetry in Palindromes
A palindrome has mirror symmetry around its center.
If a palindrome is centered at some position center, and we are at a position i inside that palindrome (i.e., i < r), then the character at i has a mirror position: mirror = 2 * center - i
Reusing Information from Mirror
If we already know that p[mirror] = x, we can safely say that:
- At least min(r - i, p[mirror]) characters around i will also match
- Beyond that, we may or may not be able to expand — so we try
- This saves time: instead of re-expanding everything from scratch at each center, we start from the known length and only expand beyond it if necessary.
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Manacher {
public:
// p[i] = radius of longest palindrome centered at i
// in transformed string
vector<int> p;
// transformed string with # and sentinels
string ms;
// preprocess the string and run the algorithm
Manacher(string &s) {
// left sentinel
ms = "@";
for (char c : s) {
ms += "#" + string(1, c);
}
// right sentinel
ms += "#$";
// run Manacher’s algorithm
runManacher();
}
void runManacher() {
int n = ms.size();
p.assign(n, 0);
int l = 0, r = 0;
for (int i = 1; i < n - 1; ++i) {
// mirror of i around center (l + r)/2
int mirror = l + r - i;
// initialize p[i] based on its mirror
// if within bounds
if (i < r)
p[i] = min(r - i, p[mirror]);
// expand palindrome centered at i
while (ms[i + 1 + p[i]] == ms[i - 1 - p[i]]){
++p[i];
}
// update [l, r] if the palindrome expands
// beyond current r
if (i + p[i] > r) {
l = i - p[i];
r = i + p[i];
}
}
}
// returns length of longest palindrome centered
// at 'cen' in original string
// 'odd' = 1 → check for odd-length, 'odd' = 0 → even-length
int getLongest(int cen, int odd) {
// map original index to transformed string index
int pos = 2 * cen + 2 + !odd;
return p[pos];
}
// checks if s[l..r] is a palindrome in O(1)
bool check(int l, int r) {
int len = r - l + 1;
int cen = (l + r) / 2;
return len <= getLongest(cen, len % 2);
}
};
Java
class Manacher {
// p[i] = radius of longest palindrome centered at i
// in transformed string
int[] p;
// transformed string with # and sentinels
String ms;
// preprocess the string and run the algorithm
Manacher(String s) {
// left sentinel
ms = "@";
for (char c : s.toCharArray()) {
ms += "#" + c;
}
// right sentinel
ms += "#$";
// run Manacher’s algorithm
runManacher();
}
void runManacher() {
int n = ms.length();
p = new int[n];
int l = 0, r = 0;
for (int i = 1; i < n - 1; ++i) {
// mirror of i around center (l + r)/2
int mirror = l + r - i;
// initialize p[i] based on its mirror
// if within bounds
if (i < r)
p[i] = Math.min(r - i, p[mirror]);
// expand palindrome centered at i
while (ms.charAt(i + 1 + p[i]) == ms.charAt(i - 1 - p[i])) {
++p[i];
}
// update [l, r] if the palindrome expands
// beyond current r
if (i + p[i] > r) {
l = i - p[i];
r = i + p[i];
}
}
}
// returns length of longest palindrome centered
// at 'cen' in original string
// 'odd' = 1 → check for odd-length, 'odd' = 0 → even-length
int getLongest(int cen, int odd) {
// map original index to transformed string index
int pos = 2 * cen + 2 + (odd == 0 ? 1 : 0);
return p[pos];
}
// checks if s[l..r] is a palindrome in O(1)
boolean check(int l, int r) {
int len = r - l + 1;
int cen = (l + r) / 2;
return len <= getLongest(cen, len % 2);
}
}
Python
class Manacher:
# p[i] = radius of longest palindrome centered at i
# in transformed string
def __init__(self, s):
# transformed string with # and sentinels
self.ms = "@"
for c in s:
self.ms += "#" + c
self.ms += "#$"
# run Manacher’s algorithm
self.p = [0] * len(self.ms)
self.runManacher()
def runManacher(self):
n = len(self.ms)
l = r = 0
for i in range(1, n - 1):
# mirror of i around center (l + r)/2
mirror = l + r - i
# initialize p[i] based on its mirror
# if within bounds
if i < r:
self.p[i] = min(r - i, self.p[mirror])
# expand palindrome centered at i
while self.ms[i + 1 + self.p[i]] == \
self.ms[i - 1 - self.p[i]]:
self.p[i] += 1
# update [l, r] if the palindrome expands
# beyond current r
if i + self.p[i] > r:
l = i - self.p[i]
r = i + self.p[i]
# returns length of longest palindrome centered
# at 'cen' in original string
# 'odd' = 1 → check for odd-length, 'odd' = 0 → even-length
def getLongest(self, cen, odd):
# map original index to transformed string index
pos = 2 * cen + 2 + (0 if odd else 1)
return self.p[pos]
# checks if s[l..r] is a palindrome in O(1)
def check(self, l, r):
length = r - l + 1
cen = (l + r) // 2
return length <= self.getLongest(cen, length % 2)
C#
using System;
class Manacher {
// p[i] = radius of longest palindrome centered at i
// in transformed string
public int[] p;
// transformed string with # and sentinels
public string ms;
// preprocess the string and run the algorithm
public manacher(string s) {
ms = "@";
foreach (char c in s) {
ms += "#" + c;
}
ms += "#$";
runManacher();
}
void runManacher() {
int n = ms.Length;
p = new int[n];
int l = 0, r = 0;
for (int i = 1; i < n - 1; ++i) {
// mirror of i around center (l + r)/2
int mirror = l + r - i;
// initialize p[i] based on its mirror
// if within bounds
if (i < r)
p[i] = Math.Min(r - i, p[mirror]);
// expand palindrome centered at i
while (ms[i + 1 + p[i]] == ms[i - 1 - p[i]]) {
++p[i];
}
// update [l, r] if the palindrome expands
// beyond current r
if (i + p[i] > r) {
l = i - p[i];
r = i + p[i];
}
}
}
// returns length of longest palindrome centered
// at 'cen' in original string
// 'odd' = 1 → check for odd-length, 'odd' = 0 → even-length
public int getLongest(int cen, int odd) {
int pos = 2 * cen + 2 + (odd == 0 ? 1 : 0);
return p[pos];
}
// checks if s[l..r] is a palindrome in O(1)
public bool check(int l, int r) {
int len = r - l + 1;
int cen = (l + r) / 2;
return len <= getLongest(cen, len % 2);
}
}
JavaScript
class Manacher {
constructor(s) {
// transformed string with # and sentinels
this.ms = "@";
for (let c of s) {
this.ms += "#" + c;
}
this.ms += "#$";
// p[i] = radius of longest palindrome centered at i
// in transformed string
this.p = Array(this.ms.length).fill(0);
// run Manacher’s algorithm
this.runManacher();
}
runManacher() {
let n = this.ms.length;
let l = 0, r = 0;
for (let i = 1; i < n - 1; ++i) {
// mirror of i around center (l + r)/2
let mirror = l + r - i;
// initialize p[i] based on its mirror
// if within bounds
if (i < r)
this.p[i] = Math.min(r - i, this.p[mirror]);
// expand palindrome centered at i
while (this.ms[i + 1 + this.p[i]] === this.ms[i - 1 - this.p[i]]) {
this.p[i]++;
}
// update [l, r] if the palindrome expands
// beyond current r
if (i + this.p[i] > r) {
l = i - this.p[i];
r = i + this.p[i];
}
}
}
// returns length of longest palindrome centered
// at 'cen' in original string
// 'odd' = 1 → check for odd-length, 'odd' = 0 → even-length
getLongest(cen, odd) {
let pos = 2 * cen + 2 + (odd === 0 ? 1 : 0);
return this.p[pos];
}
// checks if s[l..r] is a palindrome in O(1)
check(l, r) {
let len = r - l + 1;
let cen = Math.floor((l + r) / 2);
return len <= this.getLongest(cen, len % 2);
}
}
Time Complexity: O(n)
Manacher’s Algorithm runs in linear time because each character in the transformed string is visited at most once during expansion, and mirror values prevent redundant checks. In total, the algorithm performs O(n) operations where n is the length of the original string (after transformation it's about 2n+3, but still linear).
Auxiliary Space: O(n)
An extra array p[] of size proportional to the transformed string (i.e., 2n+3) is used to store palindrome radii. The transformed string also takes O(n) space, so the total additional space remains linear in terms of the original string size.
C++
class GfG {
public:
string longestPalindrome(string s) {
Manacher mob(s);
int n = s.size();
// maximum length found so far
int maxLen = 1;
// starting index of longest palindrome
int bestStart = 0;
for (int i = 0; i < n; i++) {
// check for odd-length palindrome centered at i
int oddLen = mob.getLongest(i, 1);
if (oddLen > maxLen) {
maxLen = oddLen;
bestStart = i - maxLen / 2;
}
// check for even-length palindrome centered
// between i and i+1
int evenLen = mob.getLongest(i, 0);
if (evenLen > maxLen) {
maxLen = evenLen;
bestStart = i - maxLen / 2 + 1;
}
}
// extract the longest palindromic substring
return s.substr(bestStart, maxLen);
}
};
Java
class GfG {
public String longestPalindrome(String s) {
Manacher mob = new Manacher(s);
int n = s.length();
// maximum length found so far
int maxLen = 1;
// starting index of longest palindrome
int bestStart = 0;
for (int i = 0; i < n; i++) {
// check for odd-length palindrome centered at i
int oddLen = mob.getLongest(i, 1);
if (oddLen > maxLen) {
maxLen = oddLen;
bestStart = i - maxLen / 2;
}
// check for even-length palindrome centered
// between i and i+1
int evenLen = mob.getLongest(i, 0);
if (evenLen > maxLen) {
maxLen = evenLen;
bestStart = i - maxLen / 2 + 1;
}
}
// extract the longest palindromic substring
return s.substring(bestStart, bestStart + maxLen);
}
}
Python
class GfG:
def longestPalindrome(self, s):
mob = Manacher(s)
n = len(s)
# maximum length found so far
maxLen = 1
# starting index of longest palindrome
bestStart = 0
for i in range(n):
# check for odd-length palindrome centered at i
oddLen = mob.getLongest(i, 1)
if oddLen > maxLen:
maxLen = oddLen
bestStart = i - maxLen // 2
# check for even-length palindrome centered
# between i and i+1
evenLen = mob.getLongest(i, 0)
if evenLen > maxLen:
maxLen = evenLen
bestStart = i - maxLen // 2 + 1
# extract the longest palindromic substring
return s[bestStart: bestStart + maxLen]
C#
class GfG {
public string longestPalindrome(string s) {
Manacher mob = new Manacher(s);
int n = s.Length;
// maximum length found so far
int maxLen = 1;
// starting index of longest palindrome
int bestStart = 0;
for (int i = 0; i < n; i++) {
// check for odd-length palindrome centered at i
int oddLen = mob.getLongest(i, 1);
if (oddLen > maxLen) {
maxLen = oddLen;
bestStart = i - maxLen / 2;
}
// check for even-length palindrome centered
// between i and i+1
int evenLen = mob.getLongest(i, 0);
if (evenLen > maxLen) {
maxLen = evenLen;
bestStart = i - maxLen / 2 + 1;
}
}
// extract the longest palindromic substring
return s.Substring(bestStart, maxLen);
}
}
JavaScript
class GfG {
longestPalindrome(s) {
const mob = new Manacher(s);
const n = s.length;
// maximum length found so far
let maxLen = 1;
// starting index of longest palindrome
let bestStart = 0;
for (let i = 0; i < n; i++) {
// check for odd-length palindrome centered at i
let oddLen = mob.getLongest(i, 1);
if (oddLen > maxLen) {
maxLen = oddLen;
bestStart = i - Math.floor(maxLen / 2);
}
// check for even-length palindrome centered
// between i and i+1
let evenLen = mob.getLongest(i, 0);
if (evenLen > maxLen) {
maxLen = evenLen;
bestStart = i - Math.floor(maxLen / 2) + 1;
}
}
// extract the longest palindromic substring
return s.substring(bestStart, bestStart + maxLen);
}
}
C++
class GfG {
public:
// returns true/false for each query [l, r]
vector<bool> checkPalindromes(string s,
vector<vector<int>>& queries) {
// preprocess the string using Mancher Class
Manacher mob(s);
vector<bool> res;
for (auto& q : queries) {
int l = q[0], r = q[1];
// check if a[l..r] is a palindrome
// using O(1) query
res.push_back(mob.check(l, r));
}
return res;
}
};
Java
class GfG {
// returns true/false for each query [l, r]
public ArrayList<Boolean> checkPalindromes(String s,
int[][] queries) {
// preprocess the string using Manacher Class
Manacher mob = new Manacher(s);
ArrayList<Boolean> res = new ArrayList<>();
for (int[] q : queries) {
int l = q[0], r = q[1];
// check if a[l..r] is a palindrome
// using O(1) query
res.add(mob.check(l, r));
}
return res;
}
}
Python
class GfG:
# returns true/false for each query [l, r]
def checkPalindromes(self, s, queries):
# preprocess the string using Manacher class
mob = Manacher(s)
res = []
for q in queries:
l, r = q
# check if a[l..r] is a palindrome
# using O(1) query
res.append(mob.check(l, r))
return res
C#
class GfG {
// returns true/false for each query [l, r]
public List<bool> checkPalindromes(string s,
int[,] queries) {
// preprocess the string using Manacher class
Manacher mob = new Manacher(s);
List<bool> res = new List<bool>();
// number of queries
int qCount = queries.GetLength(0);
for (int i = 0; i < qCount; i++) {
int l = queries[i, 0];
int r = queries[i, 1];
// check if a[l..r] is a palindrome
// using O(1) query
res.Add(mob.check(l, r));
}
return res;
}
}
JavaScript
class GfG {
// returns true/false for each query [l, r]
checkPalindromes(s, queries) {
// preprocess the string using Manacher class
const mob = new Manacher(s);
const res = [];
for (const [l, r] of queries) {
// check if a[l..r] is a palindrome
// using O(1) query
res.push(mob.check(l, r));
}
return res;
}
}
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Binary Indexed Tree or Fenwick TreeBinary Indexed Trees are used for problems where we have following types of multiple operations on a fixed sized.Prefix Operation (Sum, Product, XOR, OR, etc). Note that range operations can also be solved using prefix. For example, range sum from index L to R is prefix sum till R (included minus pr
15 min read
Square Root (Sqrt) Decomposition AlgorithmSquare Root Decomposition Technique is one of the most common query optimization techniques used by competitive programmers. This technique helps us to reduce Time Complexity by a factor of sqrt(N) The key concept of this technique is to decompose a given array into small chunks specifically of size
15+ min read
Binary LiftingBinary Lifting is a Dynamic Programming approach for trees where we precompute some ancestors of every node. It is used to answer a large number of queries where in each query we need to find an arbitrary ancestor of any node in a tree in logarithmic time.The idea behind Binary Lifting:1. Preprocess
15+ min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem