Java Solution with Explaination


  • 0
    E
    /*
    //Divide number in the form of only 2 and 3
    if(n%3==0){//like 15
        all 3s gives the maximum_product
    }
    if(n%3==1){//like 7
        two 2s and rest 3s as we have to make the number divisible by 3 so 7-2*2 is divisible by 3
    }
    if(n%3==2){//like 17
        one 2 and rest 3s as we have to make the number divisible by 3 so 17-2*1 is divisble by 3
    }
    */
    public class Solution {
        public int integerBreak(int n) {
            if(n==2 || n==1){
                return 1;
            }
            else if(n==3){
                return 2;
            }
            if(n%3==0){
                int p=n/3;
                return (int)Math.pow(3,p);
            }
            else if(n%3==1){
                int rem=2*2;
                int p=(n-rem)/3;
                return ((int)Math.pow(3,p))*rem;
            }
            else{
                int rem=2*1;
                int p=(n-rem)/3;
                return ((int)Math.pow(3,p))*rem;
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.