Maximum Sum Problem | Practice | GeeksforGeeks

A number n can be broken into three parts n/2, n/3 and n/4 (consider only integer part). Each number obtained in this process can be divided further recursively. Find the maximum sum that can be obtained by summing up the divided parts together.
Note: The maximum sum may be obtained without dividing n also.
 
Example 1:
Input:
n = 12
Output: 13
Explanation: Break n = 12 in three parts
{12/2, 12/3, 12/4} = {6, 4, 3}, now current
sum is = (6 + 4 + 3) = 13. Further breaking 6,
4 and 3 into parts will produce sum less than
or equal to 6, 4 and 3 respectively.


â€‹Example 2:
Input:
n = 24
Output: 27
Explanation: Break n = 24 in three parts
{24/2, 24/3, 24/4} = {12, 8, 6}, now current
sum is = (12 + 8 + 6) = 26 . But recursively
breaking 12 would produce value 13.
So our maximum sum is 13 + 8 + 6 = 27.

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which accepts an integer n and returns the maximum sum.

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

Constraints:
0 <= n <= 106
0
baibhavrajputt2 months ago
Java memoization solution
class Solution
{
    public int maxSum(int n) 
    { 
        //code here.
        int dp[] = new int[n+1];
        for(int i=0 ; i<=n ; i++)
            dp[i] = -1;
            
        return soln(n , dp);
    }
    public int soln(int i , int dp[]){
        
        if(i==0)
            return 0;
            
        if(dp[i] != -1)
            return dp[i];
        
        int by2 = soln(i/2 , dp);
        int by3 = soln(i/3 , dp);
        int by4 = soln(i/4 , dp);
            
        return dp[i] = Math.max(i , by2+by3+by4);
            
    }
}
0
raunakmishra12433 months ago
int maxSum(int n)
        {
            
           int dp[n+1];
           dp[0]=0;
           dp[1]=1;
           for(int i=2;i<=n;i++)
           {
               
               dp[i]=max(i,dp[i/2]+dp[i/3]+dp[i/4]);
               
               
           }
           return dp[n];
        }
};
+2
dhairyasavaner954 months ago
public int maxSum(int n) 
    { 
        if(n == 0) return 0;
        int x = maxSum(n/2) + maxSum(n/3) + maxSum(n/4);
        if( n <= x) return x; 
        else return n;
    }
+1
chessnoobdj4 months ago
C++ dp tabulation
int maxSum(int n)
{
	int dp[n+1] = {0};
    for(int i=1; i<=n; i++)
    	dp[i] = max(i, dp[i/2]+dp[i/3]+dp[i/4]);
    return dp[n];
}
+1
gauravtiwari30014 months ago
Simple Recursive approach without using Dynamic Programming:
 
 int maxSum(int n)
       {
           //code here.
           if(n<=1){return n;}
           int path1 = maxSum(n/2);
           int path2 = maxSum(n/3);
           int path3 = maxSum(n/4);
           return max(path1+ path2+ path3, n);
       }
0
aakashmehta8805 months ago
int maxSum(int n)
        {
            vector<int> dp(n+1,0);
            dp[0]=0,dp[1]=1;
            
            for(int i=2;i<dp.size();i++)
                dp[i]=max(i,dp[i/2]+dp[i/3]+dp[i/4]);
            
            return dp[n];
        }
-1
aakashmehta8805 months ago
int fun(int n,vector<int> &dp){
        if(n==0 or n==1) return n;
        if(dp[n]!=-1) return dp[n];
        int x=fun(n/2,dp);
        int y=fun(n/3,dp);
        int z=fun(n/4,dp);
        dp[n]=max(n,x+y+z);
        return dp[n];
    }
        int maxSum(int n)
        {
            vector<int> dp(n+1,-1);
            return fun(n,dp);
        }
 memoization
0
omkarsubhasishkhuntia7 months ago
int maxSum(int n)       {           //code here.           int dp[n];           memset(dp,0,sizeof(dp));           dp[1]=1;           dp[2]=1;           int c=0;           for(int i=3;i<=n;i++){               dp[i]=max(i/3,dp[i/3])+max(i/2,dp[i/2])+max(i/4,dp[i/4]);               c=max(c,dp[i]);           }           return c;       }
+1
bhaskarkumarpro7 months ago
 int maxSum(int n)       {           //code here.           if(n/2 + n/3 + n/4 > n)               return maxSum(n/2)+maxSum(n/3)+maxSum(n/4);           else            return n;       }
0
mzmlrafiqcse7 months ago
java
 public int maxSum(int n) { 
        int dp[] = new int[n+1];
        dp[0] = 0;
        if(n > 1) dp[1] = 1;
        for (int i=2; i<=n; i++)
           dp[i] = Math.max(dp[i/2] + dp[i/3] + dp[i/4], i);
        return dp[n];
 }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.