Open In App

Recursively break a number in 3 parts to get maximum sum

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, we can divide it in only three parts n/2, n/3 and n/4 (we will consider only integer part). The task is to find the maximum sum we can make by dividing number in three parts recursively and summing up them together.
Examples: 
 

Input : n = 12
Output : 13
// We break n = 12 in three parts {12/2, 12/3, 12/4} 
// = {6, 4, 3},  now current sum is = (6 + 4 + 3) = 13 
// again we break 6 = {6/2, 6/3, 6/4} = {3, 2, 1} = 3 + 
// 2 + 1 = 6 and further breaking 3, 2 and 1 we get maximum
// summation as 1, so breaking 6 in three parts produces
// maximum sum 6 only similarly breaking 4 in three   
// parts we can get maximum sum 4 and same for 3 also.
// Thus maximum sum by breaking number in parts  is=13

Input : n = 24
Output : 27
// We break n = 24 in three parts {24/2, 24/3, 24/4} 
// = {12, 8, 6},  now current sum is = (12 + 8 + 6) = 26
// As seen in example, recursively breaking 12 would
// produce value 13. So our maximum sum is 13 + 8 + 6 = 27.
// Note that recursively breaking 8 and 6 doesn't produce
// more values, that is why they are not broken further.

Input : n = 23
Output : 23
// we break n = 23 in three parts {23/2, 23/3, 23/4} = 
// {10, 7, 5}, now current sum is = (10 + 7 + 5) = 22. 
// Since  after further breaking we can not get maximum
// sum hence number is itself maximum i.e; answer is 23

 

Recommended Practice

A simple solution for this problem is to do it recursively. In each call we have to check only max((max(n/2) + max(n/3) + max(n/4)), n) and return it. Because either we can get maximum sum by breaking number in parts or number is itself maximum. Below is the implementation of recursive algorithm. 
 

C++




// A simple recursive C++ program to find
// maximum sum by recursively breaking a
// number in 3 parts.
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the maximum sum
int breakSum(int n)
{
    // base conditions
    if (n==0 || n == 1)
        return n;
  
    // recursively break the number and return
    // what maximum you can get
    return max((breakSum(n/2) + breakSum(n/3) +
                breakSum(n/4)),  n);
}
  
// Driver program to run the case
int main()
{
    int n = 120;
    cout << breakSum(n);
    return 0;
}


Java




// A simple recursive JAVA program to find
// maximum sum by recursively breaking a
// number in 3 parts.
import java.io.*;
  
class GFG {
     
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        // base conditions
        if (n==0 || n == 1)
            return n;
   
        // recursively break the number and return
        // what maximum you can get
        return Math.max((breakSum(n/2) + breakSum(n/3) +
                    breakSum(n/4)),  n);    
    }
          
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 12;
        System.out.println(breakSum(n));
    }
}
// This code is contributed by Amit Kumar


Python3




# A simple recursive Python3 program to 
# find maximum sum by recursively breaking  
# a number in 3 parts. 
  
# Function to find the maximum sum 
def breakSum(n) :
  
    # base conditions 
    if (n == 0 or n == 1) :
        return
  
    # recursively break the number and 
    # return what maximum you can get 
    return max((breakSum(n // 2) + 
                breakSum(n // 3) +
                breakSum(n // 4)), n) 
  
# Driver Code
if __name__ == "__main__"
  
    n = 12
    print(breakSum(n))
  
# This code is contributed by Ryuga


C#




// A simple recursive C# program to find
// maximum sum by recursively breaking a
// number in 3 parts.
using System;
  
class GFG {
      
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        // base conditions
        if (n==0 || n == 1)
            return n;
  
        // recursively break the number 
        // and return what maximum you
        // can get
        return Math.Max((breakSum(n/2)
                       + breakSum(n/3) 
                 + breakSum(n/4)), n); 
    }
          
    // Driver program to test the 
    // above function
    public static void Main () 
    {
        int n = 12;
          
        Console.WriteLine(breakSum(n));
    }
}
  
// This code is contributed by anuj_67.


PHP




<?php 
// A simple recursive PHP program to find
// maximum sum by recursively breaking a
// number in 3 parts.
  
// Function to find the maximum sum
function breakSum($n)
{
    // base conditions
    if ($n == 0 || $n == 1)
        return $n;
  
    // recursively break the number and 
    // return what maximum you can get
    return max((breakSum(intval($n / 2)) + 
                breakSum(intval($n / 3)) +
                breakSum(intval($n / 4))), $n);
}
  
// Driver program to run the case
$n = 12;
echo breakSum($n);
  
// This code is contributed by ita_c
?>


Javascript




<script>
  
// A simple recursive Javascript program to find
// maximum sum by recursively breaking a
// number in 3 parts.
      
    // Function to find the maximum sum
    function breakSum(n)
    {
        // base conditions
        if (n==0 || n == 1)
            return n;
     
        // recursively break the number and return
        // what maximum you can get
        return Math.max((breakSum(Math.floor(n/2)) + 
                    breakSum(Math.floor(n/3)) +
                    breakSum(Math.floor(n/4))),  n);  
    }
      
    // Driver program to test the above function
      
    let n = 12;
    document.write(breakSum(n));
      
    // This code is contributed by avanitrachhadiya2155
      
</script>


Output

144

Memoization of Recursive Solution

In the above solution, we see that there are many overlapping functions calls become so to reduce that we memoize the above recursive solution so it’s time complexity reduce exponentially to linear.

C++




// memoization C++ program to find maximum sum by recursively breaking a number in 3 parts.
  
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the maximum sum
int breakSum(int n, vector<int>&dp)
{
    // base conditions
    if (n == 0 || n == 1) {
        return n;
    }
  
    // if dp[n] already calculated then directly return so no overlapping problem calls
    if (dp[n] != -1) {
        return dp[n];
    }
  
    // break the number and return what maximum you can get
    return dp[n] = max((breakSum(n / 2, dp) + breakSum(n / 3, dp) + breakSum(n / 4, dp)),  n);
}
  
// Driver program to run the case
int main() {
    int n = 24;
  
    // dp array todo memoization
    vector<int>dp(n + 1, -1);
  
    cout << breakSum(n, dp);
    return 0;
}


Java




// memoization Java program to find maximum sum by recursively breaking a number in 3 parts.
  
class Main {
  
    // Function to find the maximum sum
    public static int breakSum(int n, int[] dp)
    {
        // base conditions
        if (n == 0 || n == 1)
            return n;
  
        // if dp[n] already calculated then directly return so no overlapping problem calls
        if (dp[n] != -1) {
            return dp[n];
        }
  
        // break the number and return what maximum you can get
        return dp[n] = Math.max((breakSum(n / 2, dp) + breakSum(n / 3, dp) + breakSum(n / 4, dp)),  n);
  
    }
  
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 24;
        int[] dp = new int[n+1];
        for (int i = 0; i <= n; i++) dp[i] = -1;
        System.out.println(breakSum(n, dp));
    }
}
  
// This code is contributed by ajaymakvana.


Python3




# Python3 code to find maximum sum by recursively breaking a number in 3 parts.
  
# Function to find the maximum sum
def breakSum(n, dp):
    
    # base conditions
    if n == 0 or n == 1:
        return n
        
    # if dp[n] already calculated then directly return so no overlapping problem calls
    if dp[n] != -1:
        return dp[n]
        
    # break the number and return what maximum you can get
    dp[n] = max((breakSum(n // 2, dp) + breakSum(n // 3, dp) + breakSum(n // 4, dp)), n)
    return dp[n]
  
# Driver program to run the case
if __name__ == "__main__":
    n = 24
      
    # dp array todo memoization
    dp = [-1 for i in range(n + 1)]
    print(breakSum(n, dp))
  
    # This code is contributed by lokehpotta20.


C#




// memoization C# program to find maximum sum by 
// recursively breaking a number in 3 parts.
using System;
using System.Collections.Generic;
  
class GFG {
  
  // Function to find the maximum sum
  static int breakSum(int n, List<int> dp)
  {
    // base conditions
    if (n == 0 || n == 1) {
      return n;
    }
  
    // if dp[n] already calculated then directly 
    // return so no overlapping problem calls
    if (dp[n] != -1) {
      return dp[n];
    }
  
    // break the number and return what maximum you can get
    return dp[n] = Math.Max((breakSum(n / 2, dp) + 
                             breakSum(n / 3, dp) + 
                             breakSum(n / 4, dp)),  n);
  }
  
  // Driver program to run the case
  static public void Main()
  {
    int n = 24;
  
    // dp array todo memoization
    List<int>dp = new List<int>();
    for(int i = 0; i < n + 1; i++)
      dp.Add(-1);
  
    Console.Write(breakSum(n, dp));
  }
}
  
// This code is contributed by ratiagrawal.


Javascript




// memoization Javascript program to 
// find maximum sum by recursively breaking a number in 3 parts.
  
// Function to find the maximum sum
function breakSum(n, dp)
{
    // base conditions
    if (n == 0 || n == 1) {
        return n;
    }
  
    // if dp[n] already calculated then directly return 
    // so no overlapping problem calls
    if (dp[n] != -1) {
        return dp[n];
    }
  
    // break the number and return what maximum you can get
    return dp[n] = Math.max((breakSum(parseInt(n / 2), dp) + 
           breakSum(parseInt(n / 3), dp) + breakSum(parseInt(n / 4), dp)),  n);
  
}
  
// Driver program to run the case
let n = 24;
  
// dp array todo memoization
let dp=new Array(n + 1).fill(-1);
  
console.log(breakSum(n, dp));


Output

27

Time Complexity: O(n) 
Auxiliary Space: O(n) (as we are creating dp array of size n+1)

An efficient solution for this problem is to use Dynamic programming because while breaking the number in parts recursively we have to perform some overlapping problems. For example part of n = 30 will be {15,10,7} and part of 15 will be {7,5,3} so we have to repeat the process for 7 two times for further breaking. To avoid this overlapping problem we are using Dynamic programming. We store values in an array and if for any number in recursive calls we have already solution for that number currently so we directly extract it from array. 
 

C++




// A Dynamic programming based C++ program
// to find maximum sum by recursively breaking
// a number in 3 parts.
#include<bits/stdc++.h>
#define MAX 1000000
using namespace std;
  
int breakSum(int n)
{
    int dp[n+1];
  
    // base conditions
    dp[0] = 0, dp[1] = 1;
  
    // Fill in bottom-up manner using recursive
    // formula.
    for (int i=2; i<=n; i++)
       dp[i] = max(dp[i/2] + dp[i/3] + dp[i/4], i);
  
    return dp[n];
}
  
// Driver program to run the case
int main()
{
    int n = 24;
    cout << breakSum(n);
    return 0;
}


Java




// A simple recursive JAVA program to find
// maximum sum by recursively breaking a
// number in 3 parts.
import java.io.*;
  
class GFG {
  
    final int MAX = 1000000;
      
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        int dp[] = new int[n+1];
   
        // base conditions
        dp[0] = 0;  dp[1] = 1;
       
        // Fill in bottom-up manner using recursive
        // formula.
        for (int i=2; i<=n; i++)
           dp[i] = Math.max(dp[i/2] + dp[i/3] + dp[i/4], i);
       
        return dp[n]; 
    }
          
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 24;
        System.out.println(breakSum(n));
    }
}
// This code is contributed by Amit Kumar


Python3




# A Dynamic programming based Python program
# to find maximum sum by recursively breaking
# a number in 3 parts.
  
MAX = 1000000
  
def breakSum(n):
  
    dp = [0]*(n+1)
  
    # base conditions
    dp[0] = 0
    dp[1] = 1
  
    # Fill in bottom-up manner using recursive
    # formula.
    for i in range(2, n+1):
        dp[i] = max(dp[int(i/2)] + dp[int(i/3)] + dp[int(i/4)], i);
  
    return dp[n]
  
  
# Driver program to run the case
n = 24
print(breakSum(n))
  
# This code is contributed by
# Smitha Dinesh Semwal


C#




// A simple recursive C# program to find
// maximum sum by recursively breaking a
// number in 3 parts.
using System;
  
class GFG {
      
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        int []dp = new int[n+1];
  
        // base conditions
        dp[0] = 0; dp[1] = 1;
      
        // Fill in bottom-up manner
        // using recursive formula.
        for (int i = 2; i <= n; i++)
            dp[i] = Math.Max(dp[i/2] + 
                  dp[i/3] + dp[i/4], i);
      
        return dp[n]; 
    }
          
    // Driver program to test the
    // above function
    public static void Main () 
    {
        int n = 24;
        Console.WriteLine(breakSum(n));
    }
}
  
// This code is contributed by anuj_67.


PHP




<?php
// A Dynamic programming based PHP program
// to find maximum sum by recursively breaking
// a number in 3 parts.
function breakSum($n)
{
    $dp = array_fill(0, $n + 1, 0);
  
    // base conditions
    $dp[0] = 0;
    $dp[1] = 1;
  
    // Fill in bottom-up manner using 
    // recursive formula.
    for ($i = 2; $i <= $n; $i++)
    $dp[$i] = max($dp[(int)($i / 2)] + 
                  $dp[(int)($i / 3)] + 
                  $dp[(int)($i / 4)], $i);
  
    return $dp[$n];
}
  
// Driver Code
$n = 24;
echo breakSum($n);
  
// This code is contributed by mits
?>


Javascript




<script>
// A simple recursive JAVASCRIPT program to find
// maximum sum by recursively breaking a
// number in 3 parts.
      
    let MAX = 1000000;
    // Function to find the maximum sum
    function breakSum(n)
    {
        let dp=new Array(n+1);
        for(let i=0;i<dp;i++)
        {
            dp[i]=0;
        }
        // base conditions
        dp[0] = 0;  dp[1] = 1;
        
        // Fill in bottom-up manner using recursive
        // formula.
        for (let i=2; i<=n; i++)
           dp[i] = Math.max(dp[(Math.floor(i/2))] + 
           dp[(Math.floor(i/3))] + dp[(Math.floor(i/4))], i);
        
        return dp[n];
    }
      
    // Driver program to test the above function
    let n = 24;
    document.write(breakSum(n));
      
    // This code is contributed by rag2127
      
</script>


Output

27

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



Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads