VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-missing-value-from-the-given-equation-a-b-c/

⇱ Find the missing value from the given equation a + b = c - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the missing value from the given equation a + b = c

Last Updated : 1 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:  


Output
4

Complexity Analysis:

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)
Comment
Article Tags:
Article Tags: