Last Updated: 22 Apr, 2021

Nobita’s Assignment

Moderate
Asked in company
314e Corporation

Problem statement

Nobita has got math assignment to solve. In this assignment, he has to solve a mathematical equation given in the form of a string 'S'. The equation contains only ‘+’ and ‘-’ operators and a variable ‘x’. Nobita doesn’t know how to solve the equation. So, he went to Doremon for help. Doremon gives him a gadget for this. But as usual, Nobita didn’t listen to the instructions to use that gadget.

So, now it's your task to help Nobita find solutions to the given equation, or if there is no solution, report that as well.

Note :
If there is no solution for the equation, return "No solution.”

If there is only one solution for the equation, return that solution for ‘x.’

If there are infinite solutions for the equation, return "Infinite solutions.”

‘x’ may or may not have coefficients associated with it.

‘+’, ‘-’, and ‘x’ can occur any number of times in the equation.
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases.

The next ‘T’ lines contain a string ‘S’ representing the equation given to Nobita.
Output Format :
For each test case, print the value of ‘x’, and if the solution doesn’t exist, then print “No solution,” and if there are infinite solutions, then print “Infinite solutions”.

Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of.
Constraints :
1 <= T <= 50
3 <= |S| <= 10^7
-100 <= y <= 100

Time limit: 1 sec

Approaches

01 Approach

Each constant and each variable have a corresponding sign to them ‘+’ or ‘-’. So divide the string into two partitions; all that comes before “=” is LHS and after “=” is treated as RHS. Then exclusively use the property of shifting from LHS to RHS or vice versa to solve the equation.

 

The steps are as follows:

 

  • Declare a  variable ‘FLAG’ for partition logic. This variable will be of type int and declare it with 1 initially.
  • Declare two variables ‘CONSTANT' = 0 and ‘COEFF' = 0 of type int to store resultant constants and coefficients sum from both LHS and RHS.
  • Iterate over the equation string ‘S’ (say iterator be ‘i’)
    • Declare a variable ‘SIGN’ of type int.
    • If ‘S[i]’  is equal to ‘=’ then change the ‘FLAG’ to -1.
    • Else If ‘S[i]’ is equal to ‘+’ then change ‘SIGN’ to 1.
    • If ‘S[i]’ is equal to ‘-’ then change ‘SIGN’ to -1.
    • Move iterator ‘i’ to find if we have a variable next to the current sign or a digit.
      • If there is a Digit, move ‘i’ till you find the complete number:
    • Store the number in ‘TEMP.’
    • If ‘S[i]’ == ’x’:
      • Increment ‘COEFF’ by ‘FLAG’ * ‘TEMP’ * ‘SIGN’
    • Else:
      • Increment ‘CONSTANT’ by ‘FLAG’ * ‘TEMP’ * ‘SIGN’.
    • Else if there is Variable ‘x’: Increment ‘COEFF’ by ‘FLAG’ * ‘SIGN.’
  • If the total sum of coefficients from both LHS and RHS and the total sum of constants from both LHS and RHS is 0, i.e., ‘CONSTANT’ == 0 and ‘COEFF’ == 0, in that case, return “Infinite Solutions”.
  • If the total sum of the coefficient from both LHS and RHS is 0, and the total sum of constants from both LHS and RHS is non zero, i.e., ‘COEFF’ == 0 and ‘CONSTANT’ != 0; in that case return “No Solution.”
  • In all other cases, return the string of a single solution hence deduced.