Java Code
import java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\npublic class Solution {\n    public List> reconstructMatrix(int upper, int lowe
import java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\npublic class Solution {\n    public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {\n        int n = colsum.length;\n        int count1 = 0;\n        int count2 = 0;\n\n        for (int i = 0; i < n; i++) {\n            if (colsum[i] == 1) count1++;\n            if (colsum[i] == 2) count2 += 2;\n        }\n\n        if ((upper + lower) - (count1 + count2) != 0) return new ArrayList<>();\n\n        int upp = upper - count2 / 2;\n        int low = lower - count2 / 2;\n\n        if (upp < 0 || low < 0) return new ArrayList<>();\n\n        List<List<Integer>> res = new ArrayList<>();\n        res.add(new ArrayList<>());\n        res.add(new ArrayList<>());\n\n        for (int i = 0; i < n; i++) {\n            if (colsum[i] == 2) {\n                res.get(0).add(1);\n                res.get(1).add(1);\n            } else if (colsum[i] == 1) {\n                if (upp > 0) {\n                    res.get(0).add(1);\n                    res.get(1).add(0);\n                    upp--;\n                } else {\n                    res.get(0).add(0);\n                    res.get(1).add(1);\n                    low--;\n                }\n            } else {\n                res.get(0).add(0);\n                res.get(1).add(0);\n            }\n        }\n\n        return res;\n    }\n}\n
[]