Last active
November 17, 2018 07:10
-
-
Save anujsinghwd/f7fa60cf8e9687f90ab3e7aa99c23fe5 to your computer and use it in GitHub Desktop.
Given an array A of positive integers. Find the maximum sum of a subsequence such that no two numbers in the sequence should be adjacent in the array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$arr = array(5,5,10,100,10,5); | |
$n = count($arr); | |
$inc = $arr[0]; | |
$ex = 0; | |
for($i=1;$i<$n;$i++){ | |
$old_inc = $inc; | |
$inc = max($inc, $ex + $arr[$i]); | |
$ex = $old_inc; | |
} | |
echo 'Max Sum Of:'.$inc; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment