Count of subsets of integers from 1 to N having no adjacent elements
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 non-adjacent 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.
So next Subproblem becomes DP[i-3]
So the DP relation becomes :
DP[i] = DP[i-2] + DP[i-3]
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++
#include <bits/stdc++.h>
using namespace std;
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];
}
int main()
{
int N = 20;
cout << countSubsets(N);
return 0;
}
|
Java
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));
}
}
|
Python3
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]
if __name__ = = '__main__' :
N = 20
print (countSubsets(N))
|
C#
using System;
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;
Console.Write(countSubsets(N));
}
}
|
Javascript
<script>
function countSubsets(N) {
if (N <= 2)
return N;
if (N == 3)
return 2;
var DP = Array(N + 1).fill(0);
DP[0] = 0;
DP[1] = 1;
DP[2] = 2;
DP[3] = 2;
for (i = 4; i <= N; i++) {
DP[i] = DP[i - 2] + DP[i - 3];
}
return DP[N];
}
var N = 20;
document.write(countSubsets(N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(N)
Efficient approach: Space-optimized approach
In this approach, we optimize the space by using variables.
Implementation steps :
- Initialize variables to store previous computations : prev1, prev2 , prev3, prev4
- The current computation is depend upon the prev3 and prev2.
- After every iteration update these variables to iterate further.
- At last return answer stored in curr.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubsets( int N)
{
if (N <= 2)
return N;
if (N == 3)
return 2;
int prev1 = 0, prev2 = 1, prev3 = 2, prev4 = 2 , curr;
for ( int i = 4; i <= N; i++) {
curr = prev3 + prev2;
prev1 = prev2;
prev2= prev3;
prev3 = prev4;
prev4 = curr;
}
return curr;
}
int main()
{
int N = 20;
cout << countSubsets(N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int countSubsets( int N)
{
if (N <= 2 ) {
return N;
}
if (N == 3 ) {
return 2 ;
}
int prev1 = 0 , prev2 = 1 , prev3 = 2 ;
int prev4 = 2 , curr = 0 ;
for ( int i = 4 ; i <= N; i++) {
curr = prev3 + prev2;
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = curr;
}
return curr;
}
public static void main(String[] args)
{
int N = 20 ;
System.out.println(countSubsets(N));
}
}
|
Python3
def countSubsets(N):
if (N < = 2 ):
return N
if (N = = 3 ):
return 2
prev1 = 0
prev2 = 1
prev3 = 2
prev4 = 2
curr = 0
for i in range ( 4 , N + 1 ):
curr = prev3 + prev2
prev1 = prev2
prev2 = prev3
prev3 = prev4
prev4 = curr
return curr
N = 20
print (countSubsets(N))
|
C#
using System;
class MainClass {
public static int CountSubsets( int N)
{
if (N <= 2) {
return N;
}
if (N == 3) {
return 2;
}
int prev1 = 0, prev2 = 1, prev3 = 2, prev4 = 2,
curr = 0;
for ( int i = 4; i <= N; i++) {
curr = prev3 + prev2;
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = curr;
}
return curr;
}
public static void Main( string [] args)
{
int N = 20;
Console.WriteLine(CountSubsets(N));
}
}
|
Javascript
function countSubsets(N) {
if (N <= 2) {
return N;
}
if (N == 3) {
return 2;
}
let prev1 = 0, prev2 = 1, prev3 = 2, prev4 = 2 , curr;
for (let i = 4; i <= N; i++) {
curr = prev3 + prev2;
prev1 = prev2;
prev2= prev3;
prev3 = prev4;
prev4 = curr;
}
return curr;
}
const N = 20;
console.log(countSubsets(N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
11 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...