Forming a Magic Square
Problem
Submissions
Leaderboard
Discussions
Editorial
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 matrix 3X3 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 :
5 3 4
1 5 8
6 4 2
We can convert it to the following magic square:
8 3 4
1 5 9
6 7 2
This took three replacements at a cost of
|5-8| + |8-9| + |4-7| = 7.
Github link
https://github.com/RakeshKrishna143/h...
On this page of the site you can watch the video online Forming a magic square | Hackerrank Solution in Python with a duration of hours minute second in good quality, which was uploaded by the user Engineers Revolution 08 February 2020, share the link with friends and acquaintances, this video has already been watched 12,635 times on youtube and it was liked by 94 viewers. Enjoy your viewing!