

- 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
Program to find product of few numbers whose sum is given in Python
<p>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.</p><p>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.</p><p>To solve this, we will follow these steps −</p><ul class="list"><li><p>Define a function dp() . This will take n</p></li><li><p>if n is same as 0, then</p><ul class="list"><li><p>return 1</p></li></ul></li><li><p>ans := 0</p></li><li><p>for i in range 1 to n + 1, do</p><ul class="list"><li><p>ans := maximum of ans and (i * dp(n − i))</p></li></ul></li><li><p>return ans</p></li><li><p>From the main method, do the following −</p></li><li><p>return dp(n)</p></li></ul><p>Let us see the following implementation to get better understanding −</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/SJK1XdFS" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">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))</pre><h2>Input</h2><pre class="result notranslate">12</pre><h2>Output</h2><pre class="result notranslate">81</pre>
- 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
- C++ code to find three numbers whose sum is n
- 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
- 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++
- 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 sum of k non-overlapping sublists whose sum is maximum in C++
Advertisements