Tutorialspoint
Problem
Solution
Submissions

Range Sum using Segment Tree

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Given an integer array "nums" and two integers "lower" and "upper", return the number of range sums that lie in "[lower, upper]" inclusive.

A range sum "S(i, j)" is defined as the sum of the elements in "nums" between indices "I" and "j" inclusive: "nums[i] + nums[i+1] + ... + nums[j]".

Example 1
  • Input: nums = [-2, 5, -1], lower = -2, upper = 2
  • Output: 3
  • Explanation:
    We need to count the number of range sums that lie in [-2, 2].
    The range sums are: S(0, 0) = -2, which is in the range [-2, 2];
    S(0, 1) = -2 + 5 = 3, which is NOT in the range [-2, 2];
    S(0, 2) = -2 + 5 + (-1) = 2, which is in the range [-2, 2];
    S(1, 1) = 5, which is NOT in the range [-2, 2];
    S(1, 2) = 5 + (-1) = 4, which is NOT in the range [-2, 2]; S(2, 2) = -1, which is in the range [-2, 2].
    There are 3 range sums that lie in [-2, 2].
Example 2
  • Input: nums = [0, -3, 2, -3, 4], lower = 3, upper = 5
  • Output: 2
  • Explanation:
    We need to count the number of range sums that lie in [3, 5].
    The range sums that lie in [3, 5] are: S(2, 4) = 2 + (-3) + 4 = 3, and S(4, 4) = 4.
    There are 2 range sums that lie in [3, 5].
Constraints
  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1
  • -10^5 <= lower <= upper <= 10^5
  • The sum of elements in any range [i, j] will not exceed 2^31 - 1
  • Time Complexity: O(n log n), where n is the length of the array
  • Space Complexity: O(n)
ArraysTreeAccentureGoldman Sachs
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Calculate the prefix sums of the array to make range sum queries faster
  • Use a segment tree to efficiently query and update range sums
  • For each prefix sum, count how many previous prefix sums fall within the range [prefixSum - upper, prefixSum - lower]
  • The difference between two prefix sums gives the sum of elements between those indices
  • We need to handle potential overflow due to large integers

Steps to solve by this approach:

 Step 1: Calculate the prefix sums of the array to make range sum queries faster.

 Step 2: Sort all the prefix sums and create a mapping from values to indices (with deduplication).
 Step 3: Implement a segment tree data structure to efficiently count elements within a range.
 Step 4: For each prefix sum, find how many previous prefix sums fall within the range [prefixSum - upper, prefixSum - lower].
 Step 5: Update the segment tree with the current prefix sum after counting.
 Step 6: Use TreeMap to map prefix sum values to indices efficiently.
 Step 7: Be careful with potential integer overflow by using long type for prefix sums.

Submitted Code :