Given an integer N, the task is to count the number of subsets formed from an array of integers from 1 to N which doesn’t contain adjacent elements. A subset can’t be chosen if it satisfies the nonadjacent element condition but it is possible to add more elements.
Examples:
Input: N = 4 Output: 3 Explanation: Array is {1, 2, 3, 4} So to satisfy the condition, the subsets formed are : {1, 3}, {2, 4}, {1, 4} Input: N = 5 Output: 4
Approach:
This problem can be solved by using Dynamic Programming. For the last element, we have two choices, either we include it, or we exclude it. Let DP[i] be the number of our desirable subsets ending at index i.

If we select ith index, then we can’t select (i1)th index, but we can select (i2)th index. This makes DP[i2] our new subproblem.

If we don’t select ith index, then we have to select (i1)th index, and further cannot select (i2)th index, but can select (i3)rd index.
So next Subproblem becomes DP[i3]
So the DP relation becomes :
DP[i] = DP[i2] + DP[i3]
But, we need to observe the base cases:
 When N=0, we cannot form any subset with 0 numbers.
 When N=1, we can form 1 subset, {1}
 When N=2, we can form 2 subsets, {1}, {2}
 When N=3, we can form 2 subsets, {1, 3}, {2}
Below is the implementation of above approach:
C++
// C++ Code to count subsets not containing // adjacent elements from 1 to N #include <bits/stdc++.h> using namespace std; // Function to count subsets int countSubsets( int N) { if (N <= 2) return N; if (N == 3) return 2; int DP[N + 1] = {0}; 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]; } // Driver Code int main() { int N = 20; cout << countSubsets(N); return 0; } 
Java
// Java code to count subsets not containing // adjacent elements from 1 to N class GFG{ // Function to count subsets 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]; } // Driver code public static void main(String[] args) { int N = 20 ; System.out.print(countSubsets(N)); } } // This code is contributed by sapnasingh4991 
Python3
# Python3 Code to count subsets # not containing adjacent elements # from 1 to N # Function to count subsets def countSubsets(N): if (N < = 2 ): return N if (N = = 3 ): return 2 DP = [ 0 ] * (N + 1 ) DP[ 0 ] = 0 DP[ 1 ] = 1 DP[ 2 ] = 2 DP[ 3 ] = 2 for i in range ( 4 , N + 1 ): DP[i] = DP[i  2 ] + DP[i  3 ] return DP[N] # Driver Code if __name__ = = '__main__' : N = 20 print (countSubsets(N)) # This code is contributed by Mohit Kumar 
C#
// C# code to count subsets not containing // adjacent elements from 1 to N using System; class GFG{ // Function to count subsets 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]; } // Driver code public static void Main(String[] args) { int N = 20; Console.Write(countSubsets(N)); } } // This code is contributed by sapnasingh4991 
265
Recommended Posts:
 Partition an array of nonnegative integers into two subsets such that average of both the subsets is equal
 Count of subsets not containing adjacent elements
 Pair of integers having least GCD among all given pairs having GCD exceeding K
 Count nonadjacent subsets from numbers arranged in Circular fashion
 Count of all subsequences having adjacent elements with different parity
 Count of all possible pairs of disjoint subsets of integers from 1 to N
 Sum of subsets of all the subsets of an array  O(3^N)
 Sum of subsets of all the subsets of an array  O(2^N)
 Sum of subsets of all the subsets of an array  O(N)
 Divide array in two Subsets such that sum of square of sum of both subsets is maximum
 Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
 Count number of subsets having a particular XOR value
 Count subsets having distinct even numbers
 Count no. of ordered subsets having a particular XOR value
 Count of subsets having sum of min and max element less than K
 Maximize count of subsets having product of smallest element and size of the subset at least X
 Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
 Maximum subsequence sum with adjacent elements having atleast K difference in index
 Count of numbers upto N having absolute difference of at most K between any two adjacent digits
 Count of distinct permutations of length N having no similar adjacent characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.