Problem solution · C++

Count of Range Sum

Count of Range Sum: a C++ solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
63 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Count of Range Sum, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 63 lines of C++ from the credited upstream file 327.cpp.
  • The implementation visibly relies on sequence storage.
  • 7 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeCount of Range Sum · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int countRangeSum(vector<int>& nums, int lower, int upper) {    const int n = nums.size();    int ans = 0;    vector<long> prefix{0};     for (int i = 0; i < n; ++i)      prefix.push_back(prefix.back() + nums[i]);     mergeSort(prefix, 0, n, lower, upper, ans);    return ans;  }  private:  void mergeSort(vector<long>& prefix, int l, int r, int lower, int upper,                 int& ans) {    if (l >= r)      return;     const int m = (l + r) / 2;    mergeSort(prefix, l, m, lower, upper, ans);    mergeSort(prefix, m + 1, r, lower, upper, ans);    merge(prefix, l, m, r, lower, upper, ans);  }   void merge(vector<long>& prefix, int l, int m, int r, int lower, int upper,             int& ans) {    int lo = m + 1;  // the first index s.t. prefix[lo] - prefix[i] >= lower    int hi = m + 1;  // the first index s.t. prefix[hi] - prefix[i] > upper     // For each index i in range [l, m], add hi - lo to `ans`.    for (int i = l; i <= m; ++i) {      while (lo <= r && prefix[lo] - prefix[i] < lower)        ++lo;      while (hi <= r && prefix[hi] - prefix[i] <= upper)        ++hi;      ans += hi - lo;    }     vector<long> sorted(r - l + 1);    int k = 0;      // sorted's index    int i = l;      // left's index    int j = m + 1;  // right's index     while (i <= m && j <= r)      if (prefix[i] < prefix[j])        sorted[k++] = prefix[i++];      else        sorted[k++] = prefix[j++];     // Put the possible remaining left part into the sorted array.    while (i <= m)      sorted[k++] = prefix[i++];     // Put the possible remaining right part into the sorted array.    while (j <= r)      sorted[k++] = prefix[j++];     copy(sorted.begin(), sorted.end(), prefix.begin() + l);  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