Problem Statement
Your friend once had N matches, all of the same length.
He also had three empty boxes labeled A, B, and C.
He then took each match, broke it into three pieces of arbitrary length, and placed one piece into each of the boxes.
At the end, he shuffled the contents of each box.
Your friend thinks that it is now pretty difficult to tell which parts belong to the same match. He has shown the three boxes to you, and he asked you the following question: "What length did the original matches have?"
Are you up to this challenge? We will give you one hint: it is actually much easier to answer your friend's question that it may originally seem to be.
You are given three int[]s: lenA, lenB, and lenC. Each of them contains N elements. The elements of lenA are the lengths of match pieces in box A. Boxes B and C are described by lenB and lenC in the same way. Return the length of the original matches.
Your friend thinks that it is now pretty difficult to tell which parts belong to the same match. He has shown the three boxes to you, and he asked you the following question: "What length did the original matches have?"
Are you up to this challenge? We will give you one hint: it is actually much easier to answer your friend's question that it may originally seem to be.
You are given three int[]s: lenA, lenB, and lenC. Each of them contains N elements. The elements of lenA are the lengths of match pieces in box A. Boxes B and C are described by lenB and lenC in the same way. Return the length of the original matches.
Definition
- Class:
- BrokenMatches
- Method:
- restoreLength
- Parameters:
- int[], int[], int[]
- Returns:
- int
- Method signature:
- int restoreLength(int[] lenA, int[] lenB, int[] lenC)
- (be sure your method is public)
Constraints
- lenA, lenB and lenC will each contain between 1 and 10 elements, inclusive.
- lenA, lenB and lenC will contain the same number of elements.
- Each element of lenA, lenB and lenC will be between 1 and 100, inclusive.
- The contents of boxes A, B and C represented by lenA, lenB and lenC could be obtained using the process described in the problem statement.
Examples
{4}{2}{3}Returns: 9
When there is just 1 match, the problem is easy. The answer is 4 + 2 + 3 = 9.{1,5}{2,8}{3,1}Returns: 10
The original 2 matches had length 10. One of them was broken into pieces of lengths 1, 8 and 1. The other match was broken into pieces of lengths 5, 2 and 3.{5,3,3,2}{2,2,6,4}{3,4,1,5}Returns: 10
{13,25,18,2,32}{22,15,16,17,1}{8,26,17,21,17}Returns: 50
{21,36,4,18,28,29,14,30,32,7}{20,36,9,4,18,37,17,7,11,50}{13,19,3,15,24,9,26,35,22,6}Returns: 60