Sparse Table - GeeksforGeeks

CoursesFor Working ProfessionalsLIVEDSA Live ClassesSystem DesignJava Backend DevelopmentFull Stack LIVEExplore MoreSelf-PacedDSA- Self PacedSDE TheoryMust-Do Coding QuestionsExplore MoreFor StudentsLIVECompetitive ProgrammingData Structures with C++Data ScienceExplore MoreSelf-PacedDSA- Self PacedCIPJAVA / Python / C++Explore MoreSchool CoursesSchool GuidePython ProgrammingLearn To Make AppsAll Courses
For Working ProfessionalsLIVEDSA Live ClassesSystem DesignJava Backend DevelopmentFull Stack LIVEExplore MoreSelf-PacedDSA- Self PacedSDE TheoryMust-Do Coding QuestionsExplore More
LIVEDSA Live ClassesSystem DesignJava Backend DevelopmentFull Stack LIVEExplore More
DSA Live Classes
System Design
Java Backend Development
Full Stack LIVE
Explore More
Self-PacedDSA- Self PacedSDE TheoryMust-Do Coding QuestionsExplore More
DSA- Self Paced
SDE Theory
Must-Do Coding Questions
Explore More
For StudentsLIVECompetitive ProgrammingData Structures with C++Data ScienceExplore MoreSelf-PacedDSA- Self PacedCIPJAVA / Python / C++Explore More
LIVECompetitive ProgrammingData Structures with C++Data ScienceExplore More
Competitive Programming
Data Structures with C++
Data Science
Explore More
Self-PacedDSA- Self PacedCIPJAVA / Python / C++Explore More
DSA- Self Paced
CIP
JAVA / Python / C++
Explore More
School CoursesSchool GuidePython ProgrammingLearn To Make Apps
School Guide
Python Programming
Learn To Make Apps
All Courses
TutorialsPractice DS & Algo.Must Do QuestionsDSA Topic-wiseDSA Company-wiseAlgorithmsAnalysis of AlgorithmsAsymptotic AnalysisWorst, Average and Best CasesAsymptotic NotationsLittle o and little omega notationsLower and Upper Bound TheoryAnalysis of LoopsSolving RecurrencesAmortized AnalysisWhat does 'Space Complexity' mean ?Pseudo-polynomial AlgorithmsPolynomial Time Approximation SchemeA Time Complexity QuestionSearching AlgorithmsSorting AlgorithmsGraph AlgorithmsPattern SearchingGeometric AlgorithmsMathematicalBitwise AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide and ConquerBacktrackingBranch and BoundAll AlgorithmsData StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvanced Data StructureMatrixStringsAll Data StructuresInterview CornerCompany PreparationTop TopicsPractice Company QuestionsInterview ExperiencesExperienced InterviewsInternship InterviewsCompetititve ProgrammingDesign PatternsSystem Design TutorialMultiple Choice QuizzesLanguagesCC++JavaPythonC#JavaScriptjQuerySQLPHPScalaPerlGo LanguageHTMLCSSKotlinCS SubjectsMathematicsOperating SystemDBMSComputer NetworksComputer Organization and ArchitectureTheory of ComputationCompiler DesignDigital LogicSoftware EngineeringGATEGATE Computer Science NotesLast Minute NotesGATE CS Solved PapersGATE CS Original Papers and Official KeysGATE 2021 DatesGATE CS 2021 SyllabusImportant Topics for GATE CSWeb TechnologiesHTMLCSSJavaScriptAngularJSReactJSNodeJSBootstrapjQueryPHPSoftware DesignsSoftware Design PatternsSystem Design TutorialSchool LearningSchool ProgrammingMathematicsNumber SystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculusMaths Notes (Class 8-12)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesClass 12 NotesNCERT SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionRD Sharma SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionPhysics Notes (Class 8-11)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesCS Exams/PSUsISROISRO CS Original Papers and Official KeysISRO CS Solved PapersISRO CS Syllabus for Scientist/Engineer ExamUGC NETUGC NET CS Notes Paper IIUGC NET CS Notes Paper IIIUGC NET CS Solved PapersStudentCampus Ambassador ProgramSchool Ambassador ProgramProjectGeek of the MonthCampus Geek of the MonthPlacement CourseCompetititve ProgrammingTestimonialsGeek on the TopCareersInternship
Practice DS & Algo.Must Do QuestionsDSA Topic-wiseDSA Company-wise
Must Do Questions
DSA Topic-wise
DSA Company-wise
AlgorithmsAnalysis of AlgorithmsAsymptotic AnalysisWorst, Average and Best CasesAsymptotic NotationsLittle o and little omega notationsLower and Upper Bound TheoryAnalysis of LoopsSolving RecurrencesAmortized AnalysisWhat does 'Space Complexity' mean ?Pseudo-polynomial AlgorithmsPolynomial Time Approximation SchemeA Time Complexity QuestionSearching AlgorithmsSorting AlgorithmsGraph AlgorithmsPattern SearchingGeometric AlgorithmsMathematicalBitwise AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide and ConquerBacktrackingBranch and BoundAll Algorithms
Analysis of AlgorithmsAsymptotic AnalysisWorst, Average and Best CasesAsymptotic NotationsLittle o and little omega notationsLower and Upper Bound TheoryAnalysis of LoopsSolving RecurrencesAmortized AnalysisWhat does 'Space Complexity' mean ?Pseudo-polynomial AlgorithmsPolynomial Time Approximation SchemeA Time Complexity Question
Asymptotic Analysis
Worst, Average and Best Cases
Asymptotic Notations
Little o and little omega notations
Lower and Upper Bound Theory
Analysis of Loops
Solving Recurrences
Amortized Analysis
What does 'Space Complexity' mean ?
Pseudo-polynomial Algorithms
Polynomial Time Approximation Scheme
A Time Complexity Question
Searching Algorithms
Sorting Algorithms
Graph Algorithms
Pattern Searching
Geometric Algorithms
Mathematical
Bitwise Algorithms
Randomized Algorithms
Greedy Algorithms
Dynamic Programming
Divide and Conquer
Backtracking
Branch and Bound
All Algorithms
Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvanced Data StructureMatrixStringsAll Data Structures
Arrays
Linked List
Stack
Queue
Binary Tree
Binary Search Tree
Heap
Hashing
Graph
Advanced Data Structure
Matrix
Strings
All Data Structures
Interview CornerCompany PreparationTop TopicsPractice Company QuestionsInterview ExperiencesExperienced InterviewsInternship InterviewsCompetititve ProgrammingDesign PatternsSystem Design TutorialMultiple Choice Quizzes
Company Preparation
Top Topics
Practice Company Questions
Interview Experiences
Experienced Interviews
Internship Interviews
Competititve Programming
Design Patterns
System Design Tutorial
Multiple Choice Quizzes
LanguagesCC++JavaPythonC#JavaScriptjQuerySQLPHPScalaPerlGo LanguageHTMLCSSKotlin
C
C++
Java
Python
C#
JavaScript
jQuery
SQL
PHP
Scala
Perl
Go Language
HTML
CSS
Kotlin
CS SubjectsMathematicsOperating SystemDBMSComputer NetworksComputer Organization and ArchitectureTheory of ComputationCompiler DesignDigital LogicSoftware Engineering
Mathematics
Operating System
DBMS
Computer Networks
Computer Organization and Architecture
Theory of Computation
Compiler Design
Digital Logic
Software Engineering
GATEGATE Computer Science NotesLast Minute NotesGATE CS Solved PapersGATE CS Original Papers and Official KeysGATE 2021 DatesGATE CS 2021 SyllabusImportant Topics for GATE CS
GATE Computer Science Notes
Last Minute Notes
GATE CS Solved Papers
GATE CS Original Papers and Official Keys
GATE 2021 Dates
GATE CS 2021 Syllabus
Important Topics for GATE CS
Web TechnologiesHTMLCSSJavaScriptAngularJSReactJSNodeJSBootstrapjQueryPHP
HTML
CSS
JavaScript
AngularJS
ReactJS
NodeJS
Bootstrap
jQuery
PHP
Software DesignsSoftware Design PatternsSystem Design Tutorial
Software Design Patterns
System Design Tutorial
School LearningSchool ProgrammingMathematicsNumber SystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculusMaths Notes (Class 8-12)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesClass 12 NotesNCERT SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionRD Sharma SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionPhysics Notes (Class 8-11)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 Notes
School Programming
MathematicsNumber SystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculus
Number System
Algebra
Trigonometry
Statistics
Probability
Geometry
Mensuration
Calculus
Maths Notes (Class 8-12)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesClass 12 Notes
Class 8 Notes
Class 9 Notes
Class 10 Notes
Class 11 Notes
Class 12 Notes
NCERT SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths Solution
Class 8 Maths Solution
Class 9 Maths Solution
Class 10 Maths Solution
Class 11 Maths Solution
Class 12 Maths Solution
RD Sharma SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths Solution
Class 8 Maths Solution
Class 9 Maths Solution
Class 10 Maths Solution
Class 11 Maths Solution
Class 12 Maths Solution
Physics Notes (Class 8-11)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 Notes
Class 8 Notes
Class 9 Notes
Class 10 Notes
Class 11 Notes
CS Exams/PSUsISROISRO CS Original Papers and Official KeysISRO CS Solved PapersISRO CS Syllabus for Scientist/Engineer ExamUGC NETUGC NET CS Notes Paper IIUGC NET CS Notes Paper IIIUGC NET CS Solved Papers
ISROISRO CS Original Papers and Official KeysISRO CS Solved PapersISRO CS Syllabus for Scientist/Engineer Exam
ISRO CS Original Papers and Official Keys
ISRO CS Solved Papers
ISRO CS Syllabus for Scientist/Engineer Exam
UGC NETUGC NET CS Notes Paper IIUGC NET CS Notes Paper IIIUGC NET CS Solved Papers
UGC NET CS Notes Paper II
UGC NET CS Notes Paper III
UGC NET CS Solved Papers
StudentCampus Ambassador ProgramSchool Ambassador ProgramProjectGeek of the MonthCampus Geek of the MonthPlacement CourseCompetititve ProgrammingTestimonialsGeek on the TopCareersInternship
Campus Ambassador Program
School Ambassador Program
Project
Geek of the Month
Campus Geek of the Month
Placement Course
Competititve Programming
Testimonials
Geek on the Top
Careers
Internship
JobsApply for JobsPost a JobJOB-A-THON
Apply for Jobs
Post a Job
JOB-A-THON
Events




