Skip to content

Commit 04e8045

Browse files
author
Alejandro Gonzalez Romero
committed
feat: forming a magic square algorithm
1 parent 81b6774 commit 04e8045

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,33 @@
2020

2121
## Algorithms
2222

23-
| # | Subdomain | Challenge | Solution | Difficulty | Points |
24-
|----|----------------|--------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------:|:----------:|:------:|
25-
| 1 | Warmup | [Solve Me First](https://www.hackerrank.com/challenges/solve-me-first) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/solve-me-first/solve-me-first.js) | Easy | 1 |
26-
| 2 | Warmup | [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/simple-array-sum/simple-array-sum.js) | Easy | 10 |
27-
| 3 | Warmup | [Compare the Triplets](https://www.hackerrank.com/challenges/compare-the-triplets) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/compare-the-triplets/compare-the-triplets.js) | Easy | 10 |
28-
| 4 | Warmup | [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/a-very-big-sum/a-very-big-sumdiagonal-difference.js) | Easy | 10 |
29-
| 5 | Warmup | [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/diagonal-difference/diagonal-difference.js) | Easy | 10 |
30-
| 6 | Warmup | [Plus Minus](https://www.hackerrank.com/challenges/plus-minus) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/plus-minus/plus-minus.js) | Easy | 10 |
31-
| 7 | Warmup | [Staircase](https://www.hackerrank.com/challenges/staircase) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/staircase/staircase.js) | Easy | 10 |
32-
| 8 | Warmup | [Mini-Max Sum](https://www.hackerrank.com/challenges/mini-max-sum) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/mini-max-sum/mini-max-sum.js) | Easy | 10 |
33-
| 9 | Warmup | [Time Conversion](https://www.hackerrank.com/challenges/time-conversion) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/time-conversion/time-conversion.js) | Easy | 15 |
34-
| 10 | Warmup | [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/birthday-cake-candles/birthday-cake-candles.js) | Easy | 10 |
35-
| 11 | Implementation | [Grading Students](https://www.hackerrank.com/challenges/grading) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/grading-students/grading-students.js) | Easy | 10 |
36-
| 12 | Implementation | [Apple and Orange](https://www.hackerrank.com/challenges/apple-and-orange) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/apple-and-orange/count-apples-and-oranges.js) | Easy | 10 |
37-
| 13 | Implementation | [Number Line Jumps](https://www.hackerrank.com/challenges/number-line-jumps) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/number-line-jumps/kangaroo.js) | Easy | 10 |
38-
| 14 | Implementation | [Between Two Sets](https://www.hackerrank.com/challenges/between-two-sets) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/between-two-sets/get-total-x.js) | Easy | 10 |
39-
| 15 | Implementation | [Subarray Division](https://www.hackerrank.com/challenges/the-birthday-bar) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/subarray-division/birthday.js) | Easy | 10 |
40-
| 16 | Implementation | [Divisible Sum Pairs](https://www.hackerrank.com/challenges/divisible-sum-pairs) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/divisible-sum-pairs/divisible-sum-pairs.js) | Easy | 10 |
41-
| 17 | Implementation | [Migratory Birds](https://www.hackerrank.com/challenges/migratory-birds) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/migratory-birds/migratory-birds.js) | Easy | 10 |
42-
| 18 | Implementation | [Day of the Programmer](https://www.hackerrank.com/challenges/day-of-the-programmer) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/day-of-the-programmer/day-of-programmer.js) | Easy | 15 |
43-
| 19 | Implementation | [Bon Appetit](https://www.hackerrank.com/challenges/bon-appetit) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/bill-division/bon-appetit.js) | Easy | 10 |
44-
| 20 | Implementation | [Sales by Match](https://www.hackerrank.com/challenges/sock-merchant) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/sales-by-match/sock-merchant.js) | Easy | 10 |
45-
| 21 | Implementation | [Drawing Book](https://www.hackerrank.com/challenges/drawing-book) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/drawing-book/page-count.js) | Easy | 10 |
46-
| 22 | Implementation | [Counting Valleys](https://www.hackerrank.com/challenges/counting-valleys) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/counting-valleys/counting-valleys.js) | Easy | 15 |
47-
| 23 | Implementation | [Electronics Shop](https://www.hackerrank.com/challenges/electronics-shop) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/electronics-shop/get-money-spent.js) | Easy | 15 |
48-
| 24 | Implementation | [Cats and a Mouse](https://www.hackerrank.com/challenges/cats-and-a-mouse) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/cats-and-a-mouse/cats-and-mouse.js) | Easy | 15 |
23+
| # | Subdomain | Challenge | Solution | Difficulty | Points |
24+
|----|----------------|--------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------:|:----------:|:------:|
25+
| 1 | Warmup | [Solve Me First](https://www.hackerrank.com/challenges/solve-me-first) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/solve-me-first/solve-me-first.js) | Easy | 1 |
26+
| 2 | Warmup | [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/simple-array-sum/simple-array-sum.js) | Easy | 10 |
27+
| 3 | Warmup | [Compare the Triplets](https://www.hackerrank.com/challenges/compare-the-triplets) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/compare-the-triplets/compare-the-triplets.js) | Easy | 10 |
28+
| 4 | Warmup | [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/a-very-big-sum/a-very-big-sumdiagonal-difference.js) | Easy | 10 |
29+
| 5 | Warmup | [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/diagonal-difference/diagonal-difference.js) | Easy | 10 |
30+
| 6 | Warmup | [Plus Minus](https://www.hackerrank.com/challenges/plus-minus) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/plus-minus/plus-minus.js) | Easy | 10 |
31+
| 7 | Warmup | [Staircase](https://www.hackerrank.com/challenges/staircase) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/staircase/staircase.js) | Easy | 10 |
32+
| 8 | Warmup | [Mini-Max Sum](https://www.hackerrank.com/challenges/mini-max-sum) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/mini-max-sum/mini-max-sum.js) | Easy | 10 |
33+
| 9 | Warmup | [Time Conversion](https://www.hackerrank.com/challenges/time-conversion) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/time-conversion/time-conversion.js) | Easy | 15 |
34+
| 10 | Warmup | [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/warmup/birthday-cake-candles/birthday-cake-candles.js) | Easy | 10 |
35+
| 11 | Implementation | [Grading Students](https://www.hackerrank.com/challenges/grading) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/grading-students/grading-students.js) | Easy | 10 |
36+
| 12 | Implementation | [Apple and Orange](https://www.hackerrank.com/challenges/apple-and-orange) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/apple-and-orange/count-apples-and-oranges.js) | Easy | 10 |
37+
| 13 | Implementation | [Number Line Jumps](https://www.hackerrank.com/challenges/number-line-jumps) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/number-line-jumps/kangaroo.js) | Easy | 10 |
38+
| 14 | Implementation | [Between Two Sets](https://www.hackerrank.com/challenges/between-two-sets) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/between-two-sets/get-total-x.js) | Easy | 10 |
39+
| 15 | Implementation | [Subarray Division](https://www.hackerrank.com/challenges/the-birthday-bar) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/subarray-division/birthday.js) | Easy | 10 |
40+
| 16 | Implementation | [Divisible Sum Pairs](https://www.hackerrank.com/challenges/divisible-sum-pairs) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/divisible-sum-pairs/divisible-sum-pairs.js) | Easy | 10 |
41+
| 17 | Implementation | [Migratory Birds](https://www.hackerrank.com/challenges/migratory-birds) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/migratory-birds/migratory-birds.js) | Easy | 10 |
42+
| 18 | Implementation | [Day of the Programmer](https://www.hackerrank.com/challenges/day-of-the-programmer) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/day-of-the-programmer/day-of-programmer.js) | Easy | 15 |
43+
| 19 | Implementation | [Bon Appetit](https://www.hackerrank.com/challenges/bon-appetit) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/bill-division/bon-appetit.js) | Easy | 10 |
44+
| 20 | Implementation | [Sales by Match](https://www.hackerrank.com/challenges/sock-merchant) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/sales-by-match/sock-merchant.js) | Easy | 10 |
45+
| 21 | Implementation | [Drawing Book](https://www.hackerrank.com/challenges/drawing-book) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/drawing-book/page-count.js) | Easy | 10 |
46+
| 22 | Implementation | [Counting Valleys](https://www.hackerrank.com/challenges/counting-valleys) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/counting-valleys/counting-valleys.js) | Easy | 15 |
47+
| 23 | Implementation | [Electronics Shop](https://www.hackerrank.com/challenges/electronics-shop) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/electronics-shop/get-money-spent.js) | Easy | 15 |
48+
| 24 | Implementation | [Cats and a Mouse](https://www.hackerrank.com/challenges/cats-and-a-mouse) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/cats-and-a-mouse/cats-and-mouse.js) | Easy | 15 |
49+
| 25 | Implementation | [Cats and a Mouse](https://www.hackerrank.com/challenges/magic-square-forming) | [Solution](https://github.com/algorodev/hackerrank-challenges/blob/master/algorithms/implementation/forming-a-magic-square/forming-magic-square.js) | Medium | 20 |
4950

5051
## Data Structures
5152

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const formingMagicSquare = (s) => {
2+
const magicSquares = [
3+
[[8, 1, 6], [3, 5, 7], [4, 9, 2]],
4+
[[6, 1, 8], [7, 5, 3], [2, 9, 4]],
5+
[[4, 9, 2], [3, 5, 7], [8, 1, 6]],
6+
[[2, 9, 4], [7, 5, 3], [6, 1, 8]],
7+
[[8, 3, 4], [1, 5, 9], [6, 7, 2]],
8+
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
9+
[[6, 7, 2], [1, 5, 9], [8, 3, 4]],
10+
[[2, 7, 6], [9, 5, 1], [4, 3, 8]],
11+
]
12+
13+
let minCost = Number.MAX_VALUE
14+
for (const element of magicSquares) {
15+
let cost = 0
16+
for (let j = 0; j < 3; j++) {
17+
for (let k = 0; k < 3; k++) {
18+
cost += Math.abs(s[j][k] - element[j][k])
19+
}
20+
}
21+
minCost = Math.min(minCost, cost)
22+
}
23+
24+
return minCost
25+
}

0 commit comments

Comments
 (0)