Sort by

recency

|

1257 Discussions

|

  • + 0 comments

    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;
    

    }

  • + 1 comment

    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

  • + 0 comments

    Guys, I am getting an output which doesn't match with the expected outputs of few test cases but the cost that I am getting as output is less than the expected min cost and even the final matrix also contains all distinct elements. Is anyone facing this problem or is the expected output = correct min cost? call center earphones with mic

  • + 0 comments
    def formingMagicSquare(s):
        a_b = [
            [-3,-1],
            [-3,1],
            [-1,-3],
            [-1,3],
            [1,-3],
            [1,3],
            [3,-1],
            [3,1]
        ]
        costs =[]
        for a,b in a_b:
            ms = [
                [5-b, 5+(a+b), 5 -a],
                [5-(a-b), 5, 5+(a-b)],
                [5+a, 5-(a+b), 5+b]
            ]
            cost = 0
            for i in range(3):
                for k in range(3):
                    cost+=abs(s[i][k]-ms[i][k])
            
            costs.append(cost)
        return min(costs)
    
  • + 1 comment

    I am getting an output which doesn't match with the expected outputs of few test cases but the cost that I am getting as output is less than the expected min cost and even the final matrix also contains all distinct elements. Is anyone facing this problem or is the expected output = correct min cost? ACFT Calculator