Statistics

Problem Statement for "MagicSquare"

Problem Statement

A magic square is a 3x3 array of numbers, such that the sum of each row, column, and diagonal are all the same. For example:


    8 1 6
    3 5 7
    4 9 2

In this example, all rows, columns, and diagonals sum to 15.

You will be given a int[] representing the nine numbers of a magic square, listed left-to-right, top-to-bottom. However, one of these numbers will be replaced by a -1. Write a method to determine which number was removed. For example, if the 7 was removed from the magic square above, you would be given { 8, 1, 6, 3, 5, -1, 4, 9, 2 }, and your program should return 7.

The completed magic square will consist of exactly 9 distinct positive integers.

Definition

Class:
MagicSquare
Method:
missing
Parameters:
int[]
Returns:
int
Method signature:
int missing(int[] square)
(be sure your method is public)

Constraints

  • square will contain exactly 9 elements.
  • Each element of square will either be -1 or be between 1 and 100, inclusive.
  • Exactly one element of square will be -1.
  • The eight elements of square that are not -1 will be distinct.
  • The input will be such that a number between 1 and 100, inclusive, can be found to complete the magic square, and it will not be equal to any of the other 8 numbers in the square.

Examples

  1. { 8, 1, 6, 3, 5, -1, 4, 9, 2 }

    Returns: 7

    This is the example from the problem statement.

  2. { -1, 1, 6, 3, 5, 7, 4, 9, 2 }

    Returns: 8

    The same square, but this time with the number 8 removed.

  3. { 5, 15, 13, 19, 11, 3, 9, 7, -1 }

    Returns: 17

    The missing number is 17.

  4. { 5, 15, -1, 19, 11, 3, 9, 7, 17 }

    Returns: 13

    13

  5. { 5, 15, 13, 19, 11, 3, -1, 7, 17 }

    Returns: 9

    9

  6. { -1, 1, 67, 61, 37, 13, 7, 73, 31 }

    Returns: 43

  7. { 43, -1, 7, 1, 37, 73, 67, 13, 31 }

    Returns: 61

  8. { 7, 73, -1, 61, 37, 13, 43, 1, 67 }

    Returns: 31

  9. { 67, 1, 43, -1, 37, 61, 31, 73, 7 }

    Returns: 13

  10. { 7, 61, 43, 73, -1, 1, 31, 13, 67 }

    Returns: 37

  11. { 31, 13, 67, 73, 37, -1, 7, 61, 43 }

    Returns: 1

  12. { 31, 73, 7, 13, 37, 61, -1, 1, 43 }

    Returns: 67

  13. { 26, 21, 28, 27, 25, 23, 22, -1, 24 }

    Returns: 29

  14. { 67, 13, 31, 1, 37, 73, 43, 61, -1 }

    Returns: 7

  15. { 99, 92, 97, 94, 96, -1, 95, 100, 93 }

    Returns: 98

  16. { 99, 92, 97, 94, 96, 98, 95, -1, 93 }

    Returns: 100

  17. { 5, 15, 13, 19, 11, 3, 9, 7, -1 }

    Returns: 17

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

    Returns: 9


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: