Sum of elements of a Geometric Progression (GP) in a given range
Given a Geometric Progression 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 Geometric Progression elements in the given range.
Note: The range is 1-indexed and1 ? L, R ? N, where N is the size of arr.
Examples:
Input: arr[] = {2, 4, 8, 16, 32, 64, 128, 256}, Q = [[2, 4], [2, 6], [5, 8]]
Output:
28
124
480
Explanation:
Range 1: arr = {4, 8, 16}. Therefore sum = 28
Range 2: arr = {4, 8, 16, 32, 64}. Therefore sum = 124
Range 3: arr = {32, 64, 128, 256}. Therefore sum = 480
Input: arr[] = {7, 7, 7, 7, 7, 7}, Q = [[1, 6], [2, 4], [3, 3]]
Output:
42
21
7
Explanation:
Range 1: arr = {7, 7, 7, 7, 7, 7}. Therefore sum = 42
Range 2: arr = {7, 7, 7}. Therefore sum = 21
Range 3: arr = {7}. Therefore sum = 7
Approach: Since the given sequence is an Geometric progression, the sum can be easily found out in two steps efficiently:
- Get the first element of the range.
- If d = 1, then multiply d*k to it, else multiply the (dk – 1)/(d – 1) to it, where d is the common ratio of the GP and k is number of elements in the range.
For example:
Suppose a[i] be the first element of the range, d be the common ratio of GP and k 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-1]
= a[i] + (a[i] * d) + (a[i] * d * d) + …. + (a[i] * dk)
= a[i] * (1 + d + … + dk)
= a[i] * (dk – 1)/(d – 1)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int arr[], int n,
int left, int right)
{
int k = right - left + 1;
int d = arr[1] / arr[0];
int ans = arr[left - 1];
if (d == 1)
ans = ans * d * k;
else
ans = ans * (( int ) pow (d, k) - 1 /
(d - 1));
return ans;
}
int main()
{
int arr[] = { 2, 4, 8, 16, 32,
64, 128, 256 };
int queries = 3;
int q[][2] = { { 2, 4 }, { 2, 6 },
{ 5, 8 } };
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;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int findSum( int [] arr, int n,
int left, int right)
{
int k = right - left + 1 ;
int d = arr[ 1 ] / arr[ 0 ];
int ans = arr[left - 1 ];
if (d == 1 )
ans = ans * d * k;
else
ans = ans * (( int )Math.pow(d, k) - 1 /
(d - 1 ));
return ans;
}
public static void main(String args[])
{
int [] arr = { 2 , 4 , 8 , 16 , 32 ,
64 , 128 , 256 };
int queries = 3 ;
int [][] q = { { 2 , 4 }, { 2 , 6 }, { 5 , 8 } };
int n = arr.length;
for ( int i = 0 ; i < queries; i++)
System.out.println(findSum(arr, n, q[i][ 0 ],
q[i][ 1 ]));
}
}
|
Python3
def findSum(arr, n, left, right):
k = right - left + 1
d = arr[ 1 ] / / arr[ 0 ]
ans = arr[left - 1 ]
if d = = 1 :
ans = ans * d * k
else :
ans = ans * (d * * k - 1 ) / / (d - 1 )
return ans
if __name__ = = '__main__' :
arr = [ 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 ]
queries = 3
q = [[ 2 , 4 ], [ 2 , 6 ], [ 5 , 8 ]]
n = len (arr)
for i in range (queries):
print (findSum(arr, n, q[i][ 0 ], q[i][ 1 ]))
|
C#
using System;
class GFG{
static int findSum( int [] arr, int n,
int left, int right)
{
int k = right - left + 1;
int d = arr[1] / arr[0];
int ans = arr[left - 1];
if (d == 1)
ans = ans * d * k;
else
ans = ans * (( int )Math.Pow(d, k) - 1 /
(d - 1));
return ans;
}
public static void Main( string []args)
{
int [] arr = { 2, 4, 8, 16, 32,
64, 128, 256 };
int queries = 3;
int [,] q = { { 2, 4 }, { 2, 6 }, { 5, 8 } };
int n = arr.Length;
for ( int i = 0; i < queries; i++)
Console.Write(findSum(arr, n, q[i, 0],
q[i, 1]) + "\n" );
}
}
|
Javascript
<script>
function findSum(arr, n, left, right) {
let k = right - left + 1;
let d = arr[1] / arr[0];
let ans = arr[left - 1];
if (d == 1)
ans = ans * d * k;
else
ans = ans * (Math.pow(d, k) - 1 / (d - 1));
return ans;
}
let arr = [2, 4, 8, 16, 32,
64, 128, 256];
let queries = 3;
let q = [[2, 4], [2, 6],
[5, 8]];
let n = arr.length;
for (let i = 0; i < queries; i++)
document.write(findSum(arr, n, q[i][0], q[i][1]));
</script>
|
- Time complexity: O(Q)
- Space complexity: O(1)
Last Updated :
06 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...