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: Using bit masking
Idea: 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 it AND with the mask. If the result of the AND operation is 0, then the mask does not have consecutive sets and therefore, the corresponding subset will not have adjacent elements from the array.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
int cntSubsets( int * arr, int n)
{
unsigned int max = pow (2, n);
int result = 0;
for ( int i = 0; i < max; i++) {
int counter = i;
if (counter & (counter >> 1))
continue ;
result++;
}
return result;
}
int main()
{
int arr[] = { 3, 5, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntSubsets(arr, n);
return 0;
}
|
Java
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));
}
}
|
Python3
def cntSubsets(arr, n):
max = pow ( 2 , n)
result = 0
for i in range ( max ):
counter = i
if (counter & (counter >> 1 )):
continue
result + = 1
return result
arr = [ 3 , 5 , 7 ]
n = len (arr)
print (cntSubsets(arr, n))
|
C#
using System;
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;
Console.WriteLine(cntSubsets(arr, n));
}
}
|
Javascript
<script>
function cntSubsets(arr, n)
{
var max = Math.pow(2, n);
var result = 0;
for ( var i = 0; i < max; i++) {
var counter = i;
if (counter & (counter >> 1))
continue ;
result++;
}
return result;
}
var arr = [3, 5, 7];
var n = arr.length;
document.write( cntSubsets(arr, n));
</script>
|
Time Complexity: O(2n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method 2: The above approach takes exponential time. In the above code, the number of bit-masks without consecutive 1s was 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++
#include <iostream>
using namespace std;
int cntSubsets( int * arr, int n)
{
int a[n], b[n];
a[0] = b[0] = 1;
for ( int i = 1; i < n; i++) {
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
int result = a[n - 1] + b[n - 1];
return result;
}
int main()
{
int arr[] = { 3, 5, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntSubsets(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
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++)
{
a[i] = a[i - 1 ] + b[i - 1 ];
b[i] = a[i - 1 ];
}
int result = a[n - 1 ] + b[n - 1 ];
return result;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 7 };
int n = arr.length;
System.out.println(cntSubsets(arr, n));
}
}
|
Python3
def cntSubsets(arr, n) :
a = [ 0 ] * n
b = [ 0 ] * n;
a[ 0 ] = b[ 0 ] = 1 ;
for i in range ( 1 , n) :
a[i] = a[i - 1 ] + b[i - 1 ];
b[i] = a[i - 1 ];
result = a[n - 1 ] + b[n - 1 ];
return result;
if __name__ = = "__main__" :
arr = [ 3 , 5 , 7 ];
n = len (arr);
print (cntSubsets(arr, n));
|
C#
using System;
class GFG
{
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++)
{
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
int result = a[n - 1] + b[n - 1];
return result;
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 7 };
int n = arr.Length;
Console.WriteLine(cntSubsets(arr, n));
}
}
|
Javascript
<script>
function cntSubsets(arr, n)
{
var a = Array(n);
var b = Array(n);
a[0] = b[0] = 1;
for ( var i = 1; i < n; i++) {
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
var result = a[n - 1] + b[n - 1];
return result;
}
var arr = [3, 5, 7 ];
var n = arr.length;
document.write( cntSubsets(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n), where n is the size of the given array
Another approach : Space optimized
we can space optimize previous approach by using only two variables to store the previous values instead of two arrays.
Implementation Steps:
- Initialize two variables a and b to 1.
- Use a loop to iterate over the array elements from index 1 to n-1.
- For each element at index i, update a and b using the following formulas:
a = a + b
b = previous value of a
- After the loop, compute the total number of possible subsets as the sum of a and b.
- Return the total number of possible subsets.
Implementation:
C++
#include <iostream>
using namespace std;
int cntSubsets( int * arr, int n)
{
int a = 1, b = 1;
for ( int i = 1; i < n; i++) {
int temp = a;
a = a + b;
b = temp;
}
int result = a + b;
return result;
}
int main()
{
int arr[] = { 3, 5, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntSubsets(arr, n);
return 0;
}
|
Java
import java.io.*;
class Main {
static int cntSubsets( int [] arr, int n) {
int a = 1 , b = 1 ;
for ( int i = 1 ; i < n; i++) {
int temp = a;
a = a + b;
b = temp;
}
int result = a + b;
return result;
}
public static void main(String[] args) {
int [] arr = { 3 , 5 , 7 };
int n = arr.length;
System.out.println(cntSubsets(arr, n));
}
}
|
Python3
def cntSubsets(arr, n):
a = 1
b = 1
for i in range ( 1 , n):
temp = a
a = a + b
b = temp
result = a + b
return result
arr = [ 3 , 5 , 7 ]
n = len (arr)
print (cntSubsets(arr, n))
|
C#
using System;
public class Program {
public static int CountSubsets( int [] arr, int n)
{
int a = 1, b = 1;
for ( int i = 1; i < n; i++) {
int temp = a;
a = a + b;
b = temp;
}
int result = a + b;
return result;
}
public static void Main()
{
int [] arr = { 3, 5, 7 };
int n = arr.Length;
Console.WriteLine(CountSubsets(arr, n));
}
}
|
Javascript
function cntSubsets(arr) {
let a = 1, b = 1;
for (let i = 1; i < arr.length; i++) {
let temp = a;
a = a + b;
b = temp;
}
let result = a + b;
return result;
}
const arr = [3, 5, 7];
console.log(cntSubsets(arr));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
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 method 5 of this article.
Last Updated :
29 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...