Last Updated : 11 Jul, 2025
Given an array arr[0 . . . n-1]. The task is to perform the following operation:
Examples:
Input: a[] = {2, 6, 7, 5, 18, 86, 54, 2}
Query1: maximum(2, 7)
Query2: update(3, 90)
Query3: maximum(2, 6)Output:
Maximum in range 2 to 7 is 86.
Maximum in range 2 to 6 is 90.
We have discussed Recursive segment tree implementation. In this post, iterative implementation is discussed.
The iterative version of the segment tree basically uses the fact, that for an index i, left child = 2 * i and right child = 2 * i + 1 in the tree. The parent for an index i in the segment tree array can be found by parent = i / 2. Thus we can easily travel up and down through the levels of the tree one by one. At first we compute the maximum in the ranges while constructing the tree starting from the leaf nodes and climbing up through the levels one by one. We use the same concept while processing the queries for finding the maximum in a range.
Since there are (log n) levels in the worst case, so querying takes log n time. For update of a particular index to a given value we start updating the segment tree starting from the leaf nodes and update all those nodes which are affected by the updation of the current node by gradually moving up through the levels at every iteration. Updation also takes log n time because there we have to update all the levels starting from the leaf node where we update the exact value at the exact index given by the user.
Below is the implementation of the above approach.
C++
// C++ Program to implement
// iterative segment tree.
#include <bits/stdc++.h>
using namespace std;
void construct_segment_tree(vector<int>& segtree,
vector<int>& a, int n)
{
// assign values to leaves of the segment tree
for (int i = 0; i < n; i++)
segtree[n + i] = a[i];
/* assign values to internal nodes
to compute maximum in a given range */
for (int i = n - 1; i >= 1; i--)
segtree[i] = max(segtree[2 * i],
segtree[2 * i + 1]);
}
void update(vector<int>& segtree, int pos, int value,
int n)
{
// change the index to leaf node first
pos += n;
// update the value at the leaf node
// at the exact index
segtree[pos] = value;
while (pos > 1) {
// move up one level at a time in the tree
pos >>= 1;
// update the values in the nodes in
// the next higher level
segtree[pos] = max(segtree[2 * pos],
segtree[2 * pos + 1]);
}
}
int range_query(vector<int>& segtree, int left, int
right,
int n)
{
/* Basically the left and right indices will move
towards right and left respectively and with
every each next higher level and compute the
maximum at each height. */
// change the index to leaf node first
left += n;
right += n;
// initialize maximum to a very low value
int ma = INT_MIN;
while (left < right) {
// if left index in odd
if (left & 1) {
ma = max(ma, segtree[left]);
// make left index even
left++;
}
// if right index in odd
if (right & 1) {
// make right index even
right--;
ma = max(ma, segtree[right]);
}
// move to the next higher level
left /= 2;
right /= 2;
}
return ma;
}
// Driver code
int main()
{
vector<int> a = { 2, 6, 10, 4, 7, 28, 9, 11, 6, 33 };
int n = a.size();
/* Construct the segment tree by assigning
the values to the internal nodes*/
vector<int> segtree(2 * n);
construct_segment_tree(segtree, a, n);
// compute maximum in the range left to right
int left = 1, right = 5;
cout << "Maximum in range " << left << " to "
<< right << " is " << range_query(segtree, left,
right + 1, n)
<< "\n";
// update the value of index 5 to 32
int index = 5, value = 32;
// a[5] = 32;
// Contents of array : {2, 6, 10, 4, 7, 32, 9, 11, 6, 33}
update(segtree, index, value, n);
// compute maximum in the range left to right
left = 2, right = 8;
cout << "Maximum in range " << left << " to "
<< right << " is " << range_query(segtree,
left, right + 1, n)
<< "\n";
return 0;
}
Java
// Java Program for the above approach
import java.util.Arrays;
public class IterativeSegmentTree {
private int[] segtree;
private int n;
public IterativeSegmentTree(int[] a)
{
n = a.length;
segtree = new int[2 * n];
Arrays.fill(segtree, Integer.MIN_VALUE);
constructSegmentTree(a);
}
private void constructSegmentTree(int[] a)
{
// assign values to leaves of the segment tree
for (int i = 0; i < n; i++) {
segtree[n + i] = a[i];
}
/* assign values to internal nodes
to compute maximum in a given range */
for (int i = n - 1; i >= 1; i--) {
segtree[i] = Math.max(segtree[2 * i],
segtree[2 * i + 1]);
}
}
public void update(int pos, int value)
{
// change the index to leaf node first
pos += n;
// update the value at the leaf node
// at the exact index
segtree[pos] = value;
while (pos > 1) {
// move up one level at a time in the tree
pos >>= 1;
// update the values in the nodes in
// the next higher level
segtree[pos] = Math.max(segtree[2 * pos],
segtree[2 * pos + 1]);
}
}
public int rangeQuery(int left, int right)
{
/* Basically the left and right indices will move
towards right and left respectively and with
every each next higher level and compute the
maximum at each height. */
// change the index to leaf node first
left += n;
right += n;
// initialize maximum to a very low value
int ma = Integer.MIN_VALUE;
while (left < right) {
// if left index in odd
if ((left & 1) == 1) {
ma = Math.max(ma, segtree[left]);
// make left index even
left++;
}
// if right index in odd
if ((right & 1) == 1) {
// make right index even
right--;
ma = Math.max(ma, segtree[right]);
}
// move to the next higher level
left /= 2;
right /= 2;
}
return ma;
}
//Driver code
public static void main(String[] args)
{
int[] a = { 2, 6, 10, 4, 7, 28, 9, 11, 6, 33 };
IterativeSegmentTree tree
= new IterativeSegmentTree(a);
// compute maximum in the range left to right
int left = 1, right = 5;
System.out.println(
"Maximum in range " + left + " to " + right
+ " is " + tree.rangeQuery(left, right + 1));
// update the value of index 5 to 32
int index = 5, value = 32;
// a[5] = 32;
// Contents of array : {2, 6, 10, 4, 7, 32, 9, 11,
// 6, 33}
tree.update(index, value);
// compute maximum in the range left to right
left = 2;
right = 8;
System.out.println(
"Maximum in range " + left + " to " + right
+ " is " + tree.rangeQuery(left, right + 1));
}
}
// This code is contributed by lokeshpotta20.
Python3
# Python Program to implement
# iterative segment tree.
from sys import maxsize
INT_MIN = -maxsize
def construct_segment_tree(a: list, n: int):
global segtree
# assign values to leaves of the segment tree
for i in range(n):
segtree[n + i] = a[i]
# assign values to internal nodes
# to compute maximum in a given range */
for i in range(n - 1, 0, -1):
segtree[i] = max(segtree[2 * i], segtree[2 * i + 1])
def update(pos: int, value: int, n: int):
global segtree
# change the index to leaf node first
pos += n
# update the value at the leaf node
# at the exact index
segtree[pos] = value
while pos > 1:
# move up one level at a time in the tree
pos //= 2
# update the values in the nodes in
# the next higher level
segtree[pos] = max(segtree[2 * pos], segtree[2 * pos + 1])
def range_query(left: int, right: int, n: int) -> int:
global segtree
# Basically the left and right indices will move
# towards right and left respectively and with
# every each next higher level and compute the
# maximum at each height.
# change the index to leaf node first
left += n
right += n
# initialize maximum to a very low value
ma = INT_MIN
while left < right:
# if left index in odd
if left & 1:
ma = max(ma, segtree[left])
# make left index even
left += 1
# if right index in odd
if right & 1:
# make right index even
right -= 1
ma = max(ma, segtree[right])
# move to the next higher level
left //= 2
right //= 2
return ma
# Driver Code
if __name__ == "__main__":
a = [2, 6, 10, 4, 7, 28, 9, 11, 6, 33]
n = len(a)
# Construct the segment tree by assigning
# the values to the internal nodes
segtree = [0] * (2 * n)
construct_segment_tree(a, n)
# compute maximum in the range left to right
left = 1
right = 5
print("Maximum in range %d to %d is %d" %
(left, right, range_query(left, right + 1, n)))
# update the value of index 5 to 32
index = 5
value = 32
# a[5] = 32;
# Contents of array : {2, 6, 10, 4, 7, 32, 9, 11, 6, 33}
update(index, value, n)
# compute maximum in the range left to right
left = 2
right = 8
print("Maximum in range %d to %d is %d" %
(left, right, range_query(left, right + 1, n)))
# This code is contributed by
# sanjeev2552
C#
// C# Program for the above approach
using System;
public class IterativeSegmentTree {
static int[] segtree;
static int n;
public IterativeSegmentTree(int[] a)
{
n = a.Length;
segtree = new int[2 * n];
for(int i=0;i<2*n;i++)
{
segtree[i]= Int32.MinValue;
}
constructSegmentTree(a);
}
private void constructSegmentTree(int[] a)
{
// assign values to leaves of the segment tree
for (int i = 0; i < n; i++) {
segtree[n + i] = a[i];
}
/* assign values to internal nodes
to compute maximum in a given range */
for (int i = n - 1; i >= 1; i--) {
segtree[i] = Math.Max(segtree[2 * i],
segtree[2 * i + 1]);
}
}
public void update(int pos, int value)
{
// change the index to leaf node first
pos += n;
// update the value at the leaf node
// at the exact index
segtree[pos] = value;
while (pos > 1) {
// move up one level at a time in the tree
pos >>= 1;
// update the values in the nodes in
// the next higher level
segtree[pos] = Math.Max(segtree[2 * pos],
segtree[2 * pos + 1]);
}
}
public int rangeQuery(int left, int right)
{
/* Basically the left and right indices will move
towards right and left respectively and with
every each next higher level and compute the
maximum at each height. */
// change the index to leaf node first
left += n;
right += n;
// initialize maximum to a very low value
int ma = Int32.MinValue;
while (left < right) {
// if left index in odd
if ((left & 1) == 1) {
ma = Math.Max(ma, segtree[left]);
// make left index even
left++;
}
// if right index in odd
if ((right & 1) == 1) {
// make right index even
right--;
ma = Math.Max(ma, segtree[right]);
}
// move to the next higher level
left /= 2;
right /= 2;
}
return ma;
}
//Driver code
static public void Main ()
{
int[] a = { 2, 6, 10, 4, 7, 28, 9, 11, 6, 33 };
IterativeSegmentTree tree
= new IterativeSegmentTree(a);
// compute maximum in the range left to right
int left = 1, right = 5;
Console.WriteLine(
"Maximum in range " + left + " to " + right
+ " is " + tree.rangeQuery(left, right + 1));
// update the value of index 5 to 32
int index = 5, value = 32;
// a[5] = 32;
// Contents of array : {2, 6, 10, 4, 7, 32, 9, 11,
// 6, 33}
tree.update(index, value);
// compute maximum in the range left to right
left = 2;
right = 8;
Console.WriteLine(
"Maximum in range " + left + " to " + right
+ " is " + tree.rangeQuery(left, right + 1));
}
}
// This code is contributed by Pushpesh Raj
JavaScript
<script>
// Javascript program to implement
// iterative segment tree.
function construct_segment_tree(segtree, a, n)
{
// Assign values to leaves of the segment tree
for(let i = 0; i < n; i++)
segtree[n + i] = a[i];
// Assign values to internal nodes
// to compute maximum in a given range
for(let i = n - 1; i >= 1; i--)
segtree[i] = Math.max(segtree[2 * i],
segtree[2 * i + 1]);
}
function update(segtree, pos, value, n)
{
// Change the index to leaf node first
pos += n;
// Update the value at the leaf node
// at the exact index
segtree[pos] = value;
while (pos > 1)
{
// Move up one level at a time in the tree
pos >>= 1;
// Update the values in the nodes in
// the next higher level
segtree[pos] = Math.max(segtree[2 * pos],
segtree[2 * pos + 1]);
}
}
function range_query(segtree, left, right, n)
{
/* Basically the left and right indices will move
towards right and left respectively and with
every each next higher level and compute the
maximum at each height. */
// change the index to leaf node first
left += n;
right += n;
// Initialize maximum to a very low value
let ma = Number.MIN_VALUE;
while (left < right)
{
// If left index in odd
if ((left & 1) != 0)
{
ma = Math.max(ma, segtree[left]);
// Make left index even
left++;
}
// If right index in odd
if ((right & 1) > 0)
{
// Make right index even
right--;
ma = Math.max(ma, segtree[right]);
}
// Move to the next higher level
left = parseInt(left / 2, 10);
right = parseInt(right / 2, 10);
}
return ma;
}
// Driver code
let a = [ 2, 6, 10, 4, 7, 28, 9, 11, 6, 33 ];
let n = a.length;
// Construct the segment tree by assigning
// the values to the internal nodes
let segtree = new Array(2 * n);
construct_segment_tree(segtree, a, n);
// Compute maximum in the range left to right
let left = 1, right = 5;
document.write("Maximum in range " + left +
" to " + right + " is " +
range_query(segtree, left, right + 1, n) + "</br>");
// Update the value of index 5 to 32
let index = 5, value = 32;
// a[5] = 32;
// Contents of array : {2, 6, 10, 4, 7, 32, 9, 11, 6, 33}
update(segtree, index, value, n);
// Compute maximum in the range left to right
left = 2, right = 8;
document.write("Maximum in range " + left +
" to " + right + " is " +
range_query(segtree, left, right + 1, n) + "</br>");
// This code is contributed by divyesh072019
</script>
Maximum in range 1 to 5 is 28 Maximum in range 2 to 8 is 32
Complexity Analysis:
Related Topic: Segment Tree
In 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
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 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 ProblemRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4