VOOZH about

URL: https://www.geeksforgeeks.org/dsa/range-query-largest-sum-contiguous-subarray/

⇱ Range query for Largest Sum Contiguous Subarray - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Range query for Largest Sum Contiguous Subarray

Last Updated : 11 Jul, 2025

Given an array a[] of size N, and Q queries of two types 1 and 2. For the given query of:

  • Type 1: given l and r (they are positions), and task is to print the Largest sum Contiguous Subarray 
  • Type 2: given type, index, and value, update aindex = value.

Examples: 

Input: a[] = {-2, -3, 4, -1, -2, 1, 5, -3} 
1st query : 1 5 8 
2nd query : 2 1 10 
3rd query : 1 1 3 
Output: 6, 11
Explanation: Initially the array from position 5 to 8 is {-2, 1, 5, -3}
The largest sum contiguous subarray = {1, 5}. Sum = 6
After updating a[1] = 10, array is {10, -3, 4, -1, -2, 1, 5, -3}
The largest sum contiguous subarray from 1 to 3 is {10, -3, 4}.
The sum is 11.

Naive approach: The simplest idea is to use Kadane's algorithm for every type-1 query. The complexity of every type-1 query is O(N) and the type-2 query can be done in constant time

Time Complexity: O(N*Q)
Auxiliary Space: O(1) 

Largest Sum Contiguous Subarray using Segment Tree:

An efficient approach is to build a segment tree where each node stores four values (sum, prefixsum, suffixsum, maxsum), and do a range query on it to find the answer to every query. The parent of a node will store the merging of left and right child. 

The parent node stores the value as mentioned below : 

parent_sum = left_sum + right_sum 
parent_prefixsum = max(left_prefixsum, left_sum + right_prefixsum) 
parent_suffixsum = max(right_suffixsum, right_sum + left_suffixsum) 
parent_maxsum = max(parent_prefixsum, parent_suffixsum, left_maxsum, right_maxsum, left_suffixsum + right_prefixsum)

This can be divided into the following stages

Representation of Segment trees : 

  • Leaf Nodes are the elements of the input array. 
  • Each internal node represents some merging of the leaf nodes. For this problem, merging is done as given above. 
  • An array representation of tree is used to represent Segment Trees. For each node at index i, the left child is at index 2 * i + 1, right child at 2 * i + 2 and the parent is at (i - 1) / 2

Construction of Segment Tree from given array: 

  • Start with a segment arr[0 . . . n-1]. and every time divide the current segment into two halves(if it has not yet become a segment of length 1). 
  • Then call the same procedure on both halves, 
    • For each such segment, store the values in all the four variables as given in the formulae above. 

Update a given value in array and Segment Tree: 

  • Start with the complete segment of the array provided to us. 
  • Every time divide the array into two halves, ignore the half in which the index to be updated is not present
    • Keep on ignoring halves at every step until we reach the leaf node where the value should be updated,.
    • Update the value to the given index. 
    • Now, merge the updated values according to the given formulae to all the nodes that are present in the path we have traversed. 

Answering a query: 

  • For every query, move to the left and right halves of the tree. 
    • Whenever the given range completely overlaps any halve of a tree, return the Node from that half without traversing further in that region. 
    • When a halve of the tree completely lies outside the given range, return INT_MIN. 
    • On partial overlapping of range, traverse in left and right halves and return accordingly. 

Below is the implementation of the above idea.  


Output
6
11

Time Complexity: O(N * logN) 

  • O(N ) For building the tree
  • O(logN) for every type-1 query
  • O(logN) for type-2 query.

Auxiliary Space: O(N)

Related Topic: Segment Tree

Comment