KatInterview

343. Integer Break

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method 1: Dynamic Programming

This problem has optimal substructure and overlapping subproblems, making it a good candidate for dynamic programming.

Solution

Integer Break in Python

class Solution:
    def integerBreak(self, n: int) -> int:
        # If n is 2 or 3, the results are 1 and 2 respectively.
        # This is because we must break n into at least two integers.
        # For n=2: 1+1, product = 1
        # For n=3: 1+2, product = 2
        # The formula n-1 holds for both cases.
        if n <= 3:
            return n - 1

        # The strategy is to break n into as many 3s as possible.
        # We analyze the remainder of n when divided by 3.
        remainder = n % 3

        if remainder == 0:
            # If n is a multiple of 3 (e.g., n=6 -> 3+3, n=9 -> 3+3+3), 
            # the maximum product is 3**(n/3).
            return 3 ** (n // 3)
        elif remainder == 1:
            # If n = 3k + 1 (e.g., n=4 -> 3+1, n=10 -> 3+3+3+1).
            # It's better to break 3+1 into 2+2=4.
            # So, we break n into (k-1) threes and one 4.
            # e.g., for n=10, we have 3+3+4. Product = 3**2 * 4.
            # The number of threes is (n - 4) // 3.
            return 3 ** ((n - 4) // 3) * 4
        else:  # remainder == 2
            # If n = 3k + 2 (e.g., n=5 -> 3+2, n=8 -> 3+3+2).
            # We break n into k threes and one 2.
            # The number of threes is n // 3.
            return 3 ** (n // 3) * 2

Integer Break in Java

class Solution {
    public int integerBreak(int n) {
        // If n is 2 or 3, the results are 1 and 2 respectively.
        // This is because we must break n into at least two integers.
        // For n=2: 1+1, product = 1
        // For n=3: 1+2, product = 2
        // The formula n-1 holds for both cases.
        if (n <= 3) {
            return n - 1;
        }

        // The strategy is to break n into as many 3s as possible.
        // We analyze the remainder of n when divided by 3.
        int remainder = n % 3;

        if (remainder == 0) {
            // If n is a multiple of 3 (e.g., n=6 -> 3+3, n=9 -> 3+3+3), 
            // the maximum product is 3^(n/3).
            return (int) Math.pow(3, n / 3);
        } else if (remainder == 1) {
            // If n = 3k + 1 (e.g., n=4 -> 3+1, n=10 -> 3+3+3+1).
            // It's better to break 3+1 into 2+2=4.
            // So, we break n into (k-1) threes and one 4.
            // e.g., for n=10, we have 3+3+4. Product = 3^2 * 4.
            // The number of threes is (n - 4) / 3.
            return (int) Math.pow(3, (n - 4) / 3) * 4;
        } else { // remainder == 2
            // If n = 3k + 2 (e.g., n=5 -> 3+2, n=8 -> 3+3+2).
            // We break n into k threes and one 2.
            // The number of threes is n / 3.
            return (int) Math.pow(3, n / 3) * 2;
        }
    }
}

Integer Break in C++

class Solution {
public:
    int integerBreak(int n) {
        // If n is 2 or 3, the results are 1 and 2 respectively.
        // This is because we must break n into at least two integers.
        // For n=2: 1+1, product = 1
        // For n=3: 1+2, product = 2
        // The formula n-1 holds for both cases.
        if (n <= 3) {
            return n - 1;
        }

        // The strategy is to break n into as many 3s as possible.
        // We analyze the remainder of n when divided by 3.
        int remainder = n % 3;

        if (remainder == 0) {
            // If n is a multiple of 3 (e.g., n=6 -> 3+3, n=9 -> 3+3+3), 
            // the maximum product is 3^(n/3).
            return pow(3, n / 3);
        } else if (remainder == 1) {
            // If n = 3k + 1 (e.g., n=4 -> 3+1, n=10 -> 3+3+3+1).
            // It's better to break 3+1 into 2+2=4.
            // So, we break n into (k-1) threes and one 4.
            // e.g., for n=10, we have 3+3+4. Product = 3^2 * 4.
            // The number of threes is (n - 4) / 3.
            return pow(3, (n - 4) / 3) * 4;
        } else { // remainder == 2
            // If n = 3k + 2 (e.g., n=5 -> 3+2, n=8 -> 3+3+2).
            // We break n into k threes and one 2.
            // The number of threes is n / 3.
            return pow(3, n / 3) * 2;
        }
    }
};

Integer Break in TypeScript

function integerBreak(n: number): number {
    // If n is 2 or 3, the results are 1 and 2 respectively.
    // This is because we must break n into at least two integers.
    // For n=2: 1+1, product = 1
    // For n=3: 1+2, product = 2
    // The formula n-1 holds for both cases.
    if (n <= 3) {
        return n - 1;
    }

    // The strategy is to break n into as many 3s as possible.
    // We analyze the remainder of n when divided by 3.
    const remainder = n % 3;

    if (remainder === 0) {
        // If n is a multiple of 3 (e.g., n=6 -> 3+3, n=9 -> 3+3+3), 
        // the maximum product is 3^(n/3).
        return Math.pow(3, n / 3);
    } else if (remainder === 1) {
        // If n = 3k + 1 (e.g., n=4 -> 3+1, n=10 -> 3+3+3+1).
        // It's better to break 3+1 into 2+2=4.
        // So, we break n into (k-1) threes and one 4.
        // e.g., for n=10, we have 3+3+4. Product = 3^2 * 4.
        // The number of threes is (n - 4) / 3.
        return Math.pow(3, Math.floor((n - 4) / 3)) * 4;
    } else { // remainder === 2
        // If n = 3k + 2 (e.g., n=5 -> 3+2, n=8 -> 3+3+2).
        // We break n into k threes and one 2.
        // The number of threes is n / 3.
        return Math.pow(3, Math.floor(n / 3)) * 2;
    }
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.