Build the Equation - Problem

You have a very powerful program that can solve any equation of one variable in the world. Given a table Terms containing polynomial terms, you need to build a properly formatted equation.

Table: Terms

Column NameType
powerint
factorint

Requirements:

  • The equation format is: LHS = 0
  • Each term follows: <sign><fact>X^<pow>
  • <sign> is either "+" or "-"
  • <fact> is the absolute value of the factor
  • If power = 1, omit "^1" (e.g., "+3X")
  • If power = 0, omit "X" entirely (e.g., "-3")
  • Order terms by power in descending order

Table Schema

Terms
Column Name Type Description
power PK int The exponent of X in the term (0-100)
factor int The coefficient of the term (-100 to 100, non-zero)
Primary Key: power
Note: Each row represents one term in a polynomial equation. Power values are unique.

Input & Output

Example 1 — Standard Polynomial
Input Table:
power factor
2 1
1 -4
0 2
Output:
equation
+1X^2-4X+2=0
💡 Note:

The polynomial has three terms: X² (power=2, factor=1), -4X (power=1, factor=-4), and +2 (power=0, factor=2). Terms are ordered by descending power: 2, 1, 0.

Example 2 — Single Term
Input Table:
power factor
0 1
Output:
equation
+1=0
💡 Note:

A constant term with power=0 results in just the factor value without X. The equation becomes '+1=0'.

Example 3 — Negative Leading Coefficient
Input Table:
power factor
4 -5
2 3
1 1
Output:
equation
-5X^4+3X^2+1X=0
💡 Note:

Shows negative leading coefficient (-5X⁴), power=1 formatted as X (not X^1), and proper ordering by descending power.

Constraints

  • power is an integer in the range [0, 100]
  • factor is an integer in the range [-100, 100] and cannot be zero
  • power column contains unique values

Visualization

Tap to expand
Build the Equation - Polynomial Formatter INPUT Table of Terms coeff power 3 2 -5 1 2 0 4 3 Array of objects with coefficient and power [ {coeff: 3, power: 2}, {coeff: -5, power: 1}, {coeff: 2, power: 0}, {coeff: 4, power: 3} ] ALGORITHM STEPS 1 Sort by Power Descending order (high to low) 2 Handle Signs + for positive, - for negative 3 Format Powers power 0: constant only power 1: x (no exponent) 4 Build String Concatenate all terms Processing Flow: Sorted: [p3, p2, p1, p0] 4x^3 --> "4x^3" 3x^2 --> "+3x^2" -5x^1 --> "-5x" 2x^0 --> "+2" FINAL RESULT Formatted Equation: 4x^3 + 3x^2 - 5x + 2 Term Breakdown: 4x^3 power 3 + 3x^2 power 2 - 5x power 1 + 2 constant OK - Equation Built! "4x^3 + 3x^2 - 5x + 2" Key Insight: Sort terms by descending power first to ensure proper polynomial format. Handle special cases: power=0 shows only coefficient, power=1 shows "x" without exponent, first term has no leading "+". Time Complexity: O(n log n) for sorting | Space Complexity: O(n) for result string TutorialsPoint - Build the Equation | Optimal Solution
Asked in
Microsoft 28 Oracle 22
23.4K Views
Medium Frequency
~18 min Avg. Time
892 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