Arithmetic Progression

LEVEL:Beginner

Description

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

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 2 3 4 5
Output
15
Example 2:

Input
8
0 2 4 6 8 10 12 14
Output
56
Example 3:

Input
10
1 4 7 10 13 16 19 22 25 28
Output
145

oops

Login to see Discussion




Approach

Approach-1:

Step-i)Traverse the array and check whether difference between adjacent elements is same or not
Step-ii) If not return "Given array is not in AP"
Step-iii)Else initialize sum variable to 0
Step-iv) Traverse the array and keep adding elements to the sum variable
Step-v) 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






Sign Up Page
oops

Login to see solution/discussion