Given an alphanumeric string, consisting of a single alphabet X, which represents an expression of the form:
A operator B = C where A, B and C denotes integers and the operator can be either of +, -, * or /
The task is to evaluate the missing digit X present in any of the integers A, B and C such that the given expression holds to be valid.
Examples:
Input: S = “3x + 12 = 46”
Output: 4
Explanation:
If we subtract 12 from 46, we will get 34.
So, on comparing 3x and 34. the value of x = 4Input: S = “4 – 2 = x”
Output: 2
Explanation:
After solving the equation, the value of x = 2.
Approach: Follow the steps below to solve the problem:
- Split the string to extract the two operands, operator and the resultant.
- Check if X is present in the resultant or not. If so, then compute the value of the resultant by applying operations on the first operand and second operand with the operator.
- Otherwise, if X is not present in the resultant. Then check if X is present in the first operand. If so, then apply the operation on the second operand and resultant with the operator.
- Otherwise, if X is not present in the first operand also. Then check if X is present in the second operand. If so, then apply the operation on the first operand and resultant with the operator.
Below is the implementation of the above approach:
#include <iostream> #include <sstream> #include <string> using namespace std;
int MissingDigit(string exp )
{ // Split the expression into tokens
stringstream ss( exp );
string token;
string expArr[5];
int i = 0;
while (getline(ss, token, ' ' )) {
expArr[i++] = token;
}
string firstOperand = expArr[0];
string operatorStr = expArr[1];
string secondOperand = expArr[2];
string resultant = expArr[4];
int res = 0;
string x = "" ;
if (resultant.find( "x" ) != string::npos) {
x = resultant;
firstOperand = expArr[0];
secondOperand = expArr[2];
int first = stoi(firstOperand);
int second = stoi(secondOperand);
if (operatorStr == "+" ) {
res = first + second;
}
else if (operatorStr == "-" ) {
res = first - second;
}
else if (operatorStr == "*" ) {
res = first * second;
}
else {
res = first / second;
}
}
else {
resultant = expArr[4];
int result = stoi(resultant);
if (firstOperand.find( "x" ) != string::npos) {
x = firstOperand;
secondOperand = expArr[2];
int second = stoi(secondOperand);
if (operatorStr == "+" ) {
res = result - second;
}
else if (operatorStr == "-" ) {
res = result + second;
}
else if (operatorStr == "*" ) {
res = result / second;
}
else {
res = result * second;
}
}
else {
x = secondOperand;
firstOperand = expArr[0];
int first = stoi(firstOperand);
if (operatorStr == "+" ) {
res = result - first;
}
else if (operatorStr == "-" ) {
res = first - result;
}
else if (operatorStr == "*" ) {
res = result / first;
}
else {
res = first / result;
}
}
}
string resStr = to_string(res);
int k = 0;
for ( int i = 0; i < x.length(); i++) {
if (x[i] == 'x' ) {
return resStr[k] - '0' ;
}
else {
k++;
}
}
return 0;
} int main()
{ string exp = "3x + 12 = 46" ;
cout << MissingDigit( exp ) << endl;
return 0;
} |
import java.util.*;
class MissingDigit {
public static int MissingDigit(String exp) {
String[] expArr = exp.split( " " );
String firstOperand = expArr[ 0 ];
String operator = expArr[ 1 ];
String secondOperand = expArr[ 2 ];
String resultant = expArr[ 4 ];
int res = 0 ;
String x = "" ;
if (resultant.contains( "x" )) {
x = resultant;
firstOperand = expArr[ 0 ];
secondOperand = expArr[ 2 ];
int first = Integer.parseInt(firstOperand);
int second = Integer.parseInt(secondOperand);
if (operator.equals( "+" )) {
res = first + second;
} else if (operator.equals( "-" )) {
res = first - second;
} else if (operator.equals( "*" )) {
res = first * second;
} else {
res = first / second;
}
} else {
resultant = expArr[ 4 ];
int result = Integer.parseInt(resultant);
if (firstOperand.contains( "x" )) {
x = firstOperand;
secondOperand = expArr[ 2 ];
int second = Integer.parseInt(secondOperand);
if (operator.equals( "+" )) {
res = result - second;
} else if (operator.equals( "-" )) {
res = result + second;
} else if (operator.equals( "*" )) {
res = result / second;
} else {
res = result * second;
}
} else {
x = secondOperand;
firstOperand = expArr[ 0 ];
int first = Integer.parseInt(firstOperand);
if (operator.equals( "+" )) {
res = result - first;
} else if (operator.equals( "-" )) {
res = first - result;
} else if (operator.equals( "*" )) {
res = result / first;
} else {
res = first / result;
}
}
}
String resStr = String.valueOf(res);
int k = 0 ;
for ( int i = 0 ; i < x.length(); i++) {
if (x.charAt(i) == 'x' ) {
return Character.getNumericValue(resStr.charAt(k));
} else {
k++;
}
}
return 0 ;
}
public static void main(String[] args) {
String exp = "3x + 12 = 46" ;
System.out.println(MissingDigit(exp));
}
} // this code is contributed by devendra1 |
# Python3 program to find missing # digit x in expression def MissingDigit(exp):
# Split the expression to
# extract operands, operator
# and resultant
exp = list (exp.split())
first_operand = exp[ 0 ]
operator = exp[ 1 ]
second_operand = exp[ 2 ]
resultant = exp[ - 1 ]
# If x is present in resultant
if 'x' in resultant:
x = resultant
first_operand = int (first_operand)
second_operand = int (second_operand)
if operator = = '+' :
res = first_operand + second_operand
elif operator = = '-' :
res = first_operand - second_operand
elif operator = = '*' :
res = first_operand * second_operand
else :
res = first_operand / / second_operand
# If x in present in operands
else :
resultant = int (resultant)
# If x in the first operand
if 'x' in first_operand:
x = first_operand
second_operand = int (second_operand)
if operator = = '+' :
res = resultant - second_operand
elif operator = = '-' :
res = resultant + second_operand
elif operator = = '*' :
res = resultant / / second_operand
else :
res = resultant * second_operand
# If x is in the second operand
else :
x = second_operand
first_operand = int (first_operand)
if operator = = '+' :
res = resultant - first_operand
elif operator = = '-' :
res = first_operand - resultant
elif operator = = '*' :
res = resultant / / first_operand
else :
res = first_operand / / resultant
res = str (res)
k = 0
for i in x:
if i = = 'x' :
result = res[k]
break
else :
k = k + 1
return result
# Driver Code if __name__ = = '__main__' :
# input expression
exp = "3x + 12 = 46"
print (MissingDigit(exp))
|
using System;
class GFG {
// This function computes the missing digit
public static int MissingDigit( string exp)
{
string [] expArr = exp.Split( " " );
// Extracting the operants and operators
string firstOperand = expArr[0];
string operator_ = expArr[1];
string secondOperand = expArr[2];
string resultant = expArr[4];
int res = 0;
string x = "" ;
// If x is present in resultant
if (resultant.Contains( "x" )) {
x = resultant;
firstOperand = expArr[0];
secondOperand = expArr[2];
int first = Int32.Parse(firstOperand);
int second = Int32.Parse(secondOperand);
if (operator_ == "+" ) {
res = first + second;
}
else if (operator_ == "-" ) {
res = first - second;
}
else if (operator_ == "*" ) {
res = first * second;
}
else {
res = first / second;
}
}
else
// If x is present in operands
{
resultant = expArr[4];
int result = Int32.Parse(resultant);
if (firstOperand.Contains( "x" )) {
// If x is in the first operand
x = firstOperand;
secondOperand = expArr[2];
int second = Int32.Parse(secondOperand);
if (operator_ == "+" ) {
res = result - second;
}
else if (operator_ == "-" ) {
res = result + second;
}
else if (operator_ == "*" ) {
res = result / second;
}
else {
res = result * second;
}
}
else {
// If x is in the second operand
x = secondOperand;
firstOperand = expArr[0];
int first = Int32.Parse(firstOperand);
if (operator_ == "+" ) {
res = result - first;
}
else if (operator_ == "-" ) {
res = first - result;
}
else if (operator_ == "*" ) {
res = result / first;
}
else {
res = first / result;
}
}
}
string resStr = res.ToString();
int k = 0;
for ( int i = 0; i < x.Length; i++) {
if (x[i] == 'x' ) {
return ( int )Char.GetNumericValue(resStr[k]);
}
else {
k++;
}
}
return 0;
}
// Driver code
public static void Main( string [] args)
{
string exp = "3x + 12 = 46" ;
// Function call
Console.WriteLine(MissingDigit(exp));
}
} // This code is contributed by phasing17 |
// JavaScript program to find missing // digit x in expression let res = 0,x; let result = "" ;
function MissingDigit(exp){
// Split the expression to
// extract operands, operator
// and resultant
exp = exp.split( ' ' )
let first_operand = exp[0]
let operator = exp[1]
let second_operand = exp[2]
let resultant = exp[exp.length-1]
// If x is present in resultant
if (resultant.indexOf( 'x' ) != -1){
x = resultant
first_operand = parseInt(first_operand)
second_operand = parseInt(second_operand)
if (operator == '+' )
res = first_operand + second_operand
else if (operator == '-' )
res = first_operand - second_operand
else if (operator == '*' )
res = first_operand * second_operand
else
res = Math.floor(first_operand / second_operand)
}
// If x in present in operands
else {
resultant = parseInt(resultant)
// If x in the first operand
if (first_operand.indexOf( 'x' ) != -1){
x = first_operand
second_operand = parseInt(second_operand)
if (operator == '+' )
res = resultant - second_operand
else if (operator == '-' )
res = resultant + second_operand
else if (operator == '*' )
res = Math.floor(resultant / second_operand)
else
res = resultant * second_operand
}
// If x is in the second operand
else {
let x = second_operand
first_operand = parseInt(first_operand)
if (operator == '+' )
res = resultant-first_operand
else if (operator == '-' )
res = first_operand - resultant
else if (operator == '*' )
res = Math.floor(resultant / first_operand)
else
res = Math.floor(first_operand / resultant)
}
}
res = res.toString()
let k = 0
for (let i of x){
if (i == 'x' ){
result = res[k]
break
}
else
k = k + 1
}
return result
} // Driver Code // input expression let exp = "3x + 12 = 46"
console.log(MissingDigit(exp)) // This code is contributed by shinjanpatra |
4
Time Complexity: O(L), where is the length of the equation.
Auxiliary Space: O(1)