Sum of all integers in given N ranges
Given N ranges of the form [L, R], the task is to find the sum of all integers that lie in any of the given ranges.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: arr[] = {{1, 5}, {3, 7}}, N = 2
Output: 28
Explanation: The set of integers that exist in one or more ranges is {1, 2, 3, 4, 5 , 6, 7}. Hence there sum is 28.Input: ranges[] = {{-12, 15}, {3, 9}, {-5, -2}, {20, 25}, {16, 20}}
Output: 247
Approach: The given problem can be solved by an approach similar to the Merge Overlapping Intervals problem. Below are the steps to follow:
- Sort the intervals based on increasing order of L.
- Push the first interval onto a stack and for each interval do the following:
- If the current interval does not overlap with the stack top, push it.
- If the current interval overlaps with stack top and right end of the current interval is more than that of stack top, update stack top with the value of right end of current interval.
- After traversing through all intervals, the remaining stack contains the merged intervals. The sum of the merged intervals can be calculated using formula for the sum of an Arithmetic Progression as the range [L, R] forms an AP with a common difference as 1 and the number of elements as R – L + 1. The sum is ((L + R)*(R-L+1))/2.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>#define ll long longusing namespace std; // Function to find the sum of all// integers numbers in range [L, R]ll sumInRange(long L, long R){ ll Sum = ((R - L + 1) / 2) * (2 * L + (R - L)); return Sum;} // Function to find sum of all integers// that lie in any of the given rangesll calcSum(vector<pair<long, long> > data, int n){ // Sort intervals in increasing order // according to their first element sort(data.begin(), data.end()); // Merging the overlaping intervals int i, idx = 0; // Loop to iterate through the array for (i = 1; i < n; i++) { // If current interval overlaps // with the previous intervals if ((data[idx].second >= data[i].first) || (data[i].first == data[idx].second + 1)) { // Merge the previou and the // current interval data[idx].second = max(data[idx].second, data[i].second); } else { idx++; data[idx].second = data[i].second; data[idx].first = data[i].first; } } // Stores the required sum ll Sum = 0; // Loop to calculate the sum of all // the remaining merged intervals for (i = 0; i <= idx; i++) { // Add sum of integers // in current range Sum += sumInRange(data[i].first, data[i].second); } // Return the total Sum return Sum;} // Driver Codeint main(){ vector<pair<long, long> > vec = { { -12, 15 }, { 3, 9 }, { -5, -2 }, { 20, 25 }, { 16, 20 } }; cout << calcSum(vec, vec.size()); return 0;} |
247
Time Complexity: O(N*log N)
Auxiliary Space: O(1)


