Count of subsets not containing adjacent elements | Function to return the count of possible subsets ; Total possible subsets of n sized array is ( 2 ^ n - 1 ) ; To store the required count of subsets ; Run from i 000. .0 to 111. .1 ; If current subset has consecutive elements from the array ; Driver code
function cntSubsets ( arr , n ) { var max = Math . pow ( 2 , n ) ; var result = 0 ; for ( var i = 0 ; i < max ; i ++ ) { var counter = i ; if ( counter & ( counter >> 1 ) ) continue ; result ++ ; } return result ; } var arr = [ 3 , 5 , 7 ] ; var n = arr . length ; document . write ( cntSubsets ( arr , n ) ) ;