We are given a number n, we need to find the maximum sum possible with the help of following function:
F(n) = max( (F(n/2) + F(n/3) + F(n/4) + F(n/5)), n). To calculate F(n, ) we may either have n as our result or we can further break n into four part as in given function definition. This can be done as much time as we can. Find the maximum possible sum you can get from a given N. Note : 1 can not be break further so F(1) = 1 as a base case.
Input : n = 10 Output : MaxSum = 12 Explanation: f(10) = f(10/2) + f(10/3) + f(10/4) + f(10/5) = f(5) + f(3) + f(2) + f(2) = 12 5, 3 and 2 cannot be further divided. Input : n = 2 Output : MaxSum = 2
Approach : This problem can be solve with recursive approach but that will cost us a high complexity because of its overlapping sub problems. So we apply dynamic programming concept to solve this question in bottom up manner as:
MaxSum = 106
- Size of The Subarray With Maximum Sum
- Maximize the sum of selected numbers from an array to make it empty
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Longest alternating (positive and negative) subarray starting at every index
- Maximum difference of zeros and ones in binary string
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- C++ Data Types
- Maximum path sum in a triangle.
- Maximum path sum for each position with jumps under divisibility condition
- Maximum sum of pairs with specific difference
- Recursively break a number in 3 parts to get maximum sum
- Maximum weight path ending at any element of last row in a matrix
- Min Cost Path | DP-6
- Edit Distance | DP-5
- Maximum size square sub-matrix with all 1s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t