Sort by

recency

|

1248 Discussions

|

  • + 0 comments

    Java 8 Code

    public static int formingMagicSquare(List> s) { // Write your code here // mid[1][1]=5, total= 45,col=15,row=15. int cost[]={0,0,0,0,0,0,0,0}; int t[][]={ {2,9,4,7,5,3,6,1,8}, {4,9,2,3,5,7,8,1,6}, {4,3,8,9,5,1,2,7,6}, {8,3,4,1,5,9,6,7,2}, {6,7,2,1,5,9,8,3,4}, {2,7,6,9,5,1,4,3,8}, {6,1,8,7,5,3,2,9,4}, {8,1,6,3,5,7,4,9,2}, };

    for(int i=0;i<8;i++){ cost[i]= Math.abs(t[i][0]-s.get(0).get(0))+Math.abs(t[i][1] -s.get(0).get(1))+Math.abs(t[i][2]-s.get(0).get(2));

    cost[i]=cost[i]+ Math.abs(t[i][3]-s.get(1).get(0))+Math.abs(t[i][4]-s.get(1).get(1))+Math.abs(t[i][5]-s.get(1).get(2));
    
    cost[i]=cost[i]+Math.abs(t[i][6]-s.get(2).get(0))+Math.abs(t[i][7]-s.get(2).get(1))+Math.abs(t[i][8]-s.get(2).get(2));
    
    }
    Arrays.sort(cost);
    return cost[0]; 
    
  • + 0 comments

    My JavaScript Solution

    function formingMagicSquare(s) {
        // Write your code here
        
    const square1 =  [[8, 1, 6], [3, 5, 7], [4, 9, 2]];
    
    const square2 =  [[6, 1, 8], [7, 5, 3], [2, 9, 4]];
    
    const square3 =  [[4, 9, 2], [3, 5, 7], [8, 1, 6]];
    
    const square4 =  [[2, 9, 4], [7, 5, 3], [6, 1, 8]];
    
    const square5 =  [[8, 3, 4], [1, 5, 9], [6, 7, 2]];
    
    const square6 =  [[4, 3, 8], [9, 5, 1], [2, 7, 6]];
    
    const square7 =  [[6, 7, 2], [1, 5, 9], [8, 3, 4]];
    
    const square8 =  [[2, 7, 6], [9, 5, 1], [4, 3, 8]];
    
    
    function calculateDifference(s,t) {
        
    const diff = 
        
     Math.abs ( s[0][0] - t[0][0] ) + Math.abs ( s[0][1] - t[0][1] ) + Math.abs ( s[0][2] - t[0][2] ) + 
     
     Math.abs ( s[1][0] - t[1][0] ) + Math.abs ( s[1][1] - t[1][1] )  + Math.abs ( s[1][2] - t[1][2] ) +  
     
     Math.abs ( s[2][0] - t[2][0] ) + Math.abs ( s[2][1] - t[2][1] )  + Math.abs ( s[2][2] - t[2][2] ) 
      
     
     return diff
        
    }
    
    
    const count1=calculateDifference(s,square1); 
    
    const count2=calculateDifference(s,square2); 
    
    const count3=calculateDifference(s,square3); 
    
    const count4=calculateDifference(s,square4); 
    
    const count5=calculateDifference(s,square5); 
    
    const count6=calculateDifference(s,square6); 
    
    const count7=calculateDifference(s,square7); 
    
    const count8=calculateDifference(s,square8); 
    
    
    const counts = [count1,count2,count3, count4, count5, count6, count7, count8]
    
    
    
    return Math.min(...counts)
    
    }
    
  • + 0 comments
    public static int formingMagicSquare(List<List<Integer>> s) {
            int[][][] 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}}
            };
    
            int minCost = Integer.MAX_VALUE;
    
            for (int[][] magicSquare : magicSquares) {
                int cost = getMinCost(s, magicSquare);
                minCost = Math.min(minCost, cost);
            }
    
            return minCost;
        }
    
    
    public static int getMinCost(List<List<Integer>> s, int[][] magicSquare) {
            int cost = 0;
    
            for (int row = 0; row < magicSquare.length; row++) {
                for (int col = 0; col < magicSquare[0].length; col++) {
                    cost += Math.abs(magicSquare[row][col] - s.get(row).get(col));
    }
    }
            return cost;
        }
    
  • + 0 comments

    My Javascript Solution:

    function formingMagicSquare(s) {
        let possible_a_b = [
            [-3,-1],
            [-3,1],
            [-1,-3],
            [-1,3],
            [1,-3],
            [1,3],
            [3,-1],
            [3,1]
        ]
        let costs = [0,0,0,0,0,0,0,0]
        for (let i = 0; i < possible_a_b.length; i++) {
            let [a,b] = possible_a_b[i];
            let magicSquare = [
                [5-b, 5+(a+b), 5 -a],
                [5-(a-b), 5, 5+(a-b)],
                [5+a, 5-(a+b), 5+b]
            ]
            for (let j = 0; j < s.length; j++) {
                for (let k = 0; k < s[j].length; k++) {
                    if (s[j][k] !== magicSquare[j][k]) {
                        costs[i] = costs[i] + Math.abs(s[j][k] - magicSquare[j][k])
                    }
                }
            }
        }
        costs.sort((a,b) => a -b)
        return costs[0]
    

    }

  • + 0 comments
    def formingaSquare(s):
        magic_squares = [
            [[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]]]
        min_cost = float('inf')
        for magic in magic_squares:
            cost = 0
            for i in range(3):
                for j in range(3):
                    cost += abs(s[i][j] - magic[i][j])
            min_cost = min(min_cost, cost)
        return min_cost