✅✅Java || 🚀🚀100% beats ||🔥 Simple || Explanation
Intuition\nIf a column sum is 2, both the upper and lower rows must contain a 1 in that column. If a column sum is 0, both rows will have a 0 in that column. Th
# Intuition\nIf a column sum is 2, both the upper and lower rows must contain a 1 in that column. If a column sum is 0, both rows will have a 0 in that column. Therefore, our primary focus should be on managing columns where the column sum is 1.\n\n# Approach\n1. **Initialization :** Start by initializing two arrays to represent the upper and lower rows of the matrix, both filled with zeros.\n\n2. **Handling Columns with Sum = 2 :** \n    For columns where the column sum is 2, set both the upper and lower rows to 1 in those columns. Decrease the upper and lower counts accordingly since we\'ve used up one 1 from each row.\n\n3. **Handling Columns with Sum = 1:** \n\n    - For columns where the column sum is 1, first try to place a 1 in the upper row if the remaining upper count is greater than 0. If successful, place 1 in the upper row and 0 in the lower row, then decrease the upper count.\n\n    - If the upper count is 0, try to place a 1 in the lower row if the remaining lower count is greater than 0. If successful, place 1 in the lower row and 0 in the upper row, then decrease the lower count.\n\n    - If neither the upper nor the lower rows can accommodate the 1, it means constructing the matrix is not possible, and we should return an empty list.\n\n4. **Validation :** After processing all columns, check if both the upper and lower counts are 0. If not, return an empty list as it indicates that the matrix construction failed.\n \n5. **Final answer :** Convert the upper and lower row arrays into lists and add them to the result list. Return the result list as the final reconstructed matrix.\n\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\n    public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {\n        int n = colsum.length;\n        List<List<Integer>> result = new ArrayList<>();\n        Integer upperRow[] = new Integer[n];\n        Integer lowerRow[] = new Integer[n];\n        \n        // Initialize both rows with zeros\n        Arrays.fill(upperRow, 0);\n        Arrays.fill(lowerRow, 0);\n        \n        // First pass: handle columns where colsum[i] is 2\n        for (int i = 0; i < n; i++) {\n            if (colsum[i] == 2) {\n                // Both rows must have a 1 in the i-th column\n                upperRow[i] = 1;\n                lowerRow[i] = 1;\n                upper--;\n                lower--;\n            }\n        }\n        \n        // Second pass: handle columns where colsum[i] is 1\n        for (int i = 0; i < n; i++) {\n            if (colsum[i] == 1) {\n                if (upper > 0) {\n                    // Place 1 in the upper row if possible\n                    upperRow[i] = 1;\n                    upper--;\n                } else if (lower > 0) {\n                    // Otherwise, place 1 in the lower row\n                    lowerRow[i] = 1;\n                    lower--;\n                } else {\n                    // If neither row can accommodate the 1, return an empty list\n                    return result;\n                }\n            }\n        }\n        \n        // Check if all the required 1\'s are placed correctly\n        if (upper != 0 || lower != 0) {\n            return result;\n        }\n        \n        // Convert arrays to lists and add to the result\n        result.add(Arrays.asList(upperRow));\n        result.add(Arrays.asList(lowerRow));\n        return result;\n    }\n}\n\n```
['Java']