Given an arithmetic series in arr and Q queries in the form of [L, R], where L is the left boundary of the range and R is the right boundary. The task is to find the sum of the AP elements in the given range.
Note: The range is 1-indexed and 1 ≤ L, R ≤ N, where N is the size of arr.
Examples:
Input: arr[] = {2, 4, 6, 8, 10, 12, 14, 16}, Q = [[2, 4], [2, 6], [5, 8]]
Output:
18
40
52
Explanation:
Range 1: arr = {4, 6, 8}. Therefore sum = 18
Range 2: arr = {4, 6, 8, 10, 12}. Therefore sum = 40
Range 3: arr = {10, 12, 14, 16}. Therefore sum = 52Input: arr[] = {7, 14, 21, 28, 35, 42}, Q = [[1, 6], [2, 4], [3, 3]]
Output:
147
63
21
Explanation:
Range 1: arr = {7, 14, 21, 28, 35, 42}. Therefore sum = 147
Range 2: arr = {14, 21, 28}. Therefore sum = 63
Range 3: arr = {21}. Therefore sum = 21
Approach: Since the given sequence is an arithmetic progression, the sum can be easily found out in two steps efficiently:
- Multiply the first element of the range to the number of elements in the range.
- Add (d*k*(k+1))/2 to it, where d is the common difference of the AP and k is (number of elements in the range – 1) which corresponds to the number of gaps.
For example:
Suppose a[i] be the first element of the range, d be the common difference of AP and k+1 be the number of elements in the given range.
Then the sum of the range would be
= a[i] + a[i+1] + a[i+2] + ….. + a[i+k]
= a[i] + a[i] + d + a[i] + 2 * d + …. + a[i] + k * d
= a[i] * (k + 1) + d * (1 + 2 + … + k)
= a[i] * (k + 1) + (d * k * (k+1))/2
Below is the implementation of the above approach:
CPP
// C++ program to find the sum of elements // of an AP in the given range #include <bits/stdc++.h> using namespace std; // Function to find sum in the given range int findSum(int* arr, int n, int left, int right) { // Find the value of k int k = right - left; // Find the common difference int d = arr[1] - arr[0]; // Find the sum int ans = arr[left - 1] * (k + 1); ans = ans + (d * (k * (k + 1))) / 2; return ans; } // Driver code int main() { int arr[] = { 2, 4, 6, 8, 10, 12, 14, 16 }; int queries = 3; int q[queries][2] = { { 2, 4 }, { 2, 6 }, { 5, 6 } }; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < queries; i++) cout << findSum(arr, n, q[i][0], q[i][1]) << endl; } |
Java
// Java program to find the sum of elements // of an AP in the given range class GFG{ // Function to find sum in the given range static int findSum(int []arr, int n, int left, int right) { // Find the value of k int k = right - left; // Find the common difference int d = arr[1] - arr[0]; // Find the sum int ans = arr[left - 1] * (k + 1); ans = ans + (d * (k * (k + 1))) / 2; return ans; } // Driver code public static void main(String[] args) { int arr[] = { 2, 4, 6, 8, 10, 12, 14, 16 }; int queries = 3; int q[][] = { { 2, 4 }, { 2, 6 }, { 5, 6 } }; int n = arr.length; for (int i = 0; i < queries; i++) System.out.print(findSum(arr, n, q[i][0], q[i][1]) +"\n"); } } // This code is contributed by Princi Singh |
Python3
# Python program to find the sum of elements # of an AP in the given range # Function to find sum in the given range def findSum(arr, n, left, right): # Find the value of k k = right - left; # Find the common difference d = arr[1] - arr[0]; # Find the sum ans = arr[left - 1] * (k + 1); ans = ans + (d * (k * (k + 1))) // 2; return ans; # Driver code if __name__ == '__main__': arr = [ 2, 4, 6, 8, 10, 12, 14, 16 ]; queries = 3; q = [[ 2, 4 ],[ 2, 6 ],[ 5, 6 ]]; n = len(arr); for i in range(queries): print(findSum(arr, n, q[i][0], q[i][1])); # This code is contributed by sapnasingh4991 |
C#
// C# program to find the sum of elements // of an AP in the given range using System; class GFG{ // Function to find sum in the given range static int findSum(int []arr, int n, int left, int right) { // Find the value of k int k = right - left; // Find the common difference int d = arr[1] - arr[0]; // Find the sum int ans = arr[left - 1] * (k + 1); ans = ans + (d * (k * (k + 1))) / 2; return ans; } // Driver code public static void Main(String[] args) { int []arr = { 2, 4, 6, 8, 10, 12, 14, 16 }; int queries = 3; int [,]q = { { 2, 4 }, { 2, 6 }, { 5, 6 } }; int n = arr.Length; for (int i = 0; i < queries; i++) Console.Write(findSum(arr, n, q[i,0], q[i,1]) +"\n"); } } // This code is contributed by PrinciRaj1992 |
18 40 22
Time complexity: O(1)
Space complexity: O(1)
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.
Recommended Posts:
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Minimum elements to be added in a range so that count of elements is divisible by K
- Elements to be added so that all elements of a range are present in array
- Sum of distinct elements when elements are in range 1 to n
- GCD of elements in a given range
- XOR of all the elements in the given range [L, R]
- XOR of a subarray (range of elements)
- XOR of a subarray (range of elements) | Set 2
- Find elements in a given range having at least one odd divisor
- Number of elements with odd factors in given range
- Sum of multiples of Array elements within a given range [L, R]
- Print missing elements that lie in range 0 - 99
- Check if an array contains all elements of a given range
- Sum of elements of a Geometric Progression (GP) in a given range
- Construct sum-array with sum of elements in given range
- Number of elements with even factors in the given range
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Find smallest range containing elements from k lists
- Range Queries for Frequencies of array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

