← back

Derivative of a Polynomial

#116 · Calculus · Easy

⊣ Solve on deep-ml.com

Problem

Compute the derivative of a polynomial. Given a polynomial represented as a list of coefficients (from highest degree to lowest), return the coefficients of its derivative.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def derivative_of_polynomial(coefficients: list[float]) -> list[float]:
    n = len(coefficients)
    if n <= 1:
        return [0]

    degree = n - 1
    result = []
    for i, coeff in enumerate(coefficients[:-1]):
        power = degree - i
        result.append(coeff * power)

    if not result:
        return [0]
    return result

Explanation

  1. Power rule: The derivative of a * x^n is a * n * x^(n-1).
  2. Coefficient list: Coefficients are ordered from highest degree to lowest. For example, [3, 2, 1] represents 3x^2 + 2x + 1.
  3. Process: For each coefficient (except the constant term), multiply it by its power. The constant term vanishes.
  4. Result: The derivative of 3x^2 + 2x + 1 is 6x + 2, returned as [6, 2].

Complexity

  • Time: O(n) where n is the number of coefficients
  • Space: O(n) for the result list