Company: Wipro-Project engineer-on campus_23may
Difficulty: medium
You are given an array A of N integers. Three elements of A are called non-adjacent if their consecutive indexes all differ by more than 1. What is the maximum sum of three non-adjacent elements from A? For example, given A = [8, -4, -7, -5, -5, -4, 8, 8]: elements in positions 0, 3 and 6 are non-adjacent and their sum is 8 + (-5) + 8 = 11; elements in positions 0, 6, 7 sum up to 8 + 8 + 8 = 24, but are not non-adjacent; elements in positions 0, 5, 7 are non-adjacent and sum up to 8 + (-4) + 8 = 12. The third choice is optimal because there is no way to choose any other three non-adjacent elements with a larger sum. Write a function: def solution(A) that, given an array A of N integers, returns the maximum sum of three non-adjacent elements from A. Examples: Given A = [8, -4, -7, -5, -5, -4, 8, 8], the function should return 12. As explained above, you can choose elements in positions 0, 5 and 7. Given A = [-2, -8, 1, 5, -8, 4, 7, 6], the function should return 15. The optimal choice o