Untitled

 avatar
unknown
c_cpp
a year ago
332 B
2
Indexable
bool canBeComposed(int n, int x) {
    // Base cases
    if (n == 0) return true;
    if (n < 0) return false;

    // Try to break `n` using `x`
    bool usingX = canBeComposed(n - x, x);

    // Try to break `n` using `x-1`
    bool usingXMinusOne = canBeComposed(n - x + 1, x);

    return usingX || usingXMinusOne;
}