Fast Fourier Transform - Problem
Implement the Fast Fourier Transform (FFT) algorithm to efficiently multiply two polynomials represented as coefficient arrays.
Given two polynomials A(x) and B(x) represented by their coefficient arrays, compute their product C(x) = A(x) * B(x) and return the coefficients of the resulting polynomial.
For polynomial A(x) = a₀ + a₁x + a₂x² + ... + aₙxⁿ, the coefficient array is [a₀, a₁, a₂, ..., aₙ].
Note: The degree of the product polynomial C(x) will be deg(A) + deg(B), so if A has degree n and B has degree m, then C will have degree n+m.
Input & Output
Example 1 — Basic Linear Polynomials
$
Input:
polyA = [1, 2], polyB = [3, 4]
›
Output:
[3, 10, 8]
💡 Note:
(1 + 2x)(3 + 4x) = 1×3 + 1×4x + 2x×3 + 2x×4x = 3 + 4x + 6x + 8x² = 3 + 10x + 8x²
Example 2 — Constant and Linear
$
Input:
polyA = [5], polyB = [2, 1]
›
Output:
[10, 5]
💡 Note:
5 × (2 + x) = 5×2 + 5×1×x = 10 + 5x
Example 3 — Quadratic Multiplication
$
Input:
polyA = [1, 0, 1], polyB = [1, 1]
›
Output:
[1, 1, 1, 1]
💡 Note:
(1 + x²)(1 + x) = 1×1 + 1×x + x²×1 + x²×x = 1 + x + x² + x³
Constraints
- 1 ≤ polyA.length, polyB.length ≤ 104
- -1000 ≤ polyA[i], polyB[i] ≤ 1000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code