VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-number-of-subsequences-of-n-friends/

⇱ Find the number of subsequences of N friends - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the number of subsequences of N friends

Last Updated : 29 Jan, 2024

Given two arrays A[] and B[] of length N and M respectively. A[] represents the age of N friends and B[] contains M number of pairs in the form of (X β†’ Y), which denotes X knows Y and vice-versa. Then your task is to output the count of all possible sequences of length N friends with the given condition If (X β†’ Y) and AX < AY, then in the sequence AX must appear before AY.

Note:

  • Since the number of sequences can be large, therefore use modulo 109+7.
  • If three people let say A, B and C are there. Where A β†’ B and B β†’ C, then A β†’C also hold true.
  • The person who is not known of any other person can appear anywhere in the sequence irrespective of its age.

Examples:

Input: N = 5, M = 5, A[] = {12, 14, 17, 22, 15}, B[] = {{1 β†’ 2}, {2 β†’ 3}, {3 β†’ 4}, {4 β†’ 2}, {3 β†’ 1}}

πŸ‘ t1
First Input Test case

Output: 5
Explanation: Following the given constraints there will be 5 such sequences, which are:

  • First sequence of 1 to N friends: [5, 1, 2, 3, 4]. Then age sequence will be: [A5, A1, A2, A3, A4] = {15, 12, 14, 17, 22}
  • Second sequence: [1, 5, 2, 3, 4]. Then age sequence: [A1, A5, A2, A3, A4] = {12, 15, 14, 17, 22}
  • Third sequence: [1, 2, 5, 3, 4]. Then age sequence: [A1, A2, A5, A3, A4] = {12, 14, 15, 17, 22}
  • Fourth sequence: [1, 2, 3, 5, 4]. Then age sequence: [A1, A2, A3, A5, A4] = {12, 14, 17, 15, 22}
  • Fifth sequence: [1, 2, 3, 4, 5]. Then age sequence: [A1, A2, A3, A4, A5] = {12, 14, 17, 22, 15}

It can be seen that 1, 2, 3 and 4 knows each other and their order of age is according to given constraints while 5th is unknown for all others and its order not matters in sequence. This these 5 will be the age sequences according to given constraints. Therefore, output is 5.

Input: N = 2, M = 1, A[] = {2, 2}, B[] = {{1 β†’ 2}}
Output: 2
Explanation: There will be 2 such age sequences possible: {A1, A2} and {A2, A1} . They both have same age and knows each other also. Therefore, there will be two sequences. Note that, if they don't know each other then also the sequences will be same as this case.

Approach: Implement the idea below to solve the problem

The problem is based on the Graph Theory and Combinatorics. The problem can be solved using concept of DFS and Factorial. In this problem, DFS is used to visit each node exactly once and calculate the number of ways to place each person in the sequence considering their friends who have already been placed. Factorials are used for counting sequences. It represents the number of ways to arrange N distinct items into a sequence, which is why it's used here.

The final result, which is the total number of sequences counted under the modulo 10^9+7.

Total Arrangements: There are N! ways to arrange all the ages. This comes from the basic principle of permutations in combinatorics, Maintaining Relative Order: The relative sorted ordering of ages of each connected component should be maintained. This means that for each connected component, we divide by X!, where X! is the number of elements in the ith connected component. This is because within each connected component, the elements can be arranged in X! ways, but these arrangements do not produce distinct sequences due to the ordering constraint of ages.

Equal Strengths: Some elements that have ages equal in a connected component can be arranged in Y! ways. Here, Y! is the number of equal ages in the ith connected component. These arrangements produce distinct sequences because the strength ordering constraint does not apply to elements with equal ages.

So, the total number of possible sequences is given by the formula:

  • (N!*Y!/X!)

Steps were taken to solve the problem:

  • Precompute Factorials: Initialize an array let say Fac[] to store the factorial of numbers up to 2e5. This will be used later for combinatorics calculations.
  • Graph Construction: Read the friendship pairs from B[] and construct an adjacency list representation of the graph let say Adj.
  • DFS and Calculation: For each node, If it has not been visited, perform a DFS starting from that node. During the DFS, keep track of the count of each value in a HashMap let say Map. After the DFS, calculate the result using factorials and modular arithmetic.
  • Results: Output the number of ways calculated using factorials.

Code to implement the approach:


Output
5

Time Complexity: O(N2), Where N is the number of nodes in the graph. This is because for each node, the code performs a depth-first search (DFS) which can visit each node in the graph, and for each node visited, it performs operations that are linear in the number of nodes.

Auxiliary Space: O(N), This is because it maintains an adjacency list representation of the graph, a visited array to keep track of visited nodes, and an array to store some values associated with each node are used. The HashMap used to keep track of counts during DFS also contributes to the space complexity, but its size is at most N, so it doesn’t change the overall space complexity.

Comment