Array ( [0] => [1] => questions [2] => Arrays [3] => Geometric-Progression )
Given list of elements. You have to find the sum of elements which are in GP.
First line contains a number N. Next line contains N integers separated by spaces.
Print the resultant sum value.
5 1 3 9 27 81
121
8 1 2 4 8 16 32 64 128
255
5 1/16 1/8 ¼ ½ 1
2
Login to see Discussion
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.
Login to see Solution