0% found this document useful (0 votes)
34 views3 pages

Forming A Magic Square - HackerRank

com oresulver on cuadrado magico

Uploaded by

DaNy c TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Forming A Magic Square - HackerRank

com oresulver on cuadrado magico

Uploaded by

DaNy c TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

26/11/24, 8:51 a.m.

Forming a Magic Square | HackerRank

 

30 more points to get your first star!


Forming a Magic Square  Rank: 5789303 | Points: 0/30
Problem Solving

Problem Submissions Leaderboard Editorial 

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.

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.

Note: The resulting magic square must contain distinct integers in the inclusive range .

Example

$s = [[5, 3, 4], [1, 5, 8], [6, 4, 2]]

The matrix looks like this:

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 .

Function Description

Complete the formingMagicSquare function in the editor below.

formingMagicSquare has the following parameter(s):


int s[3][3]: a array of integers

Returns
int: the minimal total cost of converting the input square to a magic square

Input Format

Each of the lines contains three space-separated integers of row .

Constraints

Sample Input 0

492
357
815

Sample Output 0

Explanation 0

If we change the bottom right value, , from to at a cost of , becomes a magic square at the minimum possible cost.

https://www.hackerrank.com/challenges/magic-square-forming/problem 1/3
26/11/24, 8:51 a.m. Forming a Magic Square | HackerRank
Sample Input 1

482
457
616

Sample Output 1

Explanation 1

Using 0-based indexing, if we make


-> at a cost of

-> at a cost of
-> at a cost of ,

then the total cost will be .

Change Theme Language C++11

1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7 vector<string> split(const string &);
8
9 /*
10 * Complete the 'formingMagicSquare' function below.
11 *
12 * The function is expected to return an INTEGER.
13 * The function accepts 2D_INTEGER_ARRAY s as parameter.
14 */
15
16 int formingMagicSquare(vector<vector<int>> s) {
17
18 }
19
20 int main()
21 {
22 ofstream fout(getenv("OUTPUT_PATH"));
23
24 vector<vector<int>> s(3);
25
26 for (int i = 0; i < 3; i++) {
27 s[i].resize(3);
28
29 string s_row_temp_temp;
30 getline(cin, s_row_temp_temp);
31
32 vector<string> s_row_temp = split(rtrim(s_row_temp_temp));
33
34 for (int j = 0; j < 3; j++) {
35 int s_row_item = stoi(s_row_temp[j]);
36
37 s[i][j] = s_row_item;
38 }
39 }
40
41 int result = formingMagicSquare(s);
42
https://www.hackerrank.com/challenges/magic-square-forming/problem 2/3
26/11/24, 8:51 a.m. Forming a Magic Square | HackerRank
43 fout << result << "\n";
44
45 fout.close();
46
47 return 0;
48 }
49
50 string ltrim(const string &str) {
51 string s(str);
Line: 88 Col: 1

 Upload Code as File Test against custom input Run Code Submit Code

Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy

https://www.hackerrank.com/challenges/magic-square-forming/problem 3/3

You might also like