7

Ways to Arrange Balls

Difficulty: HARD
Contributed By
Sakshi Bansal
Avg. time to solve
40 min
Success Rate
50%

Problem Statement

Given ‘a’ balls of type ‘A’, ‘b’ balls of type ‘B’ and ‘c’ balls of type ‘C’. You need to find the total number of ways to arrange the balls in a straight line such that adjacent balls are of different types.

In other words, find the total ways to arrange the given balls in such a way that no two balls of the same type are adjacent.

For Example :
Suppose we have 2 balls of type ‘A’, 1 ball of type ‘B’ and 1 ball of type ‘C’, following are the ways to arrange them in the required fashion. 
ABCA
ABAC 
ACBA 
ACAB 
BACA 
CABA 
Hence, there is a total of six ways.
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test contains three space-separated positive integers a, b, c, denoting the number of balls of type ‘A’, ‘B’, and ‘C’ respectively.
Output Format :
For each test case, print the total ways to arrange the balls such that adjacent balls are of a different type in a single line.

Print the output of each test case in a separate line.
Note :
You don’t have to print anything, it has already been taken care of. Just complete the function. 

If there is no way, return 0.
Constraints :
1 <= T <= 100
0 <= a, b, c <= 15

Time limit: 1 sec
Sample Input 1 :
2
1 2 1
1 1 1
Sample Output 1 :
6
6
Explanation of Sample Input 1 :
Test Case 1: We have one ball of type A, two balls of type B and one ball of type C. All the possible arrangements such that no two adjacent balls are of the same type are shown below. 
ABCB
BACB
BABC
BCAB
BCBA 
CBAB

These are all the six possible arrangements.

Test Case 2: We have 1 ball of each type. All the possible arrangements are - 
ABC
ACB
BAC
BCA
CAB
CBA

These are all six possible arrangements.
Sample Input 2 :
2
4 5 3
8 3 1
Sample Output 2 :
588
0
Reset Code
Full screen
copy-code
Console