Count of subsets not containing adjacent elements

Given an array arr[] of N integers, the task is to find the count of all the subsets which do not contain adjacent elements from the given array.

Examples:

Input: arr[] = {2, 7}
Output: 3
All possible subsets are {}, {2} and {7}.



Input: arr[] = {3, 5, 7}
Output: 5

Method 1: The idea is to use a bit-mask pattern to generate all the combinations as discussed in this article. While considering a subset, we need to check if it contains adjacent elements or not. A subset will contain adjacent elements if two or more consecutive bits are set in its bit-mask. In order to check if the bit-mask has consecutive bits set or not, we can right shift the mask by one bit and take its AND with the mask. If the result of the AND operation is 0, then the mask does not have consecutive bits set and therefore, the corresponding subset will not have adjacent elements from the array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
#include <math.h>
using namespace std;
  
// Function to return the count
// of possible subsets
int cntSubsets(int* arr, int n)
{
  
    // Total possible subsets of n
    // sized array is (2^n - 1)
    unsigned int max = pow(2, n);
  
    // To store the required
    // count of subsets
    int result = 0;
  
    // Run from i 000..0 to 111..1
    for (int i = 0; i < max; i++) {
        int counter = i;
  
        // If current subset has consecutive
        // elements from the array
        if (counter & (counter >> 1))
            continue;
        result++;
    }
    return result;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << cntSubsets(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
      
class GFG
{
  
// Function to return the count
// of possible subsets
static int cntSubsets(int[] arr, int n)
{
  
    // Total possible subsets of n
    // sized array is (2^n - 1)
    int max = (int) Math.pow(2, n);
  
    // To store the required
    // count of subsets
    int result = 0;
  
    // Run from i 000..0 to 111..1
    for (int i = 0; i < max; i++) 
    {
        int counter = i;
  
        // If current subset has consecutive
        if ((counter & (counter >> 1)) > 0)
            continue;
        result++;
    }
    return result;
}
  
// Driver code
static public void main (String []arg)
{
    int arr[] = { 3, 5, 7 };
    int n = arr.length;
  
    System.out.println(cntSubsets(arr, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count
# of possible subsets
def cntSubsets(arr, n):
  
    # Total possible subsets of n
    # sized array is (2^n - 1)
    max = pow(2, n)
  
    # To store the required
    # count of subsets
    result = 0
  
    # Run from i 000..0 to 111..1
    for i in range(max):
        counter = i
  
        # If current subset has consecutive
        # elements from the array
        if (counter & (counter >> 1)):
            continue
        result += 1
  
    return result
  
# Driver code
arr = [3, 5, 7]
n = len(arr)
  
print(cntSubsets(arr, n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the count
// of possible subsets
static int cntSubsets(int[] arr, int n)
{
  
    // Total possible subsets of n
    // sized array is (2^n - 1)
    int max = (int) Math.Pow(2, n);
  
    // To store the required
    // count of subsets
    int result = 0;
  
    // Run from i 000..0 to 111..1
    for (int i = 0; i < max; i++) 
    {
        int counter = i;
  
        // If current subset has consecutive
        if ((counter & (counter >> 1)) > 0)
            continue;
        result++;
    }
    return result;
}
  
// Driver code
static public void Main (String []arg)
{
    int []arr = { 3, 5, 7 };
    int n = arr.Length;
  
    Console.WriteLine(cntSubsets(arr, n));
}
}
      
// This code is contributed by Rajput-Ji

chevron_right


Output:

5

Method 2: The above approach takes exponential time. In the above code, the number of bit-masks without consecutive 1s were required. This count can be obtained in linear time using dynamic programming as discussed in this article.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return the count
// of possible subsets
int cntSubsets(int* arr, int n)
{
    int a[n], b[n];
  
    a[0] = b[0] = 1;
  
    for (int i = 1; i < n; i++) {
  
        // If previous element was 0 then 0
        // as well as 1 can be appended
        a[i] = a[i - 1] + b[i - 1];
  
        // If previous element was 1 then
        // only 0 can be appended
        b[i] = a[i - 1];
    }
  
    // Store the count of all possible subsets
    int result = a[n - 1] + b[n - 1];
  
    return result;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << cntSubsets(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to return the count
// of possible subsets
static int cntSubsets(int []arr, int n)
{
    int []a = new int[n];
    int []b = new int[n];
  
    a[0] = b[0] = 1;
  
    for (int i = 1; i < n; i++) 
    {
  
        // If previous element was 0 then 0
        // as well as 1 can be appended
        a[i] = a[i - 1] + b[i - 1];
  
        // If previous element was 1 then
        // only 0 can be appended
        b[i] = a[i - 1];
    }
  
    // Store the count of all possible subsets
    int result = a[n - 1] + b[n - 1];
  
    return result;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 5, 7 };
    int n = arr.length;
  
    System.out.println(cntSubsets(arr, n));
}
}
  
// This code is contributed by Princi Singh

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the count 
# of possible subsets 
def cntSubsets(arr, n) : 
  
    a = [0] * n
    b = [0] * n; 
  
    a[0] = b[0] = 1
  
    for i in range(1, n) :
          
        # If previous element was 0 then 0 
        # as well as 1 can be appended 
        a[i] = a[i - 1] + b[i - 1]; 
  
        # If previous element was 1 then 
        # only 0 can be appended 
        b[i] = a[i - 1]; 
  
    # Store the count of all possible subsets 
    result = a[n - 1] + b[n - 1]; 
  
    return result; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 3, 5, 7 ]; 
    n = len(arr); 
  
    print(cntSubsets(arr, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


Output:

5

Method 3; If we take a closer look at the pattern, we can observe that the count is actually (N + 2)th Fibonacci number for N ≥ 1.

n = 1, count = 2 = fib(3)
n = 2, count = 3 = fib(4)
n = 3, count = 5 = fib(5)
n = 4, count = 8 = fib(6)
n = 5, count = 13 = fib(7)
…………….

Therefore, the subsets can be counted in O(log n) time using the method 5 of this article.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.