Forming a Magic Square

Forming a Magic Square

We define a magic square to be an n X n matrix of distinct positive integers from 1 to n2 where the sum of any row, column, or diagonal of length n is always equal to the same number: the magic constant.

You will be given a 3 X 3 matrix s of integers in the inclusive range [1,9]. We can convert any digit a to any other digit b in the range [1,9] at cost of |a - b|. Given s, convert it into a magic square at minimal cost. Print this cost on a new line.

Note: The resulting magic square must contain distinct integers in the inclusive range [1,9].

For example, we start with the following matrix s:

1
2
3
5 3 4
1 5 8
6 4 2

We can convert it to the following magic square:

1
2
3
8 3 4
1 5 9
6 7 2

This took three replacements at a cost of |5 - 8| + |8 - 9| + |4 - 7| = 7.

Function Description

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.

formingMagicSquare has the following parameter(s):

  • s: a 3 X 3 array of integers

Input Format

Each of the lines contains three space-separated integers of row s[i].

Constraints

Output Format

Print an integer denoting the minimum cost of turning matrix s into a magic square.

Sample Input 0

1
2
3
4 9 2
3 5 7
8 1 5

Sample Output 0

1
1

Explanation 0

If we change the bottom right value, s[2][2], from 5 to 6 at a cost of |6 - 5| = 1, s becomes a magic square at the minimum possible cost.

Sample Input 1

1
2
3
4 8 2
4 5 7
6 1 6

Sample Output 1

1
4

Explanation 1

Using 0-based indexing, if we make

  • s[0][1]->9 at a cost of |9 - 8| = 1
  • s[1][0]->3 at a cost of |3 - 4| = 1
  • s[2][0]->8 at a cost of |8 - 6| = 2,

then the total cost will be 1 + 1 + 2 = 4.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Complete the formingMagicSquare function below.
function formingMagicSquare(s) {
const squares = ['618753294', '816357492', '834159672', '438951276', '672159834', '276951438', '294753618', '492357816'];
let min = 100;
let cost = (s, squares) => {

return [...s.map(value => value.join('')).join('')].reduce((target, item, index) => {
target += Math.abs(+item - +squares[index])

return target;
}, 0)
};

squares.forEach((item, index) => {
let value = cost(s, squares[index]);

(value < min) && (min = value);
});

return min;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×