- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find product of few numbers whose sum is given in Python
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 −
Example
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))
Input
12
Output
81
- Related Articles
- Program to find number of sublists whose sum is given target in python
- Find two numbers whose product is $-24$ and sum is 2.
- Find two numbers whose sum is 27 and product is 182.
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- 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
- Find two numbers whose sum and GCD are given in C++
- Find two consecutive natural numbers whose product is 20.
- Program to find maximum sum of multiplied numbers in Python
- Count of n digit numbers whose sum of digits equals to given sum in C++
- 8086 program to find sum of Even numbers in a given series
- 8086 program to find sum of odd numbers in a given series
- Program to find kpr sum for all queries for a given list of numbers in Python
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- Python Program to Find the Product of two Numbers Using Recursion

Advertisements