Given an integer N. Consider the set of first N natural numbers **A = {1, 2, 3, …, N}**. Let M and P be two **non-empty** subsets of A. The task is to count the number of unordered pairs of (M, P) such that M and P are disjoint sets. Note that the order of M and P doesn’t matter.

**Examples:**

Input:N = 3

Output:6

The unordered pairs are ({1}, {2}), ({1}, {3}),

({2}, {3}), ({1}, {2, 3}), ({2}, {1, 3}), ({3}, {1, 2}).

Input:N = 2

Output:1

Input:N = 10

Output:28501

**Approach:**

- Lets assume there are only 6 elements in the set {1, 2, 3, 4, 5, 6}.
- When you count the number of subsets with 1 as one of the element of first subset, it comes out to be 211.
- Counting number of subsets with 2 being one of the element of first subset, it comes out to be 65, because 1’s not included as order of sets doesn’t matter.
- Counting number of subset with 3 being one of the element of first set it comes out to be 65, here a pattern can be observed.
- Pattern:

5 = 3 * 1 + 2

19 = 3 * 5 + 4

65 = 3 * 19 + 8

211 = 3 * 65 + 16

S(n) = 3 * S(n-1) + 2^{(n – 2)} - Expanding it until n->2 (means numbers of elements n-2+1=n-1)

2^{(n-2)}* 3^{(0)}+ 2^{(n – 3)}* 3^{1}+ 2^{(n – 4)}* 3^{2}+ 2^{(n – 5)}* 3^{3}+ … + 2^{(0)}* 3^{(n – 2)}

From Geometric progression, a + a * r^{0}+ a * r^{1}+ … + a * r^{(n – 1)}= a * (r^{n}– 1) / (r – 1) **S(n) = 3**. Remember S(n) is number of subsets with 1 as a one of the elements of first subset but to get the required result, Denoted by^{(n – 1)}– 2^{(n – 1)}**T(n) = S(1) + S(2) + S(3) + … +S(n)**- It also forms a Geometric progression, so we calculate it by formula of sum of GP

**T(n) = (3**^{n}– 2^{n + 1}+ 1)/2 - As we require T(n) % p where p = 10
^{9}+ 7

We have to use Fermats’s little theorem

a^{-1}= a^{(m – 2)}(mod m) for modular division- 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)
- Count of subsets of integers from 1 to N having no adjacent elements
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of bitwise OR of all possible subsets of given set
- Sum of the sums of all possible subsets
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array
- Sum of products of all possible K size subsets of the given array
- Check if it is possible to split given Array into K odd-sum subsets
- Count number of subsets having a particular XOR value
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count subsets having distinct even numbers
- Count no. of ordered subsets having a particular XOR value
- Count of subsets not containing adjacent elements
- Count non-adjacent subsets from numbers arranged in Circular fashion
- Count of subsets with sum equal to X
- Count of Subsets of a given Set with element X present in it
- Count of subsets with sum equal to X using Recursion

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`#define p 1000000007`

`// Modulo exponentiation function`

`long`

`long`

`power(`

`long`

`long`

`x,`

`long`

`long`

`y)`

`{`

`// Function to calculate (x^y)%p in O(log(y))`

`long`

`long`

`res = 1;`

`x = x % p;`

`while`

`(y > 0) {`

`if`

`(y & 1)`

`res = (res * x) % p;`

`y = y >> 1;`

`x = (x * x) % p;`

`}`

`return`

`res % p;`

`}`

`// Driver function`

`int`

`main()`

`{`

`long`

`long`

`n = 3;`

`// Evaluating ((3^n-2^(n+1)+1)/2)%p`

`long`

`long`

`x = (power(3, n) % p + 1) % p;`

`x = (x - power(2, n + 1) + p) % p;`

`// From Fermats’s little theorem`

`// a^-1 ? a^(m-2) (mod m)`

`x = (x * power(2, p - 2)) % p;`

`cout << x <<`

`"\n"`

`;`

`}`

*chevron_right**filter_none*## Java

`// Java implementation of the approach`

`class`

`GFG`

`{`

`static`

`int`

`p =`

`1000000007`

`;`

`// Modulo exponentiation function`

`static`

`long`

`power(`

`long`

`x,`

`long`

`y)`

`{`

`// Function to calculate (x^y)%p in O(log(y))`

`long`

`res =`

`1`

`;`

`x = x % p;`

`while`

`(y >`

`0`

`)`

`{`

`if`

`(y %`

`2`

`==`

`1`

`)`

`res = (res * x) % p;`

`y = y >>`

`1`

`;`

`x = (x * x) % p;`

`}`

`return`

`res % p;`

`}`

`// Driver Code`

`public`

`static`

`void`

`main(String[] args)`

`{`

`long`

`n =`

`3`

`;`

`// Evaluating ((3^n-2^(n+1)+1)/2)%p`

`long`

`x = (power(`

`3`

`, n) % p +`

`1`

`) % p;`

`x = (x - power(`

`2`

`, n +`

`1`

`) + p) % p;`

`// From Fermats's little theorem`

`// a^-1 ? a^(m-2) (mod m)`

`x = (x * power(`

`2`

`, p -`

`2`

`)) % p;`

`System.out.println(x);`

`}`

`}`

`// This code is contributed by Rajput-Ji`

*chevron_right**filter_none*## Python3

`# Python3 implementation of the approach`

`p`

`=`

`1000000007`

`# Modulo exponentiation function`

`def`

`power(x, y):`

`# Function to calculate (x^y)%p in O(log(y))`

`res`

`=`

`1`

`x`

`=`

`x`

`%`

`p`

`while`

`(y >`

`0`

`):`

`if`

`(y &`

`1`

`):`

`res`

`=`

`(res`

`*`

`x)`

`%`

`p;`

`y`

`=`

`y >>`

`1`

`x`

`=`

`(x`

`*`

`x)`

`%`

`p`

`return`

`res`

`%`

`p`

`# Driver Code`

`n`

`=`

`3`

`# Evaluating ((3^n-2^(n+1)+1)/2)%p`

`x`

`=`

`(power(`

`3`

`, n)`

`%`

`p`

`+`

`1`

`)`

`%`

`p`

`x`

`=`

`(x`

`-`

`power(`

`2`

`, n`

`+`

`1`

`)`

`+`

`p)`

`%`

`p`

`# From Fermats’s little theorem`

`# a^-1 ? a^(m-2) (mod m)`

`x`

`=`

`(x`

`*`

`power(`

`2`

`, p`

`-`

`2`

`))`

`%`

`p`

`print`

`(x)`

`# This code is contributed by Mohit Kumar`

*chevron_right**filter_none*## C#

`// C# implementation of the approach`

`using`

`System;`

`class`

`GFG`

`{`

`static`

`int`

`p = 1000000007;`

`// Modulo exponentiation function`

`static`

`long`

`power(`

`long`

`x,`

`long`

`y)`

`{`

`// Function to calculate (x^y)%p in O(log(y))`

`long`

`res = 1;`

`x = x % p;`

`while`

`(y > 0)`

`{`

`if`

`(y % 2 == 1)`

`res = (res * x) % p;`

`y = y >> 1;`

`x = (x * x) % p;`

`}`

`return`

`res % p;`

`}`

`// Driver Code`

`static`

`public`

`void`

`Main ()`

`{`

`long`

`n = 3;`

`// Evaluating ((3^n-2^(n+1)+1)/2)%p`

`long`

`x = (power(3, n) % p + 1) % p;`

`x = (x - power(2, n + 1) + p) % p;`

`// From Fermats's little theorem`

`// a^-1 ? a^(m-2) (mod m)`

`x = (x * power(2, p - 2)) % p;`

`Console.Write(x);`

`}`

`}`

`// This code is contributed by ajit.`

*chevron_right**filter_none***Output:**6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

**DSA Self Paced Course**at a student-friendly price and become industry ready.## Recommended Posts:

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.