An ordered set of integers is said to be a special set if for every element of the set X, the set does not contain the element X + 1. Given an integer N, the task is to find the number of special sets whose largest element is not greater than N. Since, the number of special sets can be very large, print the answer modulo 109 + 7.
Example:Â
Â
Input: N = 3Â
Output: 5Â
{1}, {2}, {3}, {1, 3} and {3, 1} are theÂ
only special sets possible.
Input: N = 4Â
Output: 10Â
Â
Â
Approach: This problem can be solved using dynamic programming. Create an array dp[][] where dp[i][j] stores the number of special sets of length i ending with j. Now, the recurrence relation will be:Â
Â
dp[i][j] = dp[i – 1][1] + dp[i – 1][2] + … + dp[i – 1][j – 2]Â
dp[i][j] can be computed in O(1) by taking the prefix sum of the previous dp[i – 1] once.Â
Â
Now the total special sets of size i can be calculated by multiplying dp[i][n] with factorial(i).
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
typedef long long ll;Â
const int MAX = 2 * 1000 + 10;const int MOD = 1e9 + 7;Â
// To store the states of the dpll dp[MAX][MAX];Â
// Function to return (a + b) % MODll sum(ll a, ll b){Â Â Â Â return ((a % MOD) + (b % MOD)) % MOD;}Â
// Function to return (a * b) % MODll mul(ll a, ll b){Â Â Â Â return ((a % MOD) * (b % MOD)) % MOD;}Â
// Function to return the count// of special setsint cntSpecialSet(int n){Â
    // Fill the dp[][] array with the answer    // for the special sets of size 1    for (int i = 1; i <= n; i++) {        dp[1][i] = 1;Â
        // Take prefix sum of the current row which        // will be used to fill the next row        dp[1][i] += dp[1][i - 1];    }Â
    // Fill the rest of the dp[][] array    for (int i = 2; i <= n; i++) {Â
        // Recurrence relation        for (int j = 2; j <= n; j++) {            dp[i][j] = dp[i - 1][j - 2];        }Â
        // Calculate the prefix sum        for (int j = 1; j <= n; j++) {            dp[i][j] = sum(dp[i][j], dp[i][j - 1]);        }    }Â
    ll ways(1), ans(0);Â
    for (int i = 1; i <= n; i++) {Â
        // To find special set of size i        ways = mul(ways, i);Â
        // Addition of special sets of all sizes        ans = sum(ans, mul(ways, dp[i][n]));    }Â
    return ans;}Â
// Driver codeint main(){Â Â Â Â int n = 3;Â
    cout << cntSpecialSet(n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG {static int MAX = 2 * 1000 + 10;static int MOD = (int) (1e9 + 7);Â
// To store the states of the dpstatic long [][]dp = new long[MAX][MAX];Â
// Function to return (a + b) % MODstatic long sum(long a, long b){Â Â Â Â return ((a % MOD) + (b % MOD)) % MOD;}Â
// Function to return (a * b) % MODstatic long mul(long a, long b){Â Â Â Â return ((a % MOD) * (b % MOD)) % MOD;}Â
// Function to return the count// of special setsstatic long cntSpecialSet(int n){Â
    // Fill the dp[][] array with the answer    // for the special sets of size 1    for (int i = 1; i <= n; i++)     {        dp[1][i] = 1;Â
        // Take prefix sum of the current row which        // will be used to fill the next row        dp[1][i] += dp[1][i - 1];    }Â
    // Fill the rest of the dp[][] array    for (int i = 2; i <= n; i++)     {Â
        // Recurrence relation        for (int j = 2; j <= n; j++)         {            dp[i][j] = dp[i - 1][j - 2];        }Â
        // Calculate the prefix sum        for (int j = 1; j <= n; j++)         {            dp[i][j] = sum(dp[i][j], dp[i][j - 1]);        }    }Â
    long ways = 1, ans = 0;      for (int i = 1; i <= n; i++)     {Â
        // To find special set of size i        ways = mul(ways, i);Â
        // Addition of special sets of all sizes        ans = sum(ans, mul(ways, dp[i][n]));    }Â
    return ans;}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int n = 3;Â
    System.out.println(cntSpecialSet(n));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approachÂ
# Function to print the nodes having # maximum and minimum degree def minMax(edges, leng, n) : Â
    # Map to store the degrees of every node     m = {};         for i in range(leng) :        m[edges[i][0]] = 0;        m[edges[i][1]] = 0;             for i in range(leng) :                 # Storing the degree for each node        m[edges[i][0]] += 1;        m[edges[i][1]] += 1; Â
    # maxi and mini variables to store     # the maximum and minimum degree     maxi = 0;     mini = n; Â
    for i in range(1, n + 1) :        maxi = max(maxi, m[i]);         mini = min(mini, m[i]); Â
    # Printing all the nodes     # with maximum degree     print("Nodes with maximum degree : ",                                 end = "")         for i in range(1, n + 1) :        if (m[i] == maxi) :            print(i, end = " "); Â
    print()Â
    # Printing all the nodes     # with minimum degree     print("Nodes with minimum degree : ",                                 end = "")         for i in range(1, n + 1) :        if (m[i] == mini) :            print(i, end = " "); Â
# Driver code if __name__ == "__main__" : Â
    # Count of nodes and edges     n = 4; m = 6; Â
    # The edge list     edges = [[ 1, 2 ], [ 1, 3 ],              [ 1, 4 ], [ 2, 3 ],              [ 2, 4 ], [ 3, 4 ]]; Â
    minMax(edges, m, 4); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â Â Â Â Â class GFG {Â Â Â Â Â static int MAX = 2 * 1000 + 10;static int MOD = (int) (1e9 + 7);Â
// To store the states of the dpstatic long [,]dp = new long[MAX, MAX];Â
// Function to return (a + b) % MODstatic long sum(long a, long b){Â Â Â Â return ((a % MOD) + (b % MOD)) % MOD;}Â
// Function to return (a * b) % MODstatic long mul(long a, long b){Â Â Â Â return ((a % MOD) * (b % MOD)) % MOD;}Â
// Function to return the count// of special setsstatic long cntSpecialSet(int n){Â
    // Fill the dp[,] array with the answer    // for the special sets of size 1    for (int i = 1; i <= n; i++)     {        dp[1, i] = 1;Â
        // Take prefix sum of the current row which        // will be used to fill the next row        dp[1, i] += dp[1, i - 1];    }Â
    // Fill the rest of the dp[,] array    for (int i = 2; i <= n; i++)     {Â
        // Recurrence relation        for (int j = 2; j <= n; j++)         {            dp[i, j] = dp[i - 1, j - 2];        }Â
        // Calculate the prefix sum        for (int j = 1; j <= n; j++)         {            dp[i, j] = sum(dp[i, j], dp[i, j - 1]);        }     }Â
    long ways = 1, ans = 0;Â
    for (int i = 1; i <= n; i++)     {Â
        // To find special set of size i        ways = mul(ways, i);Â
        // Addition of special sets of all sizes        ans = sum(ans, mul(ways, dp[i, n]));    }Â
    return ans;}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int n = 3;Â
    Console.WriteLine(cntSpecialSet(n));}}Â
// This code is contributed by Princi Singh |
Javascript
<script>Â
b// JavaScript implementation of the approachÂ
Â
const MAX = 2 * 1000 + 10;const MOD = 1e9 + 7;Â
// To store the states of the dplet dp = new Array(MAX).fill(0).map(()=>new Array(MAX).fill(0));Â
// Function to return (a + b) % MODfunction sum(a, b){Â Â Â Â return ((a % MOD) + (b % MOD)) % MOD;}Â
// Function to return (a * b) % MODfunction mul(a, b){Â Â Â Â return ((a % MOD) * (b % MOD)) % MOD;}Â
// Function to return the count// of special setsfunction cntSpecialSet(n){Â
    // Fill the dp[][] array with the answer    // for the special sets of size 1    for (let i = 1; i <= n; i++) {        dp[1][i] = 1;Â
        // Take prefix sum of the current row which        // will be used to fill the next row        dp[1][i] += dp[1][i - 1];    }Â
    // Fill the rest of the dp[][] array    for (let i = 2; i <= n; i++) {Â
        // Recurrence relation        for (let j = 2; j <= n; j++) {            dp[i][j] = dp[i - 1][j - 2];        }Â
        // Calculate the prefix sum        for (let j = 1; j <= n; j++) {            dp[i][j] = sum(dp[i][j], dp[i][j - 1]);        }    }Â
    let ways = 1 , ans = 0;Â
    for (let i = 1; i <= n; i++) {Â
        // To find special set of size i        ways = mul(ways, i);Â
        // Addition of special sets of all sizes        ans = sum(ans, mul(ways, dp[i][n]));    }Â
    return ans;}Â
// Driver codeÂ
let n = 3;Â
document.write(cntSpecialSet(n),"</br>");Â
/// This code is contributed by shinjanpatraÂ
</script> |
5
Â
Time Complexity: O(n2)
Auxiliary Space: O(MAX2)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
