-
Notifications
You must be signed in to change notification settings - Fork 0
/
Friends_Pairing_Problem.java
48 lines (41 loc) · 1.07 KB
/
Friends_Pairing_Problem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class Friends_Pairing_Problem
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
Solution ob = new Solution();
System.out.println(ob.countFriendsPairings(n));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
static Map<Integer, Long> memo = new HashMap<>();
long M = 1000000007;
public long countFriendsPairings(int n)
{
// base case
if(n==1||n==2){
return Long.valueOf(n);
}
if(memo.containsKey(n)){
return memo.get(n);
}
//make choices
long single = countFriendsPairings(n-1);
long pair = (n-1)*countFriendsPairings(n-2);
long tot = (single + pair) % M;
memo.put(n, tot);
return tot;
}
}