Array ( [0] => [1] => questions [2] => Arrays [3] => Geometric-Progression ) Arrays | Geometric Progression | THE INQUISITIVE





Geometric Progression

LEVEL:Beginner

Description

Given list of elements. You have to find the sum of elements which are in GP.

Input Format

First line contains a number N. Next line contains N integers separated by spaces.

Output Format

Print the resultant sum value.


Example 1:

Input
5
1 3 9 27 81 
Output
121
Example 2:

Input
8
1 2 4 8 16 32 64 128 
Output
255
Example 3:

Input
5
1/16 1/8 ¼ ½ 1
Output
2

oops

Login to see Discussion




Approach


Step-i)first traverse the entire array check whether the ratio between adjacent elements is same or not
Step-ii) if ratio between adjacent elements is not same return "Not is Geometric Progression"
Step-iii) initialize sum to 0
Step-iv) traverse the array and add element to sum variable
Step-v) repeat step iv till complete array is traversed
Step-vi) return the sum

Time Complexity: O(n)
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution