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 a , b or c 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 a + b = c . 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++
// C++ program to find the missing number
// in the equation a + b = c
#include <bits/stdc++.h>
using namespace std;
// Function to find the missing number
// in the equation a + b = c
int findMissing(string str)
{
// Array of string to store individual strings
// after splitting the strings from spaces
string arrStr[5];
// Using stringstream to read a string object
// and split
stringstream ss(str);
int i = 0;
while (ss.good() && i < 5) {
ss >> arrStr[i];
++i;
}
int pos = -1;
// Find position of missing character
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];
// Using stoi() to convert strings to int
int a = stoi(c) - stoi(b);
return a;
}
else if(pos==2)
{
string a,c;
a = arrStr[0];
c = arrStr[4];
// Using stoi() to convert strings to int
int b = stoi(c) - stoi(a);
return b;
}
else if(pos == 4)
{
string b,a;
a = arrStr[0];
b = arrStr[2];
// Using stoi() to convert strings to int
int c = stoi(a) + stoi(b);
return c;
}
}
// Driver code
int main()
{
// Equation with missing value
string str = "? + 3 = 7";
cout<<findMissing(str);
return 0;
}
Java
// Java program to find the missing number
// in the equation a + b = c
import java.util.*;
class GFG{
// Function to find the missing number
// in the equation a + b = c
static int findMissing(String str)
{
// Array of String to store individual
// strings after splitting the strings
// from spaces
String arrStr[] = str.split(" ");
int pos = -1;
// Find position of missing character
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];
// Using Integer.parseInt() to
// convert strings to int
int a = Integer.parseInt(c) -
Integer.parseInt(b);
return a;
}
else if (pos == 2)
{
String a, c;
a = arrStr[0];
c = arrStr[4];
// Using Integer.parseInt() to
// convert strings to int
int b = Integer.parseInt(c) -
Integer.parseInt(a);
return b;
}
else if (pos == 4)
{
String b, a;
a = arrStr[0];
b = arrStr[2];
// Using Integer.parseInt() to
// convert strings to int
int c = Integer.parseInt(a) +
Integer.parseInt(b);
return c;
}
return 0;
}
// Driver code
public static void main(String []args)
{
// Equation with missing value
String str = "? + 3 = 7";
System.out.print(findMissing(str));
}
}
// This code is contributed by pratham76
Python3
# Python3 program to find the missing number
# in the equation a + b = c
# Function to find the missing number
# in the equation a + b = c
def findMissing(s):
# Array of string to store individual strings
# after splitting the strings from spaces
arrStr = s.split()
# Using stringstream to read a string object
# and split
pos = -1;
# Find position of missing character
if(arrStr[0] == "?"):
pos = 0;
elif(arrStr[2] == "?"):
pos = 2;
else:
pos = 4;
if(pos == 0):
b = arrStr[2];
c = arrStr[4];
# Using int() to convert strings to int
a = int(c) - int(b);
return a;
elif(pos == 2):
a = arrStr[0];
c = arrStr[4];
# Using int() to convert strings to int
b = int(c) - int(a);
return b;
elif(pos == 4):
a = arrStr[0];
b = arrStr[2];
# Using int() to convert strings to int
c = int(a) + int(b);
return c;
# Driver code
if __name__=='__main__':
# Equation with missing value
s = "? + 3 = 7";
print(findMissing(s))
# This code is contributed by rutvik_56
C#
// C# program to find the missing number
// in the equation a + b = c
using System;
class GFG
{
// Function to find the missing number
// in the equation a + b = c
static int findMissing(string str)
{
// Array of String to store individual
// strings after splitting the strings
// from spaces
string[] arrStr = str.Split(" ");
int pos = -1;
// Find position of missing character
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];
// Using Integer.parseInt() to
// convert strings to int
int a = int.Parse(c) - int.Parse(b);
return a;
}
else if (pos == 2)
{
string a, c;
a = arrStr[0];
c = arrStr[4];
// Using Integer.parseInt() to
// convert strings to int
int b = int.Parse(c) - int.Parse(a);
return b;
}
else if (pos == 4)
{
string b, a;
a = arrStr[0];
b = arrStr[2];
// Using Integer.parseInt() to
// convert strings to int
int c = int.Parse(a) + int.Parse(b);
return c;
}
return 0;
}
// Driver code
public static void Main(string[] args)
{
// Equation with missing value
string str = "? + 3 = 7";
Console.WriteLine(findMissing(str));
}
}
// This code is contributed by chitranayal.
JavaScript
<script>
// JavaScript program to find the missing number
// in the equation a + b = c
// Function to find the missing number
// in the equation a + b = c
function findMissing(str)
{
// Array of String to store individual
// strings after splitting the strings
// from spaces
let arrStr = str.split(" ");
let pos = -1;
// Find position of missing character
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];
// Using Integer.parseInt() to
// convert strings to int
let a = parseInt(c) -
parseInt(b);
return a;
}
else if (pos == 2)
{
let a, c;
a = arrStr[0];
c = arrStr[4];
// Using Integer.parseInt() to
// convert strings to int
let b = parseInt(c) -
parseInt(a);
return b;
}
else if (pos == 4)
{
let b, a;
a = arrStr[0];
b = arrStr[2];
// Using Integer.parseInt() to
// convert strings to int
let c = parseInt(a) +
parseInt(b);
return c;
}
return 0;
}
// Driver code
// Equation with missing value
let str = "? + 3 = 7";
document.write(findMissing(str));
// This code is contributed by rag2127
</script>
Complexity Analysis:
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Similar Reads
Find the missing digit x from the given expression 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 th
9 min read
Form the Cubic equation from the given roots Given the roots of a cubic equation A, B and C, the task is to form the Cubic equation from the given roots.Note: The given roots are integral.Examples:Input: A = 1, B = 2, C = 3 Output: x^3 - 6x^2 + 11x - 6 = 0 Explanation: Since 1, 2, and 3 are roots of the cubic equations, Then equation is given
5 min read
Find the quadratic equation from the given roots Given the roots of a quadratic equation A and B, the task is to find the equation.Note: The given roots are integral. Examples: Input: A = 2, B = 3 Output: x^2 - (5x) + (6) = 0 x2 - 5x + 6 = 0 x2 -3x -2x + 6 = 0 x(x - 3) - 2(x - 3) = 0 (x - 3) (x - 2) = 0 x = 2, 3 Input: A = 5, B = 10 Output: x^2 -
4 min read
Find the number of solutions to the given equation Given three integers A, B and C, the task is to find the count of values of X such that the following condition is satisfied, X = B * Sm(X)A + C where Sm(X) denotes the sum of digits of X and 1 < X < 109.Examples: Input: A = 3, B = 2, C = 8 Output: 3 For X = 10, 2 * (1)3 + 8 = 10 For X = 2008,
6 min read
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs Given six positive integers representing the Bitwise XOR and Bitwise AND of all possible pairs of a triplet (a, b, c), the task is to find the triplet. Examples: Input: aXORb = 30, aANDb = 0, aXORc = 10, aANDc = 20, aXORb = 20, aANDb = 10 Output: a = 10, b = 20, c= 30 Explanation: If a = 10, b = 20,
7 min read