Find the missing value from the given equation a + b = c
Last Updated :
01 Sep, 2022
Given an equation of the form:
a + b = c
Out of which any one of the terms , or is missing. The task is to find the missing term.
Examples:
Input: 2 + 6 = ?
Output: 8
Input: ? + 3 =6
Output: 3
Approach:
Missing numbers can be found simply using the equation . First, we will find two known numbers from the given equation(read as a string in the program) and convert them into integers, and put into the equation. In this way, we can find the third missing number. We can implement it by storing the equation into the string.
Below is the step by step algorithm:
- Split the string into smaller strings from the position of spaces and store in an array. So that the array will contain:
arr[0] = "a"
arr[1] = "+"
arr[2] = "b"
arr[3] = "="
arr[4] = "c"
- The missing character can occur at position 0 or 2 or 4 in the vector. Find the position of missing character.
- Convert known characters to integers.
- Find missing character using the equation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMissing(string str)
{
string arrStr[5];
stringstream ss(str);
int i = 0;
while (ss.good() && i < 5) {
ss >> arrStr[i];
++i;
}
int pos = -1;
if (arrStr[0] == "?" )
pos = 0;
else if (arrStr[2] == "?" )
pos = 2;
else
pos = 4;
if (pos == 0)
{
string b,c;
b = arrStr[2];
c = arrStr[4];
int a = stoi(c) - stoi(b);
return a;
}
else if (pos==2)
{
string a,c;
a = arrStr[0];
c = arrStr[4];
int b = stoi(c) - stoi(a);
return b;
}
else if (pos == 4)
{
string b,a;
a = arrStr[0];
b = arrStr[2];
int c = stoi(a) + stoi(b);
return c;
}
}
int main()
{
string str = "? + 3 = 7" ;
cout<<findMissing(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMissing(String str)
{
String arrStr[] = str.split( " " );
int pos = - 1 ;
if (arrStr[ 0 ].equals( "?" ))
pos = 0 ;
else if (arrStr[ 2 ].equals( "?" ))
pos = 2 ;
else
pos = 4 ;
if (pos == 0 )
{
String b, c;
b = arrStr[ 2 ];
c = arrStr[ 4 ];
int a = Integer.parseInt(c) -
Integer.parseInt(b);
return a;
}
else if (pos == 2 )
{
String a, c;
a = arrStr[ 0 ];
c = arrStr[ 4 ];
int b = Integer.parseInt(c) -
Integer.parseInt(a);
return b;
}
else if (pos == 4 )
{
String b, a;
a = arrStr[ 0 ];
b = arrStr[ 2 ];
int c = Integer.parseInt(a) +
Integer.parseInt(b);
return c;
}
return 0 ;
}
public static void main(String []args)
{
String str = "? + 3 = 7" ;
System.out.print(findMissing(str));
}
}
|
Python3
def findMissing(s):
arrStr = s.split()
pos = - 1 ;
if (arrStr[ 0 ] = = "?" ):
pos = 0 ;
elif (arrStr[ 2 ] = = "?" ):
pos = 2 ;
else :
pos = 4 ;
if (pos = = 0 ):
b = arrStr[ 2 ];
c = arrStr[ 4 ];
a = int (c) - int (b);
return a;
elif (pos = = 2 ):
a = arrStr[ 0 ];
c = arrStr[ 4 ];
b = int (c) - int (a);
return b;
elif (pos = = 4 ):
a = arrStr[ 0 ];
b = arrStr[ 2 ];
c = int (a) + int (b);
return c;
if __name__ = = '__main__' :
s = "? + 3 = 7" ;
print (findMissing(s))
|
C#
using System;
class GFG
{
static int findMissing( string str)
{
string [] arrStr = str.Split( " " );
int pos = -1;
if (arrStr[0].Equals( "?" ))
pos = 0;
else if (arrStr[2].Equals( "?" ))
pos = 2;
else
pos = 4;
if (pos == 0)
{
string b, c;
b = arrStr[2];
c = arrStr[4];
int a = int .Parse(c) - int .Parse(b);
return a;
}
else if (pos == 2)
{
string a, c;
a = arrStr[0];
c = arrStr[4];
int b = int .Parse(c) - int .Parse(a);
return b;
}
else if (pos == 4)
{
string b, a;
a = arrStr[0];
b = arrStr[2];
int c = int .Parse(a) + int .Parse(b);
return c;
}
return 0;
}
public static void Main( string [] args)
{
string str = "? + 3 = 7" ;
Console.WriteLine(findMissing(str));
}
}
|
Javascript
<script>
function findMissing(str)
{
let arrStr = str.split( " " );
let pos = -1;
if (arrStr[0]==( "?" ))
pos = 0;
else if (arrStr[2]==( "?" ))
pos = 2;
else
pos = 4;
if (pos == 0)
{
let b, c;
b = arrStr[2];
c = arrStr[4];
let a = parseInt(c) -
parseInt(b);
return a;
}
else if (pos == 2)
{
let a, c;
a = arrStr[0];
c = arrStr[4];
let b = parseInt(c) -
parseInt(a);
return b;
}
else if (pos == 4)
{
let b, a;
a = arrStr[0];
b = arrStr[2];
let c = parseInt(a) +
parseInt(b);
return c;
}
return 0;
}
let str = "? + 3 = 7" ;
document.write(findMissing(str));
</script>
|
Complexity Analysis:
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...