Concepts Used

Segment Trees

Difficulty Level

Easy

Problem Statement :

Given an array of N elements and Q queries. In each query he is given two values l, r.
We have to find the minimum value of all the elements from l to r.

See original problem statement here

Solution Approach :

Introduction :

Idea is to construct a segment tree with the leaf nodes having the array values and intermediate nodes stores the minimum value of the current subarray range.
For Example : arr {5,1,4,2,9} is our array then segment tree will store values like this -> {5,1,4,2,9}->1 , {5,1,4}-> 1, {5,1}-> 1, {5}-> 5(leaf), {1}->1 (leaf), {2,9}->2, {2}->2(leaf), {9}->9 (leaf).

Method 1 (Brute force):

We can search for the minimum value in the given range l to r for every query. This approach will work fine for smaller array sizes and queries, as it takes linear time to search for the minimum value for single query. As the size of the input increases this apprach will be huge drawback.

Method 2 (Segment Tree):

As the number of queries and array size is too large for linear search in every query, we will learn programming online and segment tree to solve this problem.

A Segment Tree is a data structure which allows answering range queries very effectively over a large input. Each query takes logarithmic time. Range queries includes sum over a range, or finding a minimum value over a given range etc. Query be of any type we can use segment trees and modify it accordingly.
Leaf nodes of the tree stores the actual array values and intermediate nodes stores the information of subarrays with is require to solve the problem. Lets say if we have to find a sum between different ranges, so now the intermediate nodes will store the sum of the current subarray. We fill the nodes by recursively calling left and right subtree (dividing into segements), untill there is a single element left, which can be directly assigned the value of the array. Array representation of the tree is used to represent segment tree, where (i*2)+1 represents the left node and (i*2)+2 represents right node, parent will be represented by (i-1)/2 for every index i.
>
We will construct our tree by starting at the original array and dividing it into two halves (left and right), untill there is a single element left (leaf) which can directly be filled with a[i] for any index i. Now for every range say l to r, we will store the minimum value of this range in the node.

Now that our tree is constructed, we will answer queries (minimum value in the given range). The queries can be of 3 types:

  1. The range of the tree exactly matches with the query, in this case we will return the value stored in this node.
  2. The range either belongs to the left or right node, in this case we will make two recursive calls for left and right subtrees respectively.
  3. The range overlaps with two of more ranges, in this case we are forced to go to the lower levels of both subtrees and find the minimum value which fits the range and finally return the least of them.

Algorithm :

construct():

  • if the current node is a leaf (subarray contains single element), assign the value directly, (tree[curr]= arr[l]).
  • break the tree into two halves by recursively calling for left and right subtree, construct(l,mid) and construct(mid+1,r)
  • fill the current node with the minimum of left & right node. (tree[curr] = min ((i*2)+1,(i*2)+2).

RMQ():

  • if range is within the current range, return the value stored in node.
  • if range is completely outside, return undefined.
  • else return the minimum of left & right subtrees.

Complexity Analysis :

In segment tree, preprocessing time is O(n) and worst time to for range minimum query is equivalent to the height of the tree.
The space complexity is O(n) to store the segment tree.

Solutions:

[TABS_R id=2298]
[forminator_quiz id=”2299″]

Previous post ADD ONE TO THE NUMBER
Next post Longest Palindromic Substring

Leave a Reply

Your email address will not be published. Required fields are marked *