- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a number n, we have to find two or more numbers such that their sum is equal to n, and the product of these numbers is maximized, we have to find the product.

So, if the input is like n = 12, then the output will be 81, as 3 + 3 + 3 + 3 = 12 and 3 * 3 * 3 * 3 = 81.

To solve this, we will follow these steps −

Define a function dp() . This will take n

if n is same as 0, then

return 1

ans := 0

for i in range 1 to n + 1, do

ans := maximum of ans and (i * dp(n − i))

return ans

From the main method, do the following −

return dp(n)

Let us see the following implementation to get better understanding −

class Solution: def solve(self, n): def dp(n): if n == 0: return 1 ans = 0 for i in range(1, n + 1): ans = max(ans, i * dp(n - i)) return ans return dp(n) ob1 = Solution() print(ob1.solve(12))

12

81

- Related Questions & Answers
- Program to find number of sublists whose sum is given target in python
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- Find two numbers whose sum and GCD are given in C++
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find sum of rectangle whose sum at most k in Python
- Count of n digit numbers whose sum of digits equals to given sum in C++
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- Find numbers whose sum of digits equals a value
- Python Program to Find the Product of two Numbers Using Recursion
- Python program to find product of rational numbers using reduce function
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- 8086 program to find sum of Even numbers in a given series

Advertisements