MagicSquare
SRM 188 · 2004-03-25 · by legakis
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
The completed magic square will consist of exactly 9 distinct positive integers.
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.
{ 8, 1, 6, 3, 5, -1, 4, 9, 2 }
Returns: 7
This is the example from the problem statement.
{ -1, 1, 6, 3, 5, 7, 4, 9, 2 }
Returns: 8
The same square, but this time with the number 8 removed.
{ 5, 15, 13, 19, 11, 3, 9, 7, -1 }
Returns: 17
The missing number is 17.
{ 5, 15, -1, 19, 11, 3, 9, 7, 17 }
Returns: 13
13
{ 5, 15, 13, 19, 11, 3, -1, 7, 17 }
Returns: 9
9
Submissions are judged against all 18 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MagicSquare with a public method int missing(vector<int> square) · 18 test cases · 2 s / 256 MB per case