Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Count of Range Sum problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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