WriteCome write articles for us and get featuredPracticeLearn and code with the best industry expertsPremiumGet access to ad-free content, doubt assistance and more!JobsCome and find your dream job with usGeeks DigestQuizzesGeeks CampusGblog ArticlesIDECampus Mantri
Geeks Digest
Quizzes
Geeks Campus
Gblog Articles
IDE
Campus Mantri
Sign In
Sign In
Home
Saved Videos
Courses


For Working Professionals
                        



LIVE
                        


DSA Live Classes

System Design

Java Backend Development

Full Stack LIVE

Explore More


Self-Paced
                        


DSA- Self Paced

SDE Theory

Must-Do Coding Questions

Explore More


For Students
                        



LIVE
                        


Competitive Programming

Data Structures with C++

Data Science

Explore More


Self-Paced
                        


DSA- Self Paced

CIP

JAVA / Python / C++

Explore More


School Courses
                        


School Guide

Python Programming

Learn To Make Apps


Practice DS & Algo.
                        


Must Do Questions

DSA Topic-wise

DSA Company-wise


Algorithms
                        


Searching Algorithms

Sorting Algorithms

Graph Algorithms

Pattern Searching

Geometric Algorithms

Mathematical

Bitwise Algorithms

Randomized Algorithms

Greedy Algorithms

Dynamic Programming

Divide and Conquer

Backtracking

Branch and Bound

All Algorithms


Analysis of Algorithms
                        


Asymptotic Analysis

Worst, Average and Best Cases

Asymptotic Notations

Little o and little omega notations

Lower and Upper Bound Theory

Analysis of Loops

Solving Recurrences

Amortized Analysis

What does 'Space Complexity' mean ?

Pseudo-polynomial Algorithms

Polynomial Time Approximation Scheme

A Time Complexity Question


Data Structures
                        


Arrays

Linked List

Stack

Queue

Binary Tree

Binary Search Tree

Heap

Hashing

Graph

Advanced Data Structure

Matrix

Strings

All Data Structures


Interview Corner
                        


Company Preparation

Top Topics

Practice Company Questions

Interview Experiences

Experienced Interviews

Internship Interviews

Competititve Programming

Design Patterns

System Design Tutorial

Multiple Choice Quizzes


Languages
                        


C

C++

Java

Python

C#

JavaScript

jQuery

SQL

PHP

Scala

Perl

Go Language

HTML

CSS

Kotlin


CS Subjects
                        


Mathematics

Operating System

DBMS

Computer Networks

Computer Organization and Architecture

Theory of Computation

Compiler Design

Digital Logic

Software Engineering


GATE
                        


GATE Computer Science Notes

Last Minute Notes

GATE CS Solved Papers

GATE CS Original Papers and Official Keys

GATE 2021 Dates

GATE CS 2021 Syllabus

Important Topics for GATE CS


Web Technologies
                        


HTML

CSS

JavaScript

AngularJS

ReactJS

NodeJS

Bootstrap

jQuery

PHP


Software Designs
                        


Software Design Patterns

System Design Tutorial


School Learning
                        


School Programming


Mathematics
                        


Number System

Algebra

Trigonometry

Statistics

Probability

Geometry

Mensuration

Calculus


Maths Notes (Class 8-12)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes

Class 12 Notes


NCERT Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


RD Sharma Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


Physics Notes (Class 8-11)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes


CS Exams/PSUs
                        



ISRO
                        


ISRO CS Original Papers and Official Keys

ISRO CS Solved Papers

ISRO CS Syllabus for Scientist/Engineer Exam


UGC NET
                        


UGC NET CS Notes Paper II

UGC NET CS Notes Paper III

UGC NET CS Solved Papers


Student
                        


Campus Ambassador Program

School Ambassador Program

Project

Geek of the Month

Campus Geek of the Month

Placement Course

Competititve Programming

Testimonials

Geek on the Top

Careers

Internship


Tutorials
                        



Jobs
                        


Apply for Jobs

Post a Job

JOB-A-THON


For Working Professionals
                        



LIVE
                        


DSA Live Classes

System Design

Java Backend Development

Full Stack LIVE

Explore More


DSA Live Classes


System Design


Java Backend Development


Full Stack LIVE


Explore More


Self-Paced
                        


DSA- Self Paced

SDE Theory

Must-Do Coding Questions

Explore More


DSA- Self Paced


SDE Theory


Must-Do Coding Questions


Explore More


For Students
                        



LIVE
                        


Competitive Programming

Data Structures with C++

Data Science

Explore More


Competitive Programming


Data Structures with C++


Data Science


Explore More


Self-Paced
                        


DSA- Self Paced

CIP

JAVA / Python / C++

Explore More


DSA- Self Paced


CIP


JAVA / Python / C++


Explore More


School Courses
                        


School Guide

Python Programming

Learn To Make Apps


School Guide


Python Programming


Learn To Make Apps


Practice DS & Algo.
                        


Must Do Questions

DSA Topic-wise

DSA Company-wise


Must Do Questions


DSA Topic-wise


DSA Company-wise


Algorithms
                        


Searching Algorithms

Sorting Algorithms

Graph Algorithms

Pattern Searching

Geometric Algorithms

Mathematical

Bitwise Algorithms

Randomized Algorithms

Greedy Algorithms

Dynamic Programming

Divide and Conquer

Backtracking

Branch and Bound

All Algorithms


Searching Algorithms


Sorting Algorithms


Graph Algorithms


Pattern Searching


Geometric Algorithms


Mathematical


Bitwise Algorithms


Randomized Algorithms


Greedy Algorithms


Dynamic Programming


Divide and Conquer


Backtracking


Branch and Bound


All Algorithms


Analysis of Algorithms
                        


Asymptotic Analysis

Worst, Average and Best Cases

Asymptotic Notations

Little o and little omega notations

Lower and Upper Bound Theory

Analysis of Loops

Solving Recurrences

Amortized Analysis

What does 'Space Complexity' mean ?

Pseudo-polynomial Algorithms

Polynomial Time Approximation Scheme

A Time Complexity Question


Asymptotic Analysis


Worst, Average and Best Cases


Asymptotic Notations


Little o and little omega notations


Lower and Upper Bound Theory


Analysis of Loops


Solving Recurrences


Amortized Analysis


What does 'Space Complexity' mean ?


Pseudo-polynomial Algorithms


Polynomial Time Approximation Scheme


A Time Complexity Question


Data Structures
                        


Arrays

Linked List

Stack

Queue

Binary Tree

Binary Search Tree

Heap

Hashing

Graph

Advanced Data Structure

Matrix

Strings

All Data Structures


Arrays


Linked List


Stack


Queue


Binary Tree


Binary Search Tree


Heap


Hashing


Graph


Advanced Data Structure


Matrix


Strings


All Data Structures


Interview Corner
                        


Company Preparation

Top Topics

Practice Company Questions

Interview Experiences

Experienced Interviews

Internship Interviews

Competititve Programming

Design Patterns

System Design Tutorial

Multiple Choice Quizzes


Company Preparation


Top Topics


Practice Company Questions


Interview Experiences


Experienced Interviews


Internship Interviews


Competititve Programming


Design Patterns


System Design Tutorial


Multiple Choice Quizzes


Languages
                        


C

C++

Java

Python

C#

JavaScript

jQuery

SQL

PHP

Scala

Perl

Go Language

HTML

CSS

Kotlin


C


C++


Java


Python


C#


JavaScript


jQuery


SQL


PHP


Scala


Perl


Go Language


HTML


CSS


Kotlin


CS Subjects
                        


Mathematics

Operating System

DBMS

Computer Networks

Computer Organization and Architecture

Theory of Computation

Compiler Design

Digital Logic

Software Engineering


Mathematics


Operating System


DBMS


Computer Networks


Computer Organization and Architecture


Theory of Computation


Compiler Design


Digital Logic


Software Engineering


GATE
                        


GATE Computer Science Notes

Last Minute Notes

GATE CS Solved Papers

GATE CS Original Papers and Official Keys

GATE 2021 Dates

GATE CS 2021 Syllabus

Important Topics for GATE CS


GATE Computer Science Notes


Last Minute Notes


GATE CS Solved Papers


GATE CS Original Papers and Official Keys


GATE 2021 Dates


GATE CS 2021 Syllabus


Important Topics for GATE CS


Web Technologies
                        


HTML

CSS

JavaScript

AngularJS

ReactJS

NodeJS

Bootstrap

jQuery

PHP


HTML


CSS


JavaScript


AngularJS


ReactJS


NodeJS


Bootstrap


jQuery


PHP


Software Designs
                        


Software Design Patterns

System Design Tutorial


Software Design Patterns


System Design Tutorial


School Learning
                        


School Programming


School Programming


Mathematics
                        


Number System

Algebra

Trigonometry

Statistics

Probability

Geometry

Mensuration

Calculus


Number System


Algebra


Trigonometry


Statistics


Probability


Geometry


Mensuration


Calculus


Maths Notes (Class 8-12)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes

Class 12 Notes


Class 8 Notes


Class 9 Notes


Class 10 Notes


Class 11 Notes


Class 12 Notes


NCERT Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


Class 8 Maths Solution


Class 9 Maths Solution


Class 10 Maths Solution


Class 11 Maths Solution


Class 12 Maths Solution


RD Sharma Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


Class 8 Maths Solution


Class 9 Maths Solution


Class 10 Maths Solution


Class 11 Maths Solution


Class 12 Maths Solution


Physics Notes (Class 8-11)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes


Class 8 Notes


Class 9 Notes


Class 10 Notes


Class 11 Notes


CS Exams/PSUs
                        



ISRO
                        


ISRO CS Original Papers and Official Keys

ISRO CS Solved Papers

ISRO CS Syllabus for Scientist/Engineer Exam


ISRO CS Original Papers and Official Keys


ISRO CS Solved Papers


ISRO CS Syllabus for Scientist/Engineer Exam


UGC NET
                        


UGC NET CS Notes Paper II

UGC NET CS Notes Paper III

UGC NET CS Solved Papers


UGC NET CS Notes Paper II


UGC NET CS Notes Paper III


UGC NET CS Solved Papers


Student
                        


Campus Ambassador Program

School Ambassador Program

Project

Geek of the Month

Campus Geek of the Month

Placement Course

Competititve Programming

Testimonials

Geek on the Top

Careers

Internship


Campus Ambassador Program


School Ambassador Program


Project


Geek of the Month


Campus Geek of the Month


Placement Course


Competititve Programming


Testimonials


Geek on the Top


Careers


Internship


Tutorials
                        



Jobs
                        


Apply for Jobs

Post a Job

JOB-A-THON


Apply for Jobs


Post a Job


JOB-A-THON

GBlog
Puzzles
What's New ?
Array
Matrix
Strings
Hashing
Linked List
Stack
Queue
Binary Tree
Binary Search Tree
Heap
Graph
Searching
Sorting
Divide & Conquer
Mathematical
Geometric
Bitwise
Greedy
Backtracking
Branch and Bound
Dynamic Programming
Pattern Searching
Randomized
Sqrt (or Square Root) Decomposition Technique | Set 1 (Introduction)
Sparse Table
Range Minimum Query (Square Root Decomposition and Sparse Table)
Segment Tree | Set 2 (Range Minimum Query)
Segment Tree | Set 1 (Sum of given range)
Segment tree | Efficient implementation
Lazy Propagation in Segment Tree
Persistent Segment Tree | Set 1 (Introduction)
Persistent data structures
Advantages of Trie Data Structure
Trie | (Insert and Search)
Trie | (Delete)
Longest prefix matching – A Trie based solution in Java
Pattern Searching using a Trie of all Suffixes
Pattern Searching using Suffix Tree
Ukkonen’s Suffix Tree Construction – Part 1
Ukkonen’s Suffix Tree Construction – Part 2
Ukkonen’s Suffix Tree Construction – Part 3
Ukkonen’s Suffix Tree Construction – Part 4
Ukkonen’s Suffix Tree Construction – Part 5
Ukkonen’s Suffix Tree Construction – Part 6
Generalized Suffix Tree 1
Suffix Tree Application 1 – Substring Check
Suffix Tree Application 2 – Searching All Patterns
Suffix Tree Application 3 – Longest Repeated Substring
AVL Tree | Set 1 (Insertion)
LRU Cache Implementation
Red-Black Tree | Set 1 (Introduction)
Introduction of B-Tree
Sqrt (or Square Root) Decomposition Technique | Set 1 (Introduction)
Sparse Table
Range Minimum Query (Square Root Decomposition and Sparse Table)
Segment Tree | Set 2 (Range Minimum Query)
Segment Tree | Set 1 (Sum of given range)
Segment tree | Efficient implementation
Lazy Propagation in Segment Tree
Persistent Segment Tree | Set 1 (Introduction)
Persistent data structures
Advantages of Trie Data Structure
Trie | (Insert and Search)
Trie | (Delete)
Longest prefix matching – A Trie based solution in Java
Pattern Searching using a Trie of all Suffixes
Pattern Searching using Suffix Tree
Ukkonen’s Suffix Tree Construction – Part 1
Ukkonen’s Suffix Tree Construction – Part 2
Ukkonen’s Suffix Tree Construction – Part 3
Ukkonen’s Suffix Tree Construction – Part 4
Ukkonen’s Suffix Tree Construction – Part 5
Ukkonen’s Suffix Tree Construction – Part 6
Generalized Suffix Tree 1
Suffix Tree Application 1 – Substring Check
Suffix Tree Application 2 – Searching All Patterns
Suffix Tree Application 3 – Longest Repeated Substring
AVL Tree | Set 1 (Insertion)
LRU Cache Implementation
Red-Black Tree | Set 1 (Introduction)
Introduction of B-Tree
Difficulty Level :
Hard
We have briefly discussed sparse table in Range Minimum Query (Square Root Decomposition and Sparse Table)Sparse table concept is used for fast queries on a set of static data (elements do not change). It does preprocessing so that the queries can be answered efficiently.  
Example Problem 1 : Range Minimum Query
We have an array arr[0 . . . n-1]. We need to efficiently find the minimum value from index L (query start) to R (query end) where 0 <= L <= R <= n-1. Consider a situation when there are many range queries. Example: 
Input:  arr[]   = {7, 2, 3, 0, 5, 10, 3, 12, 18};
        query[] = [0, 4], [4, 7], [7, 8]

