Solve the Linear Equation of Single Variable
Last Updated :
01 Aug, 2022
Given a linear equation, task is to find the value of variable used. The equation contains only ‘+’, ‘-‘ operation, the variable and its coefficient.
- If there is no solution for the equation, return “No solution”.
- If there are infinite solutions for the equation, return “Infinite solutions”.
- If there is exactly one solution for the equation, ensure that the value of x is an integer.
Examples :
Input : "x + 5 - 3 + x = 6 + x - 2"
Output : "x = 2"
Input : "x = x"
Output : "Infinite solutions"
Input: "2x = x"
Output: "x = 0"
Input: "x = x + 2"
Output: "No solution"
Approach : The idea is to use two pointers to update two parameters: the coefficient of variable used and the total sum. On the left and right side of ‘=’, use opposite signs for each numbers which is taken care of by a sign variable, which will flip once ‘=’ is seen. Now, in case of a unique solution, the ratio of the effective total and coefficient gives the required result. In case of infinite solutions, both the effective total and coefficient turns out to be zero e.g. x + 1 = x + 1. In case of no solution, the coefficient of x turns out to be zero, but the effective total is non-zero.
C++
#include <bits/stdc++.h>
using namespace std;
string solveEquation(string equation)
{
int n = equation.size(), sign = 1, coeff = 0;
int total = 0, i = 0;
for ( int j = 0; j < n; j++) {
if (equation[j] == '+' || equation[j] == '-' ) {
if (j > i)
total += sign * stoi(equation.substr(i, j - i));
i = j;
}
else if (equation[j] == 'x' ) {
if ((i == j) || equation[j - 1] == '+' )
coeff += sign;
else if (equation[j - 1] == '-' )
coeff -= sign;
else
coeff += sign * stoi(equation.substr(i, j - i));
i = j + 1;
}
else if (equation[j] == '=' ) {
if (j > i)
total += sign * stoi(equation.substr(i, j - i));
sign = -1;
i = j + 1;
}
}
if (i < n)
total += sign * stoi(equation.substr(i));
if (coeff == 0 && total == 0)
return "Infinite solutions" ;
if (coeff == 0 && total)
return "No solution" ;
int ans = -total / coeff;
return "x=" + to_string(ans);
}
int main()
{
string equation = "x+5-3+x=6+x-2" ;
cout << solveEquation(equation);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String solveEquation(String equation)
{
int n = equation.length(),
sign = 1 , coeff = 0 ;
int total = 0 , i = 0 ;
for ( int j = 0 ; j < n; j++)
{
if (equation.charAt(j) == '+' ||
equation.charAt(j) == '-' )
{
if (j > i)
total += sign *
Integer.parseInt(
equation.substring(i, j));
i = j;
}
else if (equation.charAt(j) == 'x' )
{
if ((i == j) ||
equation.charAt(j - 1 ) == '+' )
coeff += sign;
else if (equation.charAt(j - 1 ) == '-' )
coeff -= sign;
else
coeff += sign *
Integer.parseInt(
equation.substring(i, j));
i = j + 1 ;
}
else if (equation.charAt(j) == '=' )
{
if (j > i)
total += sign *
Integer.parseInt(
equation.substring(i, j));
sign = - 1 ;
i = j + 1 ;
}
}
if (i < n)
total = total +
sign *
Integer.parseInt(
equation.substring(i));
if (coeff == 0 &&
total == 0 )
return "Infinite solutions" ;
if (coeff == 0 &&
total != 0 )
return "No solution" ;
int ans = -total / coeff;
return (Integer.toString(ans));
}
public static void main(String args[])
{
String equation = new String( "x+5-3+x=6+x-2" );
System.out.print( "x = " +
solveEquation(equation));
}
}
|
Python3
def solveEquation(equation) :
n = len (equation)
sign = 1
coeff = 0
total = 0
i = 0
for j in range ( 0 , n) :
if (equation[j] = = '+' or
equation[j] = = '-' ) :
if (j > i) :
total = (total + sign *
int (equation[i: j]))
i = j
elif (equation[j] = = 'x' ) :
if ((i = = j) or
equation[j - 1 ] = = '+' ) :
coeff + = sign
elif (equation[j - 1 ] = = '-' ) :
coeff = coeff - sign
else :
coeff = (coeff + sign *
int (equation[i: j]))
i = j + 1
elif (equation[j] = = '=' ) :
if (j > i) :
total = (total + sign *
int (equation[i: j]))
sign = - 1
i = j + 1
if (i < n) :
total = (total + sign *
int (equation[i: len (equation)]))
if (coeff = = 0 and
total = = 0 ) :
return "Infinite solutions"
if (coeff = = 0 and total) :
return "No solution"
ans = - total / coeff
return int (ans)
equation = "x+5-3+x=6+x-2"
print ( "x = {}" .
format (solveEquation(equation)))
|
C#
using System;
class GFG
{
static string solveEquation( string equation)
{
int n = equation.Length,
sign = 1, coeff = 0;
int total = 0, i = 0;
for ( int j = 0; j < n; j++)
{
if (equation[j] == '+' ||
equation[j] == '-' )
{
if (j > i)
total += sign *
Int32.Parse(
equation.Substring(i, j - i));
i = j;
}
else if (equation[j] == 'x' )
{
if ((i == j) ||
equation[j - 1] == '+' )
coeff += sign;
else if (equation[j - 1] == '-' )
coeff -= sign;
else
coeff += sign *
Int32.Parse(
equation.Substring(i, j - i));
i = j + 1;
}
else if (equation[j] == '=' )
{
if (j > i)
total += sign *
Int32.Parse(
equation.Substring(i, j - i));
sign = -1;
i = j + 1;
}
}
if (i < n)
total += sign *
Int32.Parse(
equation.Substring(i));
if (coeff == 0 && total == 0)
return "Infinite solutions" ;
if (coeff == 0 && total != 0)
return "No solution" ;
int ans = -total / coeff;
return "x = " + ans.ToString();
}
static void Main()
{
string equation = "x+5-3+x=6+x-2" ;
Console.Write(solveEquation(equation));
}
}
|
PHP
<?php
function solveEquation( $equation )
{
$n = strlen ( $equation );
$sign = 1; $coeff = 0;
$total = 0; $i = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $equation [ $j ] == '+' ||
$equation [ $j ] == '-' )
{
if ( $j > $i )
$total += $sign *
intval ( substr ( $equation ,
$i , $j - $i ));
$i = $j ;
}
else if ( $equation [ $j ] == 'x' )
{
if (( $i == $j ) ||
$equation [ $j - 1] == '+' )
$coeff += $sign ;
else if ( $equation [ $j - 1] == '-' )
$coeff -= $sign ;
else
$coeff += $sign *
intval ( substr ( $equation ,
$i , $j - $i ));
$i = $j + 1;
}
else if ( $equation [ $j ] == '=' )
{
if ( $j > $i )
$total += $sign *
intval ( substr ( $equation ,
$i , $j - $i ));
$sign = -1;
$i = $j + 1;
}
}
if ( $i < $n )
$total += $sign *
intval ( substr ( $equation , $i ));
if ( $coeff == 0 &&
$total == 0)
return "Infinite solutions" ;
if ( $coeff == 0 && $total )
return "No solution" ;
$ans = - $total / $coeff ;
return "x = " . $ans ;
}
$equation = "x+5-3+x=6+x-2" ;
echo (solveEquation( $equation ));
?>
|
Javascript
function solveEquation(equation)
{
let n = equation.length, sign = 1, coeff = 0;
let total = 0, i = 0;
for (let j = 0; j < n; j++) {
if (equation[j] == '+' || equation[j] == '-' ) {
if (j > i)
total += sign * parseInt(equation.substr(i, j - i));
i = j;
}
else if (equation[j] == 'x' ) {
if ((i == j) || equation[j - 1] == '+' )
coeff += sign;
else if (equation[j - 1] == '-' )
coeff -= sign;
else
coeff += sign * parseInt(equation.substr(i, j - i));
i = j + 1;
}
else if (equation[j] == '=' ) {
if (j > i)
total += sign * parseInt(equation.substr(i, j - i));
sign = -1;
i = j + 1;
}
}
if (i < n)
total += sign * parseInt(equation.substr(i));
if (coeff == 0 && total == 0)
return "Infinite solutions" ;
if (coeff == 0 && total)
return "No solution" ;
let ans = -Math.floor(total / coeff);
return "x=" + ans.toString();
}
let equation = "x+5-3+x=6+x-2" ;
console.log(solveEquation(equation));
|
Time complexity : O(n), where n is the length of equation string. Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...