Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Breaking integer to maximize product in JavaScript
In JavaScript, finding the maximum product by breaking an integer into parts is a classic dynamic programming problem. We need to split a number into at least two chunks that sum to the original number while maximizing their product.
Problem Statement
Given an integer num, break it into at least two positive integers whose sum equals num and maximize the product of these integers.
For example, if num = 10, we can break it into 3 + 3 + 4 = 10, and the product 3 × 3 × 4 = 36 is maximum possible.
Algorithm Explanation
We use dynamic programming where dp[i] represents the maximum product obtainable by breaking integer i. For each number, we try all possible splits and choose the one giving maximum product.
Example Implementation
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];
};
// Test with different values
console.log(breakInt(10)); // Breaking 10
console.log(breakInt(8)); // Breaking 8
console.log(breakInt(6)); // Breaking 6
36 18 9
How It Works
The algorithm considers each possible way to split number i into two parts: j and i-j. For each part, we take the maximum between the number itself or its optimal breakdown (dp[j]).
Step-by-Step Breakdown
const breakIntWithSteps = (num) => {
const dp = new Array(num + 1).fill(0);
dp[0] = 0;
dp[1] = 1;
console.log(`Building dp array for num = ${num}`);
for(let i = 2; i <= num; i++){
console.log(`\nCalculating dp[${i}]:`);
for(let j = 1; 2*j <= i; j++){
let leftPart = Math.max(j, dp[j]);
let rightPart = Math.max(i-j, dp[i-j]);
let product = leftPart * rightPart;
console.log(` Split ${i} = ${j} + ${i-j}, product = ${leftPart} × ${rightPart} = ${product}`);
dp[i] = Math.max(dp[i], product);
}
console.log(` dp[${i}] = ${dp[i]}`);
}
return dp[num];
};
breakIntWithSteps(6);
Building dp array for num = 6 Calculating dp[2]: Split 2 = 1 + 1, product = 1 × 1 = 1 dp[2] = 1 Calculating dp[3]: Split 3 = 1 + 2, product = 1 × 2 = 2 dp[3] = 2 Calculating dp[4]: Split 4 = 1 + 3, product = 1 × 2 = 2 Split 4 = 2 + 2, product = 2 × 2 = 4 dp[4] = 4 Calculating dp[5]: Split 5 = 1 + 4, product = 1 × 4 = 4 Split 5 = 2 + 3, product = 2 × 2 = 4 dp[5] = 4 Calculating dp[6]: Split 6 = 1 + 5, product = 1 × 4 = 4 Split 6 = 2 + 4, product = 2 × 4 = 8 Split 6 = 3 + 3, product = 2 × 2 = 4 dp[6] = 9
Time and Space Complexity
Time Complexity: O(n²) where n is the input number
Space Complexity: O(n) for the dp array
Conclusion
This dynamic programming approach efficiently finds the maximum product by breaking an integer into optimal parts. The key insight is that for larger numbers, breaking into smaller pieces (especially 3s and 2s) typically yields better results than keeping large chunks.
