C++ Solution
Runtime: 88 ms, faster than 23.00% of C++ online submissions for Reconstruct a 2-Row Binary Matrix.\nMemory Usage: 62.6 MB, less than 13.67% of C++ online submi
Runtime: 88 ms, faster than 23.00% of C++ online submissions for Reconstruct a 2-Row Binary Matrix.\nMemory Usage: 62.6 MB, less than 13.67% of C++ online submissions for Reconstruct a 2-Row Binary Matrix.\n\n\nGiven array is Binary and so the max col value can be 2. So, we first put both in upper and lower row in ith\ncolumn if column value is 2. Then we iteratively put 1 in those columns of upper or lower row if upper sum\nand ith column is greater than 0 or lower sum and ith column is greater than 0. Then we again check the\nupper sum and lower sum and specific column sum for the valdity of the result. Any invalid case makes\nresult impossible.\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& col) {\n        \n\n        int n = col.size();\n        vector<vector<int>>res(2,vector<int>(n,0));\n        vector<int>colsum(col.begin(),col.end());\n        \n        int rowsum [2] = {upper,lower};\n        \n        // make res[0][j] && res[1][j] 1 if col[j]==2\n        for(int j=0;j<n;j++)\n        {\n           if(colsum[j]==2)\n           {\n               res[0][j] = 1;\n               res[1][j] = 1;\n               colsum[j] = 0;\n               rowsum[0] -= 1;\n               rowsum[1] -= 1;\n           }\n        }\n        \n        // make res[i][j] 1 if both rowsum[i]>0 && colsum[j]>0\n        for(int i=0;i<2;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(res[i][j]!=1)\n                {\n                    res[i][j] = (rowsum[i]>0 && colsum[j]>0) ? 1 : 0;\n                    rowsum[i] -= res[i][j];\n                    colsum[j] -= res[i][j];\n                }\n            }   \n        }\n        \n        // check validity of the result vector with given costraints\n        int t_upper = 0;\n        int t_lower = 0;\n        for(int i=0;i<n;i++)\n        {\n            t_upper+=res[0][i];\n            t_lower+=res[1][i];\n            \n            if(col[i]!=(res[0][i]+res[1][i]))return {}; // invalid\n        }\n        \n        if(t_upper != upper || t_lower!=lower)return {};    // invalid\n        \n        \n        return res;\n    }\n};\n```
['Array', 'C']