Sort by

recency

|

1240 Discussions

|

  • + 0 comments
    function formingMagicSquare(s) {
        
        var M = 15
        var y = 5
        var x = 3
        var j = [... new Array(3)].map(x =>Array(3).fill())
        
        var flat_s = s.flat()
        
            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]
        ];
        
        var flat_squres = magicSquares.flat()
        
    
        
    var sum = Infinity
    var current_sum = 0
     
     var j = 0   
     for(var i = 0; i < flat_squres.length;i++){
        
        console.log(`m ${flat_squres[i]} vs ${flat_s[j]}`)
        current_sum += Math.abs(flat_squres[i] - flat_s[j])
        j = j + 1
        if (j == 9){
            
    
            sum = sum > current_sum ? current_sum : sum
            current_sum = 0
            j = 0
            
        }
    
     }
     
     console.log(sum)
     return sum
    
        
        
        
    }
    
  • + 0 comments

    JS/Javascript solution:-

    function formingMagicSquare(s) {
        // Flatten the input 2D array
        const flatInput = s.flat();
    
        // Define all possible 3x3 magic squares
        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]
        ];
    
        // Calculate the minimum cost to convert to any magic square
        let minCost = Infinity;
    
        for (const magic of magicSquares) {
            let cost = 0;
            for (let i = 0; i < 9; i++) {
                cost += Math.abs(flatInput[i] - magic[i]);
            }
            minCost = Math.min(minCost, cost);
        }
    
        return minCost;
    }
    
  • + 0 comments
    def formingMagicSquare(s):
        magic_matrix = [
            [[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]]
        ]
        costs = []
        for matrix in magic_matrix:
            diff = []
            for i in range(3):
                for j in range(3):
                    diff.append(abs(s[i][j] - matrix[i][j]))
            costs.append(sum(diff))
        
        return min(costs)
    
  • + 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
    }
    
  • + 7 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?