Organizing Containers of Balls

  • + 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");}
                
                
                
                
                
                
                
                
                
                
                
                
                
            }
        }
    }