Statistics

Problem Statement for "SumRectangle"

Problem Statement

A sum rectangle is a rectangle divided into a grid of unit squares. Each square contains a number, and the numbers in neighboring squares always satisfy the following property:

The number in any square S that is neither in the bottom row nor in the right column can be computed as the sum of the following three numbers:

  • The number in the square directly below S.
  • The number in the square directly to the right of S.
  • The number in the other square adjacent to the previous two squares (the one diagonally down and to the right of S).

An example of a correctly filled sum rectangle:

+----+----+----+----+----+
| 88 | 57 | 33 | 10 |  5 |
+----+----+----+----+----+
| 18 | 13 | 11 | 12 | -7 |
+----+----+----+----+----+
|  1 |  4 | -2 |  1 | 18 |
+----+----+----+----+----+

For example, in the top left corner we have 88 = 18 + 57 + 13.

We have a secret sum rectangle. You will be given a int[] leftColumn containing the leftmost number in each row of our rectangle, from the top to the bottom. You will also be given an int[] topRow containing the topmost number in each column of our rectangle, from the left to the right. Compute and return the number in the bottom right corner. If the input is such that this number cannot be determined uniquely, return 0 instead.

Definition

Class:
SumRectangle
Method:
getCorner
Parameters:
int[], int[]
Returns:
int
Method signature:
int getCorner(int[] leftColumn, int[] topRow)
(be sure your method is public)

Notes

  • You may assume that the return value will always fit into an int (i.e., a 32-bit signed integer data type).

Constraints

  • leftColumn will contain between 1 and 10 elements, inclusive.
  • Each element of leftColumn will be between 0 and 100, inclusive.
  • topRow will contain between 1 and 10 elements, inclusive.
  • Each element of topRow will be between 0 and 100, inclusive.
  • Element 0 of leftColumn will be equal to element 0 of topRow.

Examples

  1. {88,18,1}

    {88,57,33,10,5}

    Returns: 18

    This is the rectangle from the problem statement. The lower right corner is uniquely determined by the left column and the top row.

  2. {0,0,0,0}

    {0,0,0,0}

    Returns: 0

    The only correct way to fill this rectangle is to place a zero into each square.

  3. {6,1}

    {6,2}

    Returns: 3

    This is the smallest non-trivial case: +----+----+ | 6 | 2 | +----+----+ | 1 | ?? | +----+----+

  4. {47}

    {47}

    Returns: 47

  5. {47,42}

    {47}

    Returns: 42

  6. {47}

    {47,42}

    Returns: 42

  7. {100,0,100,0,100,0,100,0,100,0}

    {100,0,100,0,100,0,100,0,100,0}

    Returns: 59841700

  8. {0,100,0,100,0,100,0,100,0,100}

    {0,100,0,100,0,100,0,100,0,100}

    Returns: -86414600

  9. {12,21,21,14,51,25,26,76,0,24}

    {12,62,52,35,75,2,53,35,73,21}

    Returns: -17584335

  10. {12}

    {12,62,52,35,75,2,53,35,73,21}

    Returns: 21

  11. {12,62,52,35,75,2,53,35,73,21}

    {12}

    Returns: 21

  12. {0,0,0,0,0,0}

    {0,0,47,0,0}

    Returns: -2350

  13. {3,1,4,1,5,9,2,6,5}

    {3,5,8,9,7,9,3,2}

    Returns: 706

  14. {0,100,0,100,0,100,0,100,0,100}

    {0,100,0,100,0,100,0,100,0,100}

    Returns: -86414600

  15. {27,9,3,1}

    {27,9,3,1}

    Returns: 61

  16. {27,9,3,1}

    {27,9,3,1,3}

    Returns: -119

  17. {100,0,100,0,97,4,100,1,100,0}

    {100,10,90,1,98,4,100,1,100,3}

    Returns: 54439753

  18. {0}

    {0}

    Returns: 0

  19. {100}

    {100}

    Returns: 100

  20. {1,2}

    {1}

    Returns: 2

  21. {1}

    {1,100}

    Returns: 100

  22. {0,100}

    {0,100}

    Returns: -200

  23. {100,2}

    {100,2,3}

    Returns: -97

  24. {6, 1 }

    {6, 2 }

    Returns: 3

  25. {1 }

    {1 }

    Returns: 1

  26. {77 }

    {77 }

    Returns: 77

  27. {2 }

    {2 }

    Returns: 2

  28. {0, 1 }

    {0, 1 }

    Returns: -2

  29. {5 }

    {5 }

    Returns: 5

  30. {99 }

    {99 }

    Returns: 99

  31. {50 }

    {50 }

    Returns: 50

  32. {88, 18, 1 }

    {88, 57, 33, 10, 5 }

    Returns: 18

  33. {1, 1 }

    {1, 1 }

    Returns: -1

  34. {77, 88 }

    {77 }

    Returns: 88

  35. {10 }

    {10 }

    Returns: 10

  36. {1 }

    {1, 2 }

    Returns: 2

  37. {7 }

    {7 }

    Returns: 7

  38. {6, 5, 2, 3 }

    {6 }

    Returns: 3

  39. {88 }

    {88, 2 }

    Returns: 2

  40. {1 }

    {1, 1, 1, 1 }

    Returns: 1

  41. {3 }

    {3 }

    Returns: 3

  42. {66 }

    {66 }

    Returns: 66

  43. {1, 2, 3 }

    {1 }

    Returns: 3

  44. {3, 2 }

    {3, 2 }

    Returns: -1

  45. {9, 0, 3, 5, 4, 3 }

    {9, 3, 9, 7, 3 }

    Returns: -1167

  46. {1 }

    {1, 6 }

    Returns: 6

  47. {3, 1 }

    {3 }

    Returns: 1

  48. {9 }

    {9 }

    Returns: 9

  49. {1, 2, 3, 4, 5, 6, 7, 8 }

    {1 }

    Returns: 8

  50. {88 }

    {88, 57 }

    Returns: 57


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: