Problem from interview not working for unknown cases

I’ve came across on an interview with this problem: https://www.chegg.com/homework-help/questions-and-answers/board-2-rows-n-columns-represented-matrix-m-rows-numbered-0-1-top-bottom-columns-numbered--q38897583

I developed a solution:

 public static boolean esPosible(int U, int L, int[] c) {
    for (int i = 0; i < c.length; i++) {
        if (c[i] == 2) {
            L--;
            U--;
        }
    }
    if (U < 0 || L < 0) {
        return false;
    }
    for (int i = 0; i < c.length; i++) {
        if (c[i] == 1) {
            if (U> 0) {
                U--;
            } else {
                L--;
            }
        }
    }
    return U == 0 && L == 0;
}

public String solution(int U, int L, int[] c) {
    boolean posible = esPosible(U, L, c);
    String res = "IMPOSSIBLE";
    if (posible) {
        String res1 = "", res2 = "";
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 0) {
                res1 += "0";
                res2 += "0";
            }
            if (c[i] == 2) {
                U--; L--;
                res1 += "1";
                res2 += "1";
            }
            if (c[i] == 1) {
                if (U > 0) {
                    U--;
                    res1+="1";
                    res2+="0";
                } else {
                    L--;
                    res1+="0";
                    res2+="1";
                }
            }
        }
        res = res1 + "," + res2;
    }
    
return res;
}

But I got 70% of success cases.
Is something wrong? I really dont know what is wrong, it seems pretty greedy for me

Many thanks

I think there is some problem in esPosible(U, L, c),
because everything else seems fine.
1 Like

Hello meet2mky, thank you for your answer. I updated the code, “esPosible” function was missing.
What do you think now? Many thanks