Maximum subsequence sum such that no three are consecutive | C # program to find the maximum sum such that no three are consecutive using recursion . ; Returns maximum subsequence sum such that no three elements are consecutive ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases ; Driver code
using System ; class GFG { static int [ ] arr = { 100 , 1000 , 100 , 1000 , 1 } ; static int [ ] sum = new int [ 10000 ] ; static int maxSumWO3Consec ( int n ) { if ( sum [ n ] != - 1 ) return sum [ n ] ; if ( n == 0 ) return sum [ n ] = 0 ; if ( n == 1 ) return sum [ n ] = arr [ 0 ] ; if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; return sum [ n ] = Math . Max ( Math . Max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n ] ) , arr [ n ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) ; } public static void Main ( String [ ] args ) { int n = arr . Length ; for ( int i = 0 ; i < sum . Length ; i ++ ) sum [ i ] = - 1 ; Console . WriteLine ( maxSumWO3Consec ( n ) ) ; } }