Sort by

recency

|

1260 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-forming-a-magic-square-problem-solution.html

  • + 0 comments

    for matrix 6 1 2 7 2 6 5 6 2

    cost is coming as 18 but it should be 20 as most economical transformation will be

    6 1 8 7 5 3 2 9 4

  • + 1 comment
    void printf_matrix(int s_rows, int s_columns, int** s){
        for (int i = 0; i<s_rows; i++){
            for (int j = 0; j<s_columns; j++){
                printf("%d ", s[i][j]);
            }
            printf("\n");
        }
    }
    
    int compute_cost(int s_rows, int s_columns, int** s, int** s_){
        int res = 0;
        for (int i = 0; i<s_rows; i++){
            for (int j = 0; j<s_columns; j++){
                res += abs(s[i][j] - s_[i][j]);            
            }
        }
        return res;
    }
    
    int formingMagicSquare(int s_rows, int s_columns, int** s) {
        // All 8 possible 3x3 magic squares
        int magic_squares[8][3][3] = {
            {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}},
            {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}},
            {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}},
            {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}},
            {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}},
            {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}},
            {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}},
            {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}
        };
    
        int min_cost = -1;
        int** best_matrix = NULL;
    
        // Compare input matrix against all 8 magic squares
        for (int k = 0; k < 8; k++) {
            int* magic_1D_pointer[3] = {magic_squares[k][0], magic_squares[k][1], magic_squares[k][2]};
            int** magic_ptr = magic_1D_pointer;
            int cost = compute_cost(s_rows, s_columns, s, magic_ptr);
            if (min_cost == -1 || cost < min_cost) {
                min_cost = cost;
                best_matrix = magic_ptr; // Points to static memory, no need to free
            }
        }
    
        printf_matrix(s_rows, s_columns, best_matrix);
        return min_cost;
    }
    
    • + 0 comments

      Forming a magic square is a fascinating mathematical exercise that involves arranging numbers in a square grid so that the sums of the numbers in each row, column, and both main diagonals are all the same. This constant sum is known as the magic constant or magic sum. Typically, a magic square uses a set of consecutive integers starting from 1. The simplest magic square royal dream x8 is a 3x3 grid, and it has a magic constant of 15 when filled with numbers 1 to 9. Constructing a magic square requires logical thinking, attention to pattern, and sometimes specific techniques depending on whether the square has an odd, even, or doubly even number of rows and columns.

  • + 1 comment

    The Fastest, concise, and most straight-forwarded Solution You will ever find across all discussion:

    include

    using namespace std;

    int main(){ int magic_sqr[8][9] = { {8,1,6,3,5,7,4,9,2}, {6,1,8,7,5,3,2,9,4}, {4,3,8,9,5,1,2,7,6}, {8,3,4,1,5,9,6,7,2}, {2,9,4,7,5,3,6,1,8}, {4,9,2,3,5,7,8,1,6}, {6,7,2,1,5,9,8,3,4}, {2,7,6,9,5,1,4,3,8} };

    int sqr[9];
    
    for(int i = 0; i < 9; i++){
        cin >> sqr[i];
    }
    
    int min_cost = 36; //'9 9 9 9 9 9 9 9 9' is converted to magic_sqr and it costs exactly 36 (absolute difference)
    
    for(int i = 0; i < 8; i++){
        int cost = 0;
        for(int j = 0; j < 9; j++){
            cost += abs(magic_sqr[i][j] - sqr[j]);
        }
        min_cost = min(cost, min_cost);
    }
    
    cout << min_cost << '\n';
    return 0;
    

    }

    • + 0 comments

      I came to the same solution in TypeScript, except without serialising the matrix s. I think this is the only comprehensive solution.

  • + 2 comments

    Could I get clarification? I implemented a code and found the cost lower than the one in the example for a new matrix. For the given matrix:
    4 8 2
    4 5 7
    6 1 6
    The matrix below solves it at a lower cost (this is what my code found):
    [4, 8, 2]
    [4, 4, 6] = cost is 2
    [6, 2, 6] = cost is 1
    The magic constant is 14 and The cost is 3

    • + 2 comments

      sum of rows and columns and diagonals for magic square is 15 not 14, also you cannot repeat numbers

      • + 0 comments

        its not mentioned as 15, so u can take anything as the sum

    • + 0 comments

      The diagonal does not add up to 14. 6 + 4 + 2 = 12.