Count of subsets of integers from 1 to N having no adjacent elements | Java code to count subsets not containing adjacent elements from 1 to N ; Function to count subsets ; Driver code
class GFG { static int countSubsets ( int N ) { if ( N <= 2 ) return N ; if ( N == 3 ) return 2 ; int [ ] DP = new int [ N + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; DP [ 2 ] = 2 ; DP [ 3 ] = 2 ; for ( int i = 4 ; i <= N ; i ++ ) { DP [ i ] = DP [ i - 2 ] + DP [ i - 3 ] ; } return DP [ N ] ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . print ( countSubsets ( N ) ) ; } }