Solve the Equation - Problem

Solve a given equation and return the value of 'x' in the form of a string "x=#value".

The equation contains only '+' and '-' operations, the variable 'x' and its coefficient.

Return cases:

  • "No solution" if there is no solution for the equation
  • "Infinite solutions" if there are infinite solutions for the equation
  • "x=#value" if there is exactly one solution (where #value is an integer)

Input & Output

Example 1 — Basic Case
$ Input: equation = "x+5-3+x=6+x-2"
Output: "x=2"
💡 Note: Left side: x + x + 5 - 3 = 2x + 2. Right side: 6 + x - 2 = x + 4. Moving terms: 2x - x = 4 - 2, so x = 2.
Example 2 — No Solution
$ Input: equation = "x=x"
Output: "Infinite solutions"
💡 Note: Both sides are identical (x = x), so any value of x satisfies the equation.
Example 3 — Contradictory
$ Input: equation = "2x=x"
Output: "x=0"
💡 Note: Moving terms: 2x - x = 0, so x = 0.

Constraints

  • The equation contains only '+', '-' operation, the variable 'x' and its coefficient
  • There is exactly one '=' in the equation
  • The equation is valid and follows the format described

Visualization

Tap to expand
Solve the Equation: x+5-3+x=6+x-2Input: x+5-3+x=6+x-2Linear equation with variable xParse & Collect TermsLeft: 2x+2, Right: x+4Rearrange: 2x - x = 4 - 2Output: x=2Single solution found
Understanding the Visualization
1
Input
Linear equation like x+5-3+x=6+x-2
2
Parse
Extract coefficients and constants from both sides
3
Output
x=value, No solution, or Infinite solutions
Key Takeaway
🎯 Key Insight: Transform the equation into ax = b form by collecting like terms, then analyze the coefficients to determine if there's one solution, no solution, or infinite solutions.
Asked in
Google 15 Amazon 12
23.4K 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