Skip to content

Instantly share code, notes, and snippets.

@anujsinghwd
Last active November 17, 2018 07:10
Show Gist options
  • Save anujsinghwd/f7fa60cf8e9687f90ab3e7aa99c23fe5 to your computer and use it in GitHub Desktop.
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.
<?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