public static int maximumSumNonAdjacentSubSequence(int[] a){
  int incl=a[0], excl=0, prevIncl=incl;
  for (int i=1; i < a.length; i++) {
    incl=excl + a[i];
    excl=Math.max(prevIncl,excl);
    prevIncl=incl;
  }
  return Math.max(incl,excl);
}

public static int maximumSumNonAdjacentSubSequence ( int [ ] a ) { int incl = a [ 0 ] , excl = 0 , prevIncl = incl ; for ( int i = 1 ; i < a . length ; i ++ ) { incl = excl + a [ i ] ; excl = Math . max ( prevIncl , excl ) ; prevIncl = incl ; } return Math . max ( incl , excl ) ; }
given an array of positive numbers , finds the maximum sum of a sub - sequence with the constraint that no 2 numbers in the sub - sequence should be adjacent in the array . < p / > example : 1 ) 3 2 7 10 should return 13 ( sum of 3 and 10 ) 2 ) 3 2 5 10 7 should return 15 ( sum of 3 , 5 and 7 ) . here we maintain 2 variables incl and excl which is max sum till now ( satisfying the constraint ) including the current element and excluding the current element respectively .
