Organizing Containers of Balls

Sort by

recency

|

601 Discussions

|

  • + 0 comments
    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    
    int main(){
        int q; 
        scanf("%d",&q);
        for(int a = 0; a < q; a++){
            int n,f=0,d=0; 
            scanf("%d",&n);
            int M[n][n],x[n],s,y=0,z[n],l;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++)
                scanf("%d ",&M[i][j]);
           
            }
           for(int i=0;i<n;i++){ 
               s=0;
               l=0;
                for(int j=0;j<n;j++)
                         s=s+M[j][i];                               
                      x[i]=s;      
               for(int j=0;j<n;j++)
                         l=l+M[i][j];       
                   z[i]=l; 
           }
            for(int i=0;i<n;i++) {
                d=0;
                  for(int j=0;j<n;j++)
                    if(x[i]==z[j]) d=1;
                      
                     f=f+d;   
            }
            if(f==n && d==1) printf("Possible\n");
            else printf("Impossible\n");
            
            
        }
        return 0;
    }
    
  • + 0 comments
    def organizingContainers(container):
        container_sizes = [sum(row) for row in container]
        ball_counts = [sum(i) for i in zip(*container)]
        return "Possible" if sorted(container_sizes) == sorted(ball_counts) else "Impossible"
    

    Python

  • + 0 comments

    Haskell:

    module Main where
    
    import Control.Monad (replicateM, replicateM_)
    import Data.List (sort, transpose)
    
    rowsums :: [[Integer]] -> [Integer]
    rowsums = map sum
    
    colsums :: [[Integer]] -> [Integer]
    colsums rows = map sum (transpose rows)
    
    testsums :: [[Integer]] -> Bool
    testsums rows = sort (rowsums rows) == sort (colsums rows)
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            n <- readLn :: IO Int
            rows <- replicateM n $ map read . words <$> getLine :: IO [[Integer]]
            putStrLn $ if testsums rows then "Possible" else "Impossible"
    
  • + 0 comments

    Python usng @mikesboyle solution:

        in_n = [sum(i) for i in container]
        ball_n = list(map(sum, zip(*container)))
        return "Possible" if sorted(in_n) == sorted(ball_n) else "Impossible"
    
  • + 0 comments

    Solution 1. Determine the size of the boxes 2. Determine the quantity of each type 3. If the quantity of each type corresponds to the size of the box -> return 'Possible' otherwise 'Impossible' Code:

    const boxs_hash: { [key: number]: number } = {};
      const ball_hash: { [key: number]: number } = {};
    
      for (let i = 0; i < container.length; i++) {
        const box = container[i];
        boxs_hash[i] = 0;
        for (let j = 0; j < box.length; j++) {
          const ball = box[j];
          boxs_hash[i] = (boxs_hash[i] || 0) + ball;
          ball_hash[j] = (ball_hash[j] || 0) + ball;
        }
      }
    
      const boxs_arr = Object.values(boxs_hash).sort();
      const ball_arr = Object.values(ball_hash).sort();
    
      return boxs_arr.every((value, i) => value === ball_arr[i])
        ? 'Possible'
        : 'Impossible';