Sort by

recency

|

1237 Discussions

|

  • + 0 comments

    Go solution:

    func formingMagicSquare(s [][]int32) int32 {
        combinations := [][][]int32 {
            { 
                {8, 1, 6}, 
                {3, 5, 7}, 
                {4, 9, 2}, 
            }, 
            { 
                {6, 1, 8}, 
                {7, 5, 3},
                {2, 9, 4},
            }, 
            { 
                {4, 9, 2},
                {3, 5, 7},
                {8, 1, 6}, 
            }, 
            { 
                {2, 9, 4}, 
                {7, 5, 3}, 
                {6, 1, 8}, 
            }, 
            { 
                {8, 3, 4}, 
                {1, 5, 9}, 
                {6, 7, 2}, 
            }, 
            { 
                {4, 3, 8}, 
                {9, 5, 1}, 
                {2, 7, 6}, 
            }, 
            { 
                {6, 7, 2},
                {1, 5, 9}, 
                {8, 3, 4},
            }, 
            { 
                {2, 7, 6}, 
                {9, 5, 1}, 
                {4, 3, 8}, 
            },
        }
        
        minCost := int32(math.MaxInt32) 
        for _ , combination := range combinations {
            cost := int32(0)
            for i := 0; i < 3; i++ {
                for j := 0; j < 3; j++ {
                    cost += int32(math.Abs(float64(combination[i][j] - s[i][j])))               
                }
            }
            
            if cost < minCost {
                minCost = cost
            }
        }
        
        return minCost
    }
    
  • + 3 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?

  • + 0 comments
    function formingMagicSquare(s) {
        const magicSquares = [
            [8, 1, 6, 3, 5, 7, 4, 9, 2],
            [6, 1, 8, 7, 5, 3, 2, 9, 4],
            [4, 9, 2, 3, 5, 7, 8, 1, 6],
            [2, 9, 4, 7, 5, 3, 6, 1, 8],
            [8, 3, 4, 1, 5, 9, 6, 7, 2],
            [4, 3, 8, 9, 5, 1, 2, 7, 6],
            [6, 7, 2, 1, 5, 9, 8, 3, 4],
            [2, 7, 6, 9, 5, 1, 4, 3, 8],
        ];
    
        let minCost = Infinity;
    
        s = s.flat();
    
        for (let ms of magicSquares) {
            let sum = 0;
    
            for (let i = 0; i < 9; i++) {
                sum += Math.abs(ms[i] - s[i]);
            }
            minCost = Math.min(sum, minCost);
        }
    
        return minCost;
    }
    
  • + 0 comments
    def rotange(mat :list):
        u = mat[0][1]
        l = mat[1][0]
        d = mat[2][1]
        r = mat[1][2]
        # change angle
        mat[0][1] = l
        mat[1][0] = d
        mat[2][1] = r
        mat[1][2] = u
    
    
        top_left = mat[0][0]
        top_right = mat[0][-1]
        bottom_left = mat[-1][0]
        bottom_right = mat[-1][-1]
        # change concer
        mat[0][0] = bottom_left
        mat[0][-1] = top_left
        mat[-1][-1] = top_right
        mat[-1][0] = bottom_right
    
        return mat
    def row(mat):
        for i in range(3):
            mat[i][0],mat[i][-1] = mat[i][-1],mat[i][0]
        return mat
    def column(mat):
        for i in range(3):
            mat[0][i],mat[-1][i] = mat[-1][i],mat[0][i]
        return mat
    
    def formingMagicSquare(s):
        magic = [
        [[6, 1, 8], [7, 5, 3], [2, 9, 4]],
        [[8, 1, 6], [3, 5, 7], [4, 9, 2]],
        [[4, 9, 2], [3, 5, 7], [8, 1, 6]],
        [[4, 3, 8], [9, 5, 1], [2, 7, 6]],
        [[2, 7, 6], [9, 5, 1], [4, 3, 8]],  
        [[2, 9, 4], [7, 5, 3], [6, 1, 8]],
        [[6, 7, 2], [1, 5, 9], [8, 3, 4]],
        [[8, 3, 4], [1, 5, 9], [6, 7, 2]]]
        cost = 10e18
        for arr in magic:
            ans = 0
            for i in range(3):
                for j in range(3):
                    ans += abs(arr[i][j] - s[i][j])
            print(ans)
            cost = min(cost,ans)
    
    
    
        return cost
    
  • + 0 comments

    C# solution:

    using MagicSquare = System.Collections.Generic.List>; using Row = System.Collections.Generic.List;

    class Result {

    const int MAGIC_CONSTANT = 15;
    private static MagicSquare[]? allMagicSquares = null;
    public static int formingMagicSquare(MagicSquare s)
    {
        allMagicSquares ??= GetAllMagicSquares();
        int minCost = allMagicSquares.Min(square => Cost(square, s));
        return minCost;
    }
    
    private static int Cost(MagicSquare first, MagicSquare second)
    {
        int cost = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cost += Math.Abs(first[i][j] - second[i][j]);
            }
        }
        return cost;
    }
    
    private static HashSet<int> Union(HashSet<int> source, int i)
    {
        HashSet<int> result = new HashSet<int>(source);
        result.Add(i);
        return result;
    }
    
    private static MagicSquare[] GetAllMagicSquares()
    => (
        from c1 in Enumerable.Range(1, 9)
        let set1 = new HashSet<int>() { c1 }
    
        from c2 in Enumerable.Range(1, 9)
        where !set1.Contains(c2)
        let set2 = Union(set1, c2)
    
        let c3 = MAGIC_CONSTANT - (c1 + c2)
        where !set2.Contains(c3)
        let set3 = Union(set2, c3)
    
        from c4 in Enumerable.Range(1, 9)
        where !set3.Contains(c4)
        let set4 = Union(set3, c4)
    
        from c5 in Enumerable.Range(1, 9)
        where !set4.Contains(c5)
        let set5 = Union(set4, c5)
    
        let c6 = MAGIC_CONSTANT - (c4 + c5)
        where !set5.Contains(c6)
        let set6 = Union(set5, c6)
    
        let c7 = MAGIC_CONSTANT - (c1 + c4)
        where !set6.Contains(c7)
        let set7 = Union(set6, c7)
    
        let c8 = MAGIC_CONSTANT - (c2 + c5)
        where !set7.Contains(c8)
        let set8 = Union(set7, c8)
    
        let c9 = MAGIC_CONSTANT - (c7 + c8)
        where !set8.Contains(c9)
        && c3 + c6 + c9 == MAGIC_CONSTANT
        // diagonals
        && c1 + c5 + c9 == MAGIC_CONSTANT
        && c3 + c5 + c7 == MAGIC_CONSTANT
        select new MagicSquare()
        {
            new Row(){c1, c2, c3},
            new Row(){c4, c5, c6},
            new Row(){c7, c8, c9}
        }).ToArray();
    

    } `