• + 0 comments

    some one may get the same issue. * First Code's logic is correct but it fails for some test cases, becuase of total cost is computed on all intgers (b, bc, w, wc, z), not on the long data types.

    long taumBday(int b, int w, int bc, int wc, int z) {
        // Check if converting black to white or white to black is cheaper
        if (bc > wc + z) {
            return (b * (wc + z)) + (w * wc);
        } else if (wc > bc + z) {
            return (b * bc) + (w * (bc + z));
        } else {
            // If neither conversion is cheaper, buy gifts at original prices
            return (b * bc) + (w * wc);
        }
    }
    
    ***Correct Solution:** *
    long taumBday(int b, int w, int bc, int wc, int z) {
        // Check if converting black to white or white to black is cheaper
        if (bc > wc + z) {
            return (b * (wc + z)) + (w * wc);
        } else if (wc > bc + z) {
            return (b * bc) + (w * (bc + z));
        } else {
            // If neither conversion is cheaper, buy gifts at original prices
            return (b * bc) + (w * wc);
        }
    }
    
    // see the most optimized code in the below solution, written by @alban_tyrex