Count of subsets not containing adjacent elements | Function to return the count of possible subsets ; If previous element was 0 then 0 as well as 1 can be appended ; If previous element was 1 then only 0 can be appended ; Store the count of all possible subsets ; Driver code
function cntSubsets ( arr , n ) { var a = Array ( n ) ; var b = Array ( n ) ; a [ 0 ] = b [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } var result = a [ n - 1 ] + b [ n - 1 ] ; return result ; } var arr = [ 3 , 5 , 7 ] ; var n = arr . length ; document . write ( cntSubsets ( arr , n ) ) ;