Solved Problem - PnC
In this lesson, we'll discuss a solved PnC Problem.
We'll cover the following
Problem statement
There are different types of balls. There are balls of type 1, balls of type , and so on to . How many ways can you choose balls that are of different types?
Input format
The first line contains one positive integer N – the number of types of balls.
The second line contains a sequence where - the count of balls of each type.
Output format
Print a single integer to answer the problem.
Sample
Input 1
3
2 1 1
Output 1
5
Input 2
4
1 2 2 2
Output 2
18
Solution
We will subtract the non-desirable number of ways from the totals,
Total number of ways - Pick 2 from () balls
Non-desirable case - when both balls are of the same type. The number of ways to pick that is:
The answer is just
Note: Use long long int
since the result can overflow int
.
Get hands-on with 1400+ tech skills courses.