# The Problem:

`5 3 41 5 86 4 2`
`8 3 41 5 96 7 2`
• int s: a 3x3 array of integers
• int: the minimal total cost of converting the input square to a magic square

# The Solution:

`function formingMagicSquare(s) {    // Length of array    let n = s.length    let newArr = []    // iterate through matrix, add each element to newArr    for (let i = 0; i < n; i++) {        for (let j = 0; j < n; j++) {            newArr.push(s[i][j])        }    }    // print all corner numbers (index: 0,2,6,8) (values: 2-8,4-6,8-2,6-4)    // candidate pairs// (1,9)(9,1)// (3,7)(7,3)    // (2,8)(8,2)// (4,6)(6,4)        let changes = []    let result = 0    for (let i = 0; i < newArr.length; i++) {        // NOTE: Work on writing a dynamic code block         switch (i) {            case 0: {                if (newArr[i] == 8) {                    let beforeChange = newArr                    newArr = 2                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 2) {                    let beforeChange = newArr                    newArr = 8                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 4) {                    let beforeChange = newArr                    newArr = 6                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 6) {                    let beforeChange = newArr                    newArr = 4                    changes.push(Math.abs(beforeChange - newArr))                }            }                break;            case 1:                if (newArr[i] == 1) {                    let beforeChange = newArr                    newArr = 9                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 9) {                    let beforeChange = newArr                    newArr = 1                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 3) {                    let beforeChange = newArr                    newArr = 7                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 7) {                    let beforeChange = newArr                    newArr = 3                    changes.push(Math.abs(beforeChange - newArr))                }                break;            case 2: {                if (newArr[i] == 8) {                    let beforeChange = newArr                    newArr = 2                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 2) {                    let beforeChange = newArr                    newArr = 8                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 4) {                    let beforeChange = newArr                    newArr = 6                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 6) {                    let beforeChange = newArr                    newArr = 4                    changes.push(Math.abs(beforeChange - newArr))                }            }                break;            case 3: {                if (newArr[i] == 1) {                    let beforeChange = newArr                    newArr = 9                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 9) {                    let beforeChange = newArr                    newArr = 1                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 3) {                    let beforeChange = newArr                    newArr = 7                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 7) {                    let beforeChange = newArr                    newArr = 3                    changes.push(Math.abs(beforeChange - newArr))                }                break;            }            case 4: {                let beforeChange = newArr[i]                newArr[i] = 5                changes.push(Math.abs(beforeChange - newArr[i]))                break;            }            case 5: {                if (newArr[i] == 1) {                    let beforeChange = newArr                    newArr = 9                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 9) {                    let beforeChange = newArr                    newArr = 1                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 3) {                    let beforeChange = newArr                    newArr = 7                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 7) {                    let beforeChange = newArr                    newArr = 3                    changes.push(Math.abs(beforeChange - newArr))                }                break;            }            case 6: {                if (newArr[i] == 8) {                    let beforeChange = newArr                    newArr = 2                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 2) {                    let beforeChange = newArr                    newArr = 8                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 4) {                    let beforeChange = newArr                    newArr = 6                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 6) {                    let beforeChange = newArr                    newArr = 4                    changes.push(Math.abs(beforeChange - newArr))                }            }                break;            case 7:                if (newArr[i] == 1) {                    let beforeChange = newArr                    newArr = 9                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 9) {                    let beforeChange = newArr                    newArr = 1                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 3) {                    let beforeChange = newArr                    newArr = 7                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 7) {                    let beforeChange = newArr                    newArr = 3                    changes.push(Math.abs(beforeChange - newArr))                }                break;            case 8: {                if (newArr[i] == 8) {                    let beforeChange = newArr                    newArr = 2                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 2) {                    let beforeChange = newArr                    newArr = 8                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 4) {                    let beforeChange = newArr                    newArr = 6                    changes.push(Math.abs(beforeChange - newArr))                }                if (newArr[i] == 6) {                    let beforeChange = newArr                    newArr = 4                    changes.push(Math.abs(beforeChange - newArr))                }            }                break;            default:        }        result = changes.reduce((acc, cur) => {            return acc + cur        })      }      console.log("Result: ", result)  return result}`

# Thought Process:

• 3 x 3 magic square needs to sum up to 15.
• 4 corners of the square need to be even numbers
• Middle square is going to always be a 5
• Numbers surrounding the 5 thats are not corners need to be odd numbers
• Diagonal corner numbers have the be either [2 and 8] or [4 and 6] because they have to add up to 10 since the sum of the diagonal should be 10 + 5(since 5 is always in the middle)
• Odd diagonals also have to add up to 10 so the pairs for those are [7 and 3] or [9 and 1]
1. We got the length of one array in the 2D array.
2. Iterated through the dynamic array and push the values to a new array
3. Then we declared a new empty array and a counter that started at 0.
4. Now we iterate for each number in the new array to check is they are following the previous rules mentioned. (This works because the numbers were pushed in order.)
5. We check each number with a switch case. While keeping track of previous numbers and newly changed numbers.
6. We then subtract the previous change number and the new number to get the difference. And push the difference to the changes array.
7. We later sum all of the numbers in the changes array resulting in the total cost. Which we save in the results variable.

--

--

--

## More from Nerly Ton

Love podcasts or audiobooks? Learn on the go with our new app.

## How to Insert & Select data using SQLite in a C# console application ## PRTG Network Monitoring ## Best practice scraping websites using pagination with indeed.com case studies  ## Machine Learning 101 All Algorithms in python (Logistic Regression) ## Why you need to GIT good ## 4 branching workflows for Git ## Jenkins with Dockerhub integration  ## More from Medium  