Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Count of Range Sum problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Count of Range Sum problem solution You are given an integer array nums and two integers lower and upper, return the number of range sums that lie in [lower, upper] inclusive.

Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j inclusive, where i <= j.

Leetcode Count of Range Sum problem solution

Leetcode Count of Range Sum problem solution in Python.

class Solution:
    def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
        res = 0
        presum = 0
        sorted_presum = [0]
        for k in range(len(nums)):
            presum += nums[k]
            idx_c = bisect_right(sorted_presum, presum)
            idx_l = bisect_left(sorted_presum, presum - upper)
            idx_r = bisect_right(sorted_presum, presum - lower)
            res += idx_r - idx_l

            sorted_presum[idx_c:idx_c] = [presum]
            
        return res

Count of Range Sum problem solution in Java.

class Solution {
    public int countRangeSum(int[] nums, int lower, int upper) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        
        return countRanges(nums, lower, upper);
    }
    
    private int countRanges(int[] nums, int lower, int upper) {
        long sum = 0;
        Map<Long, Integer> sumMap = new HashMap<>();
        int count = 0;
        sumMap.put(0l, 1);
        for(int i=0; i<nums.length; i++) {
            sum = sum + nums[i];
            
            for(int j=lower; j<=upper; j++) {
                Integer prev = sumMap.get(sum-j);
                if(prev != null) {
                    count = count + prev;
                }
            }
            
            if(!sumMap.containsKey(sum)) {
                sumMap.put(sum, 0);
            }
            sumMap.put(sum, sumMap.get(sum) + 1);
        }
        
        return count;
    }
}

Problem solution in C++.

int countRangeSum(vector<int>& A, int lower, int upper) {
        int n = A.size();
        vector<long> prefix(n + 1), aux(n + 1);
        for (int i = 0; i < n; ++i)
            prefix[i + 1] = prefix[i] + A[i];
        return countRangeSum(prefix, 0, n + 1, lower, upper, aux);
}

int countRangeSum(vector<long> &prefix, int beg, int end, int lower, int upper, vector<long> &aux) {
    if (end - beg <= 1) return 0;
    auto mid = beg + (end - beg) / 2;
    auto count = countRangeSum(prefix, beg, mid, lower, upper, aux) 
               + countRangeSum(prefix, mid, end, lower, upper, aux);
    auto p = beg;
    for (int i = beg, j = mid, k = mid, t = mid; i < mid; ++i) {
        while (j < end && prefix[j] - prefix[i] <  lower) ++j;
        while (k < end && prefix[k] - prefix[i] <= upper) ++k;
        while (t < end && prefix[t] < prefix[i]) aux[p++] = prefix[t++];
        aux[p++] = prefix[i];
        count += k - j;
    }
    while (beg < p) prefix[beg] = aux[beg++];
    return count;
}

Problem solution in C.

int countConsistentStrings(char * allowed, char ** words, int wordsSize){
int freq[26] = {0};
  int j;
    int count = wordsSize;
    for(int i = 0; i < strlen(allowed); i++){
        freq[allowed[i] - 97]++;
    }

 
    for(int i = 0; i < wordsSize; i++){
        for(j = 0; j < strlen(words[i]); j++){
           if(freq[*(words[i]+ j) -97] == 0){ 
               count = count - 1;
               break;
           }
        } 
    }
    return count;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes