Build the Equation - Problem

You're tasked with creating a polynomial equation formatter that takes mathematical terms and builds a properly formatted equation for a powerful equation solver.

Given a table of polynomial terms, each with a power and factor, you need to construct an equation in the specific format: LHS = 0

Formatting Rules:

  • Sign: '+' or '-' based on factor's sign
  • Factor: Use absolute value of the factor
  • Power formatting:
    • Power = 0: Just the factor (e.g., "+3", "-7")
    • Power = 1: Factor + X (e.g., "+3X", "-5X")
    • Power > 1: Factor + X^power (e.g., "+2X^3", "-4X^7")
  • Order: Terms must be sorted by power in descending order

Example: Terms with powers [2,0,1] and factors [1,-3,4] should produce: +1X^2+4X-3=0

Input & Output

example_1.sql β€” Basic Polynomial
$ Input: Terms table: +-------+--------+ | power | factor | +-------+--------+ | 2 | 1 | | 1 | -4 | | 0 | 2 | +-------+--------+
β€Ί Output: +1X^2-4X+2=0
πŸ’‘ Note: Terms sorted by power descending: 2,1,0. Formatted as: +1X^2 (power>1), -4X (power=1), +2 (power=0)
example_2.sql β€” Single Term
$ Input: Terms table: +-------+--------+ | power | factor | +-------+--------+ | 0 | -3 | +-------+--------+
β€Ί Output: -3=0
πŸ’‘ Note: Single term with power=0, so format is just the signed factor: -3
example_3.sql β€” Linear Equation
$ Input: Terms table: +-------+--------+ | power | factor | +-------+--------+ | 1 | 5 | | 0 | -2 | +-------+--------+
β€Ί Output: +5X-2=0
πŸ’‘ Note: Linear equation with power=1 term (+5X) and constant term (-2)

Visualization

Tap to expand
Polynomial Equation BuilderInput TermsPower: 2, Factor: 1Power: 0, Factor: -3Power: 1, Factor: 4Sort by PowerPower: 2, Factor: 1Power: 1, Factor: 4Power: 0, Factor: -3Format Terms+1X^2+4X-3Formatting Rulesβ€’ Power = 0: Β±factor (e.g., +2, -3)β€’ Power = 1: Β±factorX (e.g., +4X, -5X)β€’ Power > 1: Β±factorX^power (e.g., +1X^2, -2X^3)Final Result+1X^2+4X-3=0
Understanding the Visualization
1
Gather Terms
Collect all polynomial terms with their powers and factors
2
Sort by Power
Arrange terms in descending order of power (highest first)
3
Format Terms
Apply specific formatting rules based on power value
4
Build Equation
Concatenate formatted terms and add '=0' to complete equation
Key Takeaway
🎯 Key Insight: Use SQL's GROUP_CONCAT with ORDER BY to sort terms by power descending, then apply CASE statements to format each term correctly based on its power value.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

Sorting the terms by power dominates the time complexity

n
2n
⚑ Linearithmic
Space Complexity
O(n)

Space needed to store all terms and build the result string

n
2n
⚑ Linearithmic Space

Constraints

  • power is an integer in the range [0, 100]
  • factor is an integer in the range [-100, 100] and cannot be zero
  • Each power value is unique in the table
  • The table contains at least 1 row
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 18
28.5K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen