
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Breaking integer to maximize product in JavaScript
Problem
We are required to write a JavaScript function that takes in an Integer, num, as the first and the only argument.
Our function should break these integers into at least two chunks which when added gives the sum integer num and when multiplied gives maximum possible product. Finally, our function should return this maximum possible product.
For example, if the input to the function is −
const num = 10;
Then the output should be −
const output = 36;
Output Explanation:
Because 10 can be broken into 3 + 3 + 4 which when multiplied gives 36.
Example
The code for this will be −
const num = 10; const breakInt = (num = 2) => { const dp = new Array(num + 1).fill(0); dp[0] = 0; dp[1] = 1; for(let i = 2; i <= num; i++){ for(let j = 1; 2*j <= i; j++){ dp[i] = Math.max(dp[i], Math.max(j, dp[j]) * Math.max(i-j, dp[i-j]) ); }; }; return dp[num]; }; console.log(breakInt(num));
Output
And the output in the console will be −
36
- Related Articles
- Breaking camelCase syntax in JavaScript
- Breaking a loop in functional programming JavaScript.
- Maximize the product of four factors of a Number in C++
- Maximize first array over second in JavaScript
- How to set line breaking rules for non-CJK scripts in JavaScript?
- How important is backslash in breaking a string in JavaScript?
- Integer Range in JavaScript
- How to define integer constants in JavaScript?
- How to split sentence into blocks of fixed length without breaking words in JavaScript
- Convert integer array to string array in JavaScript?
- How to convert a Boolean to Integer in JavaScript?
- JavaScript Program for Maximize Elements Using Another Array
- Breaking Cryptography
- How to convert a string into integer in JavaScript?
- Subarrays product sum in JavaScript

Advertisements