You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# We define a magic square to be an matrix of distinct positive integers from to where the sum of any row, column, or diagonal of length is always equal to the same number: the magic constant.
91
+
92
+
# You will be given a matrix of integers in the inclusive range . We can convert any digit to any other digit in the range at cost of . Given , convert it into a magic square at minimal cost. Print this cost on a new line.
93
+
94
+
# Note: The resulting magic square must contain distinct integers in the inclusive range .
95
+
96
+
# For example, we start with the following matrix :
97
+
98
+
# 5 3 4
99
+
# 1 5 8
100
+
# 6 4 2
101
+
# We can convert it to the following magic square:
102
+
103
+
# 8 3 4
104
+
# 1 5 9
105
+
# 6 7 2
106
+
# This took three replacements at a cost of .
107
+
108
+
# Function Description
109
+
110
+
# Complete the formingMagicSquare function in the editor below. It should return an integer that represents the minimal total cost of converting the input square to a magic square.
111
+
112
+
# formingMagicSquare has the following parameter(s):
113
+
114
+
# s: a array of integers
115
+
# Input Format
116
+
117
+
# Each of the lines contains three space-separated integers of row .
118
+
119
+
# Constraints
120
+
121
+
# Output Format
122
+
123
+
# Print an integer denoting the minimum cost of turning matrix into a magic square.
124
+
125
+
# Sample Input 0
126
+
127
+
# 4 9 2
128
+
# 3 5 7
129
+
# 8 1 5
130
+
# Sample Output 0
131
+
132
+
# 1
133
+
# Explanation 0
134
+
135
+
# If we change the bottom right value, , from to at a cost of , becomes a magic square at the minimum possible cost.
0 commit comments