Output: Minimum of [0, 4] is 0
        Minimum of [4, 7] is 3
        Minimum of [7, 8] is 12
The idea is to precompute minimum of all subarrays of size 2j where j varies from 0 to Log n. We make a table lookup[i][j] such that lookup[i][j] contains minimum of range starting from i and of size 2j. For example lookup[0][3] contains minimum of range [0, 7] (starting with 0 and of size 23)How to fill this lookup or sparse table? The idea is simple, fill in a bottom-up manner using previously computed values. We compute ranges with current power of 2 using values of lower power of two. For example, to find a minimum of range [0, 7] (Range size is a power of 3), we can use the minimum of following two. a) Minimum of range [0, 3] (Range size is a power of 2) b) Minimum of range [4, 7] (Range size is a power of 2)Based on above example, below is formula, 
// Minimum of single element subarrays is same
// as the only element.
lookup[i][0] = arr[i]

// If lookup[0][2] <=  lookup[4][2], 
// then lookup[0][3] = lookup[0][2]
If lookup[i][j-1] <= lookup[i+2j-1][j-1]
   lookup[i][j] = lookup[i][j-1]

// If lookup[0][2] >  lookup[4][2], 
// then lookup[0][3] = lookup[4][2]
Else 
   lookup[i][j] = lookup[i+2j-1][j-1] 
 
For any arbitrary range [l, R], we need to use ranges which are in powers of 2. The idea is to use the closest power of 2. We always need to do at most one comparison (compare the minimum of two ranges which are powers of 2). One range starts with L and ends with “L + closest-power-of-2”. The other range ends at R and starts with “R – same-closest-power-of-2 + 1”. For example, if the given range is (2, 10), we compare the minimum of two ranges (2, 9) and (3, 10). Based on above example, below is formula, 
// For (2, 10), j = floor(Log2(10-2+1)) = 3
j = floor(Log(R-L+1))

// If lookup[2][3] <=  lookup[3][3], 
// then RMQ(2, 10) = lookup[2][3]
If lookup[L][j] <= lookup[R-(int)pow(2, j)+1][j]
   RMQ(L, R) = lookup[L][j]

// If lookup[2][3] >  arr[lookup[3][3], 
// then RMQ(2, 10) = lookup[3][3]
Else 
   RMQ(L, R) = lookup[R-(int)pow(2, j)+1][j]
Since we do only one comparison, the time complexity of query is O(1).Below is the implementation of the above idea. 
C++
Java
Python3
C#
Javascript
// C++ program to do range minimum query// using sparse table#include <bits/stdc++.h>using namespace std;#define MAX 500 // lookup[i][j] is going to store minimum// value in arr[i..j]. Ideally lookup table// size should not be fixed and should be// determined using n Log n. It is kept// constant to keep code simple.int lookup[MAX][MAX]; // Fills lookup array lookup[][] in bottom up manner.void buildSparseTable(int arr[], int n){    // Initialize M for the intervals with length 1    for (int i = 0; i < n; i++)        lookup[i][0] = arr[i];     // Compute values from smaller to bigger intervals    for (int j = 1; (1 << j) <= n; j++) {         // Compute minimum value for all intervals with        // size 2^j        for (int i = 0; (i + (1 << j) - 1) < n; i++) {             // For arr[2][10], we compare arr[lookup[0][7]]            // and arr[lookup[3][10]]            if (lookup[i][j - 1] <                        lookup[i + (1 << (j - 1))][j - 1])                lookup[i][j] = lookup[i][j - 1];            else                lookup[i][j] =                         lookup[i + (1 << (j - 1))][j - 1];        }    }} // Returns minimum of arr[L..R]int query(int L, int R){    // Find highest power of 2 that is smaller    // than or equal to count of elements in given    // range. For [2, 10], j = 3    int j = (int)log2(R - L + 1);     // Compute minimum of last 2^j elements with first    // 2^j elements in range.    // For [2, 10], we compare arr[lookup[0][3]] and    // arr[lookup[3][3]],    if (lookup[L][j] <= lookup[R - (1 << j) + 1][j])        return lookup[L][j];     else        return lookup[R - (1 << j) + 1][j];} // Driver programint main(){    int a[] = { 7, 2, 3, 0, 5, 10, 3, 12, 18 };    int n = sizeof(a) / sizeof(a[0]);    buildSparseTable(a, n);    cout << query(0, 4) << endl;    cout << query(4, 7) << endl;    cout << query(7, 8) << endl;    return 0;}
// Java program to do range minimum query// using sparse tableimport java.io.*; class GFG {     static int MAX =500;         // lookup[i][j] is going to store minimum    // value in arr[i..j]. Ideally lookup table    // size should not be fixed and should be    // determined using n Log n. It is kept    // constant to keep code simple.    static int [][]lookup = new int[MAX][MAX];         // Fills lookup array lookup[][] in bottom up manner.    static void buildSparseTable(int arr[], int n)    {                 // Initialize M for the intervals with length 1        for (int i = 0; i < n; i++)            lookup[i][0] = arr[i];             // Compute values from smaller to bigger intervals        for (int j = 1; (1 << j) <= n; j++) {                 // Compute minimum value for all intervals with            // size 2^j            for (int i = 0; (i + (1 << j) - 1) < n; i++) {                     // For arr[2][10], we compare arr[lookup[0][7]]                // and arr[lookup[3][10]]                if (lookup[i][j - 1] <                            lookup[i + (1 << (j - 1))][j - 1])                    lookup[i][j] = lookup[i][j - 1];                else                    lookup[i][j] =                            lookup[i + (1 << (j - 1))][j - 1];            }        }    }         // Returns minimum of arr[L..R]    static int query(int L, int R)    {                 // Find highest power of 2 that is smaller        // than or equal to count of elements in given        // range. For [2, 10], j = 3        int j = (int)Math.log(R - L + 1);             // Compute minimum of last 2^j elements with first        // 2^j elements in range.        // For [2, 10], we compare arr[lookup[0][3]] and        // arr[lookup[3][3]],        if (lookup[L][j] <= lookup[R - (1 << j) + 1][j])            return lookup[L][j];             else            return lookup[R - (1 << j) + 1][j];    }         // Driver program    public static void main (String[] args)    {        int a[] = { 7, 2, 3, 0, 5, 10, 3, 12, 18 };        int n = a.length;                 buildSparseTable(a, n);                 System.out.println(query(0, 4));        System.out.println(query(4, 7));        System.out.println(query(7, 8));         }} // This code is contributed by vt_m.
# Python3 program to do range minimum# query using sparse tableimport math # Fills lookup array lookup[][] in# bottom up manner.def buildSparseTable(arr, n):     # Initialize M for the intervals    # with length 1    for i in range(0, n):        lookup[i][0] = arr[i]         j = 1         # Compute values from smaller to    # bigger intervals    while (1 << j) <= n:         # Compute minimum value for all        # intervals with size 2^j        i = 0        while (i + (1 << j) - 1) < n:             # For arr[2][10], we compare arr[lookup[0][7]]            # and arr[lookup[3][10]]            if (lookup[i][j - 1] <                lookup[i + (1 << (j - 1))][j - 1]):                lookup[i][j] = lookup[i][j - 1]            else:                lookup[i][j] = \                        lookup[i + (1 << (j - 1))][j - 1]                         i += 1        j += 1        # Returns minimum of arr[L..R]def query(L, R):     # Find highest power of 2 that is smaller    # than or equal to count of elements in    # given range. For [2, 10], j = 3    j = int(math.log2(R - L + 1))     # Compute minimum of last 2^j elements    # with first 2^j elements in range.    # For [2, 10], we compare arr[lookup[0][3]]    # and arr[lookup[3][3]],    if lookup[L][j] <= lookup[R - (1 << j) + 1][j]:        return lookup[L][j]     else:        return lookup[R - (1 << j) + 1][j] # Driver Codeif __name__ == "__main__":     a = [7, 2, 3, 0, 5, 10, 3, 12, 18]    n = len(a)    MAX = 500         # lookup[i][j] is going to store minimum    # value in arr[i..j]. Ideally lookup table    # size should not be fixed and should be    # determined using n Log n. It is kept    # constant to keep code simple.    lookup = [[0 for i in range(MAX)]                 for j in range(MAX)]     buildSparseTable(a, n)    print(query(0, 4))    print(query(4, 7))    print(query(7, 8))     # This code is contributed by Rituraj Jain
// C# program to do range minimum query// using sparse tableusing System; public class GFG {         static int MAX= 500;         // lookup[i][j] is going to store minimum    // value in arr[i..j]. Ideally lookup table    // size should not be fixed and should be    // determined using n Log n. It is kept    // constant to keep code simple.    static int [,]lookup = new int[MAX, MAX];         // Fills lookup array lookup[][] in bottom up manner.    static void buildSparseTable(int []arr, int n)    {        // Initialize M for the intervals with length 1        for (int i = 0; i < n; i++)            lookup[i, 0] = arr[i];             // Compute values from smaller to bigger intervals        for (int j = 1; (1 << j) <= n; j++) {                 // Compute minimum value for all intervals with            // size 2^j            for (int i = 0; (i + (1 << j) - 1) < n; i++) {                     // For arr[2][10], we compare arr[lookup[0][7]]                // and arr[lookup[3][10]]                if (lookup[i, j - 1] <                            lookup[i + (1 << (j - 1)), j - 1])                    lookup[i, j] = lookup[i, j - 1];                else                    lookup[i, j] =                            lookup[i + (1 << (j - 1)), j - 1];            }        }    }         // Returns minimum of arr[L..R]    static int query(int L, int R)    {                 // Find highest power of 2 that is smaller        // than or equal to count of elements in given        // range. For [2, 10], j = 3        int j = (int)Math.Log(R - L + 1);             // Compute minimum of last 2^j elements with first        // 2^j elements in range.        // For [2, 10], we compare arr[lookup[0][3]] and        // arr[lookup[3][3]],        if (lookup[L, j] <= lookup[R - (1 << j) + 1, j])            return lookup[L, j];             else            return lookup[R - (1 << j) + 1, j];    }         // Driver program    static public void Main ()    {        int []a = { 7, 2, 3, 0, 5, 10, 3, 12, 18 };        int n = a.Length;                 buildSparseTable(a, n);                 Console.WriteLine(query(0, 4));        Console.WriteLine(query(4, 7));        Console.WriteLine(query(7, 8));    }} // This code is contributed by vt_m.
<script> // Javascript program to do range minimum query// using sparse tablevar MAX = 500; // lookup[i][j] is going to store minimum// value in arr[i..j]. Ideally lookup table// size should not be fixed and should be// determined using n Log n. It is kept// constant to keep code simple.var lookup = Array.from(Array(MAX), ()=>Array(MAX)); // Fills lookup array lookup[][] in bottom up manner.function buildSparseTable(arr, n){    // Initialize M for the intervals with length 1    for (var i = 0; i < n; i++)        lookup[i][0] = arr[i];     // Compute values from smaller to bigger intervals    for (var j = 1; (1 << j) <= n; j++) {         // Compute minimum value for all intervals with        // size 2^j        for (var i = 0; (i + (1 << j) - 1) < n; i++) {             // For arr[2][10], we compare arr[lookup[0][7]]            // and arr[lookup[3][10]]            if (lookup[i][j - 1] <                        lookup[i + (1 << (j - 1))][j - 1])                lookup[i][j] = lookup[i][j - 1];            else                lookup[i][j] =                         lookup[i + (1 << (j - 1))][j - 1];        }    }} // Returns minimum of arr[L..R]function query(L, R){    // Find highest power of 2 that is smaller    // than or equal to count of elements in given    // range. For [2, 10], j = 3    var j = parseInt(Math.log2(R - L + 1));     // Compute minimum of last 2^j elements with first    // 2^j elements in range.    // For [2, 10], we compare arr[lookup[0][3]] and    // arr[lookup[3][3]],    if (lookup[L][j] <= lookup[R - (1 << j) + 1][j])        return lookup[L][j];     else        return lookup[R - (1 << j) + 1][j];} // Driver programvar a = [7, 2, 3, 0, 5, 10, 3, 12, 18];var n = a.length;buildSparseTable(a, n);document.write( query(0, 4) + "<br>");document.write( query(4, 7) + "<br>");document.write( query(7, 8)); // This code is contributed by noob2000.</script>
0
3
12
Time Complexity: O(n*n)
Auxiliary Space: O(n*n)So sparse table method supports query operation in O(1) time with O(n Log n) preprocessing time and O(n Log n) space.   
Example Problem 2 : Range GCD Query
We have an array arr[0 . . . n-1]. We need to find the greatest common divisor in the range L and R where 0 <= L <= R <= n-1. Consider a situation when there are many range queries Examples:  
Input : arr[] = {2, 3, 5, 4, 6, 8}
        queries[] = {(0, 2), (3, 5), (2, 3)}
Output : 1
         2
         1
We use below properties of GCD:
GCD function is associative [ GCD(a, b, c) = GCD(GCD(a, b), c) = GCD(a, GCD(b, c))], we can compute GCD of a range using GCDs of subranges.
If we take GCD of an overlapping range more than once, then it does not change answer. For example GCD(a, b, c) = GCD(GCD(a, b), GCD(b, c)). Therefore like minimum range query problem, we need to do only one comparison to find GCD of given range.
We build a sparse table using the same logic as above. After building the sparse table, we can find all GCDs by breaking given range in powers of 2 and add GCD of every piece to the current answer. 
C++
Java
Python3
C#
Javascript
// C++ program to do range minimum query// using sparse table#include <bits/stdc++.h>using namespace std;#define MAX 500 // lookup[i][j] is going to store GCD of// arr[i..j]. Ideally lookup table// size should not be fixed and should be// determined using n Log n. It is kept// constant to keep code simple.int table[MAX][MAX]; // it builds sparse table.void buildSparseTable(int arr[], int n){    // GCD of single element is element itself    for (int i = 0; i < n; i++)        table[i][0] = arr[i];     // Build sparse table    for (int j = 1; j <= log2(n); j++)        for (int i = 0; i <= n - (1 << j); i++)            table[i][j] = __gcd(table[i][j - 1],                    table[i + (1 << (j - 1))][j - 1]);} // Returns GCD of arr[L..R]int query(int L, int R){    // Find highest power of 2 that is smaller    // than or equal to count of elements in given    // range.For [2, 10], j = 3    int j = (int)log2(R - L + 1);     // Compute GCD of last 2^j elements with first    // 2^j elements in range.    // For [2, 10], we find GCD of arr[lookup[0][3]] and    // arr[lookup[3][3]],    return __gcd(table[L][j], table[R - (1 << j) + 1][j]);} // Driver programint main(){    int a[] = { 7, 2, 3, 0, 5, 10, 3, 12, 18 };    int n = sizeof(a) / sizeof(a[0]);    buildSparseTable(a, n);    cout << query(0, 2) << endl;    cout << query(1, 3) << endl;    cout << query(4, 5) << endl;    return 0;}
// Java program to do range minimum query// using sparse tableimport java.util.*; class GFG{static final int MAX = 500; // lookup[i][j] is going to store GCD of// arr[i..j]. Ideally lookup table// size should not be fixed and should be// determined using n Log n. It is kept// constant to keep code simple.static int [][]table = new int[MAX][MAX]; // it builds sparse table.static void buildSparseTable(int arr[],                             int n){    // GCD of single element is    // element itself    for (int i = 0; i < n; i++)        table[i][0] = arr[i];     // Build sparse table    for (int j = 1; j <= n; j++)        for (int i = 0; i <= n - (1 << j); i++)            table[i][j] = __gcd(table[i][j - 1],                                table[i + (1 << (j - 1))][j - 1]);} // Returns GCD of arr[L..R]static int query(int L, int R){    // Find highest power of 2 that is    // smaller than or equal to count of    // elements in given range.For [2, 10], j = 3    int j = (int)Math.log(R - L + 1);     // Compute GCD of last 2^j elements    // with first 2^j elements in range.    // For [2, 10], we find GCD of    // arr[lookup[0][3]] and arr[lookup[3][3]],    return __gcd(table[L][j],                 table[R - (1 << j) + 1][j]);} static int __gcd(int a, int b){    return b == 0 ? a : __gcd(b, a % b);    } // Driver Codepublic static void main(String[] args){    int a[] = { 7, 2, 3, 0, 5, 10, 3, 12, 18 };    int n = a.length;    buildSparseTable(a, n);    System.out.print(query(0, 2) + "\n");    System.out.print(query(1, 3) + "\n");    System.out.print(query(4, 5) + "\n");}} // This code is contributed by PrinciRaj1992
# Python3 program to do range minimum# query using sparse tableimport math # Fills lookup array lookup[][] in# bottom up manner.def buildSparseTable(arr, n):     # GCD of single element is element itself    for i in range(0, n):        table[i][0] = arr[i]     # Build sparse table    j = 1    while (1 << j) <= n:        i = 0        while i <= n - (1 << j):            table[i][j] = math.gcd(table[i][j - 1],                                   table[i + (1 << (j - 1))][j - 1])                         i += 1        j += 1 # Returns minimum of arr[L..R]def query(L, R):     # Find highest power of 2 that is smaller    # than or equal to count of elements in    # given range. For [2, 10], j = 3    j = int(math.log2(R - L + 1))     # Compute GCD of last 2^j elements with    # first 2^j elements in range.    # For [2, 10], we find GCD of arr[lookup[0][3]]    # and arr[lookup[3][3]],    return math.gcd(table[L][j],                    table[R - (1 << j) + 1][j])     # Driver Codeif __name__ == "__main__":     a = [7, 2, 3, 0, 5, 10, 3, 12, 18]    n = len(a)    MAX = 500         # lookup[i][j] is going to store minimum    # value in arr[i..j]. Ideally lookup table    # size should not be fixed and should be    # determined using n Log n. It is kept    # constant to keep code simple.    table = [[0 for i in range(MAX)]                for j in range(MAX)]     buildSparseTable(a, n)    print(query(0, 2))    print(query(1, 3))    print(query(4, 5))     # This code is contributed by Rituraj Jain
// C# program to do range minimum query// using sparse tableusing System; class GFG{static readonly int MAX = 500; // lookup[i,j] is going to store GCD of// arr[i..j]. Ideally lookup table// size should not be fixed and should be// determined using n Log n. It is kept// constant to keep code simple.static int [,]table = new int[MAX, MAX]; // it builds sparse table.static void buildSparseTable(int []arr,                             int n){    // GCD of single element is    // element itself    for (int i = 0; i < n; i++)        table[i, 0] = arr[i];     // Build sparse table    for (int j = 1; j <= n; j++)        for (int i = 0; i <= n - (1 << j); i++)            table[i, j] = __gcd(table[i, j - 1],                                table[i + (1 << (j - 1)),                                                 j - 1]);} // Returns GCD of arr[L..R]static int query(int L, int R){    // Find highest power of 2 that is    // smaller than or equal to count of    // elements in given range.    // For [2, 10], j = 3    int j = (int)Math.Log(R - L + 1);     // Compute GCD of last 2^j elements    // with first 2^j elements in range.    // For [2, 10], we find GCD of    // arr[lookup[0,3]] and arr[lookup[3,3]],    return __gcd(table[L, j],                 table[R - (1 << j) + 1, j]);} static int __gcd(int a, int b){    return b == 0 ? a : __gcd(b, a % b);    } // Driver Codepublic static void Main(String[] args){    int []a = { 7, 2, 3, 0, 5, 10, 3, 12, 18 };    int n = a.Length;    buildSparseTable(a, n);    Console.Write(query(0, 2) + "\n");    Console.Write(query(1, 3) + "\n");    Console.Write(query(4, 5) + "\n");}} // This code is contributed by Rajput-Ji
<script>// Javascript program to do range minimum query// using sparse tablelet MAX = 500; // lookup[i][j] is going to store GCD of// arr[i..j]. Ideally lookup table// size should not be fixed and should be// determined using n Log n. It is kept// constant to keep code simple.let table = new Array(MAX);for(let i = 0; i < MAX; i++){    table[i] = new Array(MAX);    for(let j = 0; j < MAX; j++)        table[i][j] = 0;} // it builds sparse table.function buildSparseTable(arr,n){    // GCD of single element is    // element itself    for (let i = 0; i < n; i++)        table[i][0] = arr[i];      // Build sparse table    for (let j = 1; j <= n; j++)        for (let i = 0; i <= n - (1 << j); i++)            table[i][j] = __gcd(table[i][j - 1],                                table[i + (1 << (j - 1))][j - 1]);} // Returns GCD of arr[L..R]function query(L,R){    // Find highest power of 2 that is    // smaller than or equal to count of    // elements in given range.For [2, 10], j = 3    let j = Math.floor(Math.log(R - L + 1));      // Compute GCD of last 2^j elements    // with first 2^j elements in range.    // For [2, 10], we find GCD of    // arr[lookup[0][3]] and arr[lookup[3][3]],    return __gcd(table[L][j],                 table[R - (1 << j) + 1][j]);} function __gcd(a,b){    return b == 0 ? a : __gcd(b, a % b);   } // Driver Codelet a = [7, 2, 3, 0, 5, 10, 3, 12, 18];let n = a.length;buildSparseTable(a, n);document.write(query(0, 2) + "<br>");document.write(query(1, 3) + "<br>");document.write(query(4, 5) + "<br>"); // This code is contributed by avanitrachhadiya2155</script>
1
1
5
Time Complexity: O(n*n)
Auxiliary Space: O(n*n) 
kaushalkumarroy
rituraj_jain
ajit12
princiraj1992
Rajput-Ji
nit1n
noob2000
quannguyenhoang79
avanitrachhadiya2155
satyamchhatrala
rishavnitro
array-range-queries
Advanced Data Structure
Arrays
Technical Scripter
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Agents in Artificial Intelligence
Decision Tree Introduction with example
AVL Tree | Set 2 (Deletion)
Red-Black Tree | Set 2 (Insert)
Disjoint Set Data Structures
Arrays in Java
Arrays in C/C++
Program for array rotation
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews