Archive / 2019 / Selection Test
Candy Combinations
There is a bag filled with candies. Each candy has a different color. John can pick a given number of candies from the bag. He wants to know how many combinations he can get.
Example:
The bag has 5 candies red, green, blue, yellow and orange. John can pick 3 candies. He can get 10 combinations
R G B
R G Y
R G O
R B Y
R B O
R Y O
G B Y
G B O
G Y O
B Y O
You are given the number of pills in the bag (N) and the number of candies John can pick (R). You have to output the number of combinations he can get.
You can use the following formula to calculate the number of combinations:
1 x 2 x 3 x ... (N - 1) x N / {(1 x 2 x 3 x ... (R - 1) x R) x (1 x 2 x 3 x ... (N - R - 1) x (N - R))}
For example when N = 5 and R = 3
1 x 2 x 3 x 4 x 5/((1 x 2 x 3) x (1 x 2)) = 120 / (6 x 2) = 10
When N = 5 and R = 5
1 x 2 x 3 x 4 x 5/((1 x 2 x 3 x 4 x 5)) = 120 / 120 = 1
When N = 5 and R = 1
1 x 2 x 3 x 4 x 5/((1 x 2 x 3 x 4) x 1) = 120 / 24 = 5
Input Format
The input is two space-separated numbers N and R
Output Format
You should output the number of combinations
Constraints
- 1 \(\leq\) N \(\leq\) 10 (N is greater than or equal to 1 and less than or equal to 10)
- 1 \(\leq\) R \(\leq\) N (R is greater than or equal to 1 and less than or equal to N)
Sample Input 0
5 3
Sample Output 0
10
Sample Input 1
5 5
Sample Output 1
1
Sample Input 2
5 1
Sample Output 2
5
Open on HackerRank ↗ · Markdown source · Back to the archive