Array ( [0] => [1] => questions [2] => Arrays [3] => Arithmetic-Progression )
Given list of elements. You have to find the sum of elements which are in AP.
First line contains a number N. Next line contains N integers separated by spaces.
Print the resultant sum value.
5 1 2 3 4 5
15
8 0 2 4 6 8 10 12 14
56
10 1 4 7 10 13 16 19 22 25 28
145
Login to see Discussion
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.
Login to see Solution