Sunday, February 3, 2019

Maximum sum such that no two elements are adjacent

Problem Statement : Given an array of numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7).Answer the question in most efficient way.


Hint:

One of three for current index in a for loop 
1. best at (i-2) added to current
2. best at (i-1)
3. current only


Solution:



No comments:

Post a Comment