Count of subsets not containing adjacent elements | Java implementation of the approach ; 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 ; Driver code
import java . util . * ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int max = ( int ) Math . pow ( 2 , n ) ; int result = 0 ; for ( int i = 0 ; i < max ; i ++ ) { int counter = i ; if ( ( counter & ( counter >> 1 ) ) > 0 ) continue ; result ++ ; } return result ; } static public void main ( String [ ] arg ) { int arr [ ] = { 3 , 5 , 7 } ; int n = arr . length ; System . out . println ( cntSubsets ( arr , n ) ) ; } }