Organizing Containers of Balls

Sort by

recency

|

25 Discussions

|

  • + 0 comments
    public static String isSwapPossible(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        Map<Integer, BigDecimal> rowMap = new HashMap<>();
        Map<Integer, BigDecimal> colMap = new HashMap<>();
    
        for (int i = 0; i < rows; i++) {
    
            for (int j = 0; j < cols; j++) {
                BigDecimal currentRowSum = rowMap.get(i);
                if (currentRowSum != null) {
                    rowMap.put(i, currentRowSum.add(new BigDecimal(matrix[i][j])));
                } else {
                    rowMap.put(i, new BigDecimal(matrix[i][j]));
                }
            }
    
        }
    
        for (int j = 0; j < cols; j++) {
    
            for (int i = 0; i < rows; i++) {
                BigDecimal currentColSum = colMap.get(j);
    
                if (currentColSum != null) {
                    colMap.put(j, currentColSum.add(new BigDecimal(matrix[i][j])));
                } else {
                    colMap.put(j, new BigDecimal(matrix[i][j]));
                }
            }
            }
    
        List<BigDecimal> rowList = new ArrayList<>(rowMap.values());
        List<BigDecimal> colList = new ArrayList<>(colMap.values());
    
        Collections.sort(rowList);
        Collections.sort(colList);
    
        if (!rowList.equals(colList)) {
            return "Impossible";
        }
    
        return "Possible";
    
    }
    
  • + 0 comments

    excerpt (Haskell one-liner)

    sol m = (sort $ sum <$> m) == (sort $ sum <$> transpose m)
    
  • + 0 comments

    Can you proove if sorted arrays are same, we can swap to achieve the goal? @ma5termind

  • + 0 comments
    for _ in range(int(input())):
        n = int(input())
        arr = [list(map(int,input().split())) for i in range(n)]
        tot_balls = [sum([arr[i][j] for j in range(n)]) for i in range(n)]
        col_balls = [sum([arr[j][i] for j in range(n)]) for i in range(n)]
        print('Possible' if sorted(tot_balls)==sorted(col_balls) else 'Impossible')
    
  • + 0 comments

    help please

    import java.io.*;
    
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    import java.util.function.*;
    import java.util.stream.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int q = in.nextInt();
            for(int a0 = 0; a0 < q; a0++){
                int n = in.nextInt();
                int[][] M = new int[n][n];
                for(int M_i=0; M_i < n; M_i++){
                    for(int M_j=0; M_j < n; M_j++){
                        M[M_i][M_j] = in.nextInt();
                    }
                }
                int k=0;
                int count1=0;
                int count=0;
                 List<Integer> l1=null;
                        List<Integer> l2=null;
                // your code goes here
                for(int i=0;i<n-1;i++){
                    if(k<(n-1))
                       for(int j=0;j<n;j++){
                       l1=new ArrayList<Integer>(M[k][j]);
                         l2=new ArrayList<Integer>(M[k+1][j]);
                        
                       
                    
                   }
                     int sum1=l1.stream().mapToInt(Integer::intValue).sum();
                        int sum2=l2.stream().mapToInt(Integer::intValue).sum();
                    if((sum1==sum2)){
                        count++;
                    }k++;
                       
                    
                }
                
                k=0;
                
                
                
                for(int i=0;i<n-1;i++){
                     if(k<(n-1))
                    for(int j=0;j<n;j++){
                      l1=new ArrayList<Integer>(M[j][k]);
                      l2=new ArrayList<Integer>(M[j][k+1]);
                        
                       
                    
                   }
                     int s1=l1.stream().mapToInt(Integer::intValue).sum();
                        int s2=l2.stream().mapToInt(Integer::intValue).sum();
                    if((s1==s2)){
                        count1++;
                    }else{count1--;} 
                    k++;
                  
                }
                
                if(count==count1){System.out.println("Possible");}
                else{System.out.println("Impossible");}
                
                
                
                
                
                
                
                
                
                
                
                
                
            }
        }
    }