We get it, stuff happens. Just enter your email address below and we'll send you a link to reset your password!
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
8 0 2 4 6 8 10 12 14
10 1 4 7 10 13 16 19 22 25 28
Login to see Discussion
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)
Let us know if you can come up with a better approach, mail us at email@example.com Your approach will be reviewed and posted with credits to you.
Login to see Solution