forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendsPairing.java
More file actions
26 lines (25 loc) · 755 Bytes
/
FriendsPairing.java
File metadata and controls
26 lines (25 loc) · 755 Bytes
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
/*Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once.
Find out the total number of ways in which friends can remain single or can be paired up.
*/
import java.util.*;
public class FriendsPairing {
static int countFriendsPairing(int n)
{
int a = 1, b = 2, c = 0;
if (n <= 2) {
return n;
}
for (int i = 3; i <= n; i++) {
c = b + (i - 1) * a;
a = b;
b = c;
}
return c;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(countFriendsPairing(n));
}
}