RainX_69

PRINT LARGEST SUBMATRIX WITH SUM ZERO | HARD | OA

Mar 6th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/problems/largest-rectangular-sub-matrix-whose-sum-is-0/1?page=1&difficulty[]=1&difficulty[]=2&status[]=unsolved&category[]=Dynamic%20Programming&category[]=Binary%20Search&category[]=Trie&category[]=union-find&sortBy=submissions
  2.  
  3. Given a matrix mat[][] of size N x M. The task is to find the largest rectangular sub-matrix by area whose sum is 0.
  4.  
  5. If there are multiple solutions return the rectangle which starts from minimum column index. If you still have multiple solutions return the one starting from minimum row index. If you still have multiple solutions return the one having greatest row number. If no such matrix is present return a zero (0) size matrix.
  6.  
  7. Example 1:
  8. Input: N = 3, M = 3
  9. mat[][] =  1, 2, 3
  10.           -3,-2,-1
  11.            1, 7, 5
  12. Output:  1, 2, 3
  13.         -3,-2,-1
  14.  
  15. Example 2:
  16. Input: N = 4, M = 4
  17. mat[][] = 9, 7, 16, 5
  18.           1,-6,-7, 3
  19.           1, 8, 7, 9
  20.           7, -2, 0, 10
  21.  
  22. Output: -6,-7
  23.           8, 7
  24.          -2, 0
  25. ---------------------------------------------------------------------------------------------------------------------------------------
  26.  
  27.  
  28. Code explanation-
  29.  
  30. You may be wondering, why I am not taking into considering other tie breaking conditions. Let us understand
  31. I am iterating from r1=0 to r2=n. So, if I find an maxanswer that was seen before. I am sure that maxanswer will have minimum row index.
  32.  
  33. The reason we are using column as tie breaker is because it is what matters now. row index and row number are already taken care of. Actually if you think, YOU WILL REALISE THIS ROW NUMBER CONDITION DOES NOT EVEN MATTER HERE. So the answer now depends only on column, cuz the current answer if equal to already max answer, would fail at second condition cuz max that occurred before had minimum row index.
  34.  
  35. ---------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. class Solution{
  38.   public:
  39.   pair<int,int> getCoordinates(vector<int> &arr){
  40.       unordered_map<int,int> mpp;
  41.       int currsum=0;
  42.       int mxLength=0;
  43.       pair<int,int> res={-1,-1};
  44.       for(int i=0;i<arr.size();i++){
  45.           currsum+=arr[i];
  46.           if(currsum==0){
  47.               mxLength=i+1;
  48.               res={0,i};
  49.           }
  50.           if(mpp.find(currsum)!=mpp.end()){
  51.               if(mxLength<i-mpp[currsum]){
  52.                   mxLength=i-mpp[currsum];
  53.                   res={mpp[currsum]+1,i};
  54.               }
  55.           }
  56.           else{
  57.               mpp[currsum]=i; /* because we do not want to
  58.                                  update with greater index
  59.                                  for same sum. Doing this will
  60.                                  give us a higher probability
  61.                                  of getting larger lengths */
  62.           }
  63.       }
  64.       return res;
  65.   }
  66.  
  67.   vector<vector<int>> sumZeroMatrix(vector<vector<int>> a){
  68.       int mxSum=0;
  69.       int n=a.size();
  70.       int m=a[0].size();
  71.      
  72.       int r1=INT_MAX;
  73.       int c1=INT_MAX;
  74.       int r2=INT_MAX;
  75.       int c2=INT_MAX;
  76.      
  77.       for(int i=0;i<n;i++){
  78.           vector<int> sum(m,0);
  79.           for(int j=i;j<n;j++){
  80.               for(int k=0;k<m;k++){
  81.                   sum[k]+=a[j][k];
  82.               }
  83.               auto cood=getCoordinates(sum);
  84.              
  85.               if(cood.first==-1){  // did not get zero sum rectangle
  86.                   continue;
  87.               }
  88.              
  89.               int Tr1=i;
  90.               int Tr2=j;
  91.               int Tc1=cood.first;
  92.               int Tc2=cood.second;
  93.               int size=(Tr2-Tr1+1)*(Tc2-Tc1+1);
  94.              
  95.               if(size>mxSum || (size==mxSum && Tc1<c1)){
  96.                   mxSum=size;
  97.                   r1=Tr1;
  98.                   r2=Tr2;
  99.                   c1=Tc1;
  100.                   c2=Tc2;
  101.               }
  102.           }
  103.       }
  104.       vector<vector<int>> res;
  105.      
  106.       if(r1==INT_MAX){ // no answer found
  107.           return res;
  108.       }
  109.      
  110.       for(int i=r1;i<=r2;i++){
  111.           vector<int> temp;
  112.           for(int j=c1;j<=c2;j++){
  113.               temp.push_back(a[i][j]);
  114.           }
  115.           res.push_back(temp);
  116.       }
  117.      
  118.       return res;
  119.   }
Advertisement
Add Comment
Please, Sign In to add comment