Check if a number is a Mystery Number Last Updated : 28 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given a number, check whether it is a mystery number or not. A mystery number is a number that can be expressed as the sum of two numbers and those two numbers should be the reverse of each other. Examples: Input : n = 121 Output : 29 92 Input : n = 22 Output : 11 11 Source: Paytm Interview Set 23The idea is to try every possible pair smaller than or equal to n. Below is the implementation of the above approach. C++ // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Finds reverse of given num x. int reverseNum(int x) { string s = to_string(x); reverse(s.begin(), s.end()); stringstream ss(s); int rev = 0; ss >> rev; return rev; } bool isMysteryNumber(int n) { for (int i=1; i <= n/2; i++) { // if found print the pair, return int j = reverseNum(i); if (i + j == n) { cout << i << " " << j; return true; } } cout << "Not a Mystery Number"; return false; } int main() { int n = 121; isMysteryNumber(n); return 0; } Java // Java implementation of above approach class GFG { // Finds reverse of given num x. static int reverseNum(int x) { String s = Integer.toString(x); String str=""; for(int i=s.length()-1;i>=0;i--) { str=str+s.charAt(i); } int rev=Integer.parseInt(str); return rev; } static boolean isMysteryNumber(int n) { for (int i=1; i <= n/2; i++) { // if found print the pair, return int j = reverseNum(i); if (i + j == n) { System.out.println( i + " " + j); return true; } } System.out.println("Not a Mystery Number"); return false; } public static void main(String []args) { int n = 121; isMysteryNumber(n); } } // This code is contributed by ihritik Python3 # Python3 implementation of above approach # Finds reverse of given num x. def reverseNum(x): s = str(x) s = s[::-1] return int(s) def isMysteryNumber(n): for i in range(1, n // 2 + 1): # if found print the pair, return j = reverseNum(i) if i + j == n: print(i, j) return True print("Not a Mystery Number") return False # Driver Code n = 121 isMysteryNumber(n) # This code is contributed by # Mohit Kumar 29 (IIIT gwalior) C# // C# implementation of above approach using System; class GFG { // Finds reverse of given num x. static int reverseNum(int x) { string s = x.ToString(); string str=""; for(int i=s.Length-1;i>=0;i--) { str=str+s[i]; } int rev=Int32.Parse(str); return rev; } static bool isMysteryNumber(int n) { for (int i=1; i <= n/2; i++) { // if found print the pair, return int j = reverseNum(i); if (i + j == n) { Console.WriteLine( i + " " + j); return true; } } Console.WriteLine("Not a Mystery Number"); return false; } public static void Main() { int n = 121; isMysteryNumber(n); } } // This code is contributed by ihritik PHP <?php // PHP implementation of above approach // Finds reverse of given num x. function reverseNum($x) { $s = (string)$x; $s = strrev($s); $rev = (int)$s; return $rev; } function isMysteryNumber($n) { for ($i=1; $i <= $n/2; $i++) { // if found print the pair, return $j = reverseNum($i); if ($i + $j == $n) { echo $i . " ".$j; return true; } } echo "Not a Mystery Number"; return false; } $n = 121; isMysteryNumber($n); return 0; // This code is contributed by Ita_c. ?> JavaScript <script> // Javascript implementation of above approach // Finds reverse of given num x. function reverseNum(x) { let s = x.toString(); let str=""; for(let i=s.length-1;i>=0;i--) { str=str+s[i]; } let rev=parseInt(str); return rev; } function isMysteryNumber(n) { for (let i=1; i <= Math.floor(n/2); i++) { // if found print the pair, return let j = reverseNum(i); if (i + j == n) { document.write( i + " " + j+"<br>"); return true; } } document.write("Not a Mystery Number<br>"); return false; } let n = 121; isMysteryNumber(n); // This code is contributed by avanitrachhadiya2155 </script> Output: 29 92 Time Complexity: O(n) Auxiliary Space: O(log10n) Comment More infoAdvertise with us Next Article Check if a number is a Mystery Number K Kushdeep_Mittal Follow Improve Article Tags : Algorithms Technical Scripter DSA Practice Tags : Algorithms Similar Reads Mathematical Algorithms - Number Digits "Mathematical Algorithms | Number Digits" is a guide that explores problems and solutions involving digits. It covers common digit-related issues, such as digit properties, manipulation, and counting under constraints. The article discusses algorithmic approaches like modular arithmetic, string hand 9 min read Spy Number (Sum and Products of Digits are same) A number is said to be a Spy number if the sum of all the digits is equal to the product of all digits. Examples : Input : 1412Output : Spy NumberExplanation : sum = (1 + 4 + 1 + 2) = 8product = (1 * 4 * 1 * 2) = 8since, sum == product == 8 Input : 132Output : Spy NumberExplanation : sum = (1 + 3 + 4 min read Playing with Numbers Playing with Numbers: Numbers are the mathematical objects used to count, measure, and label. The original examples are the natural numbers 1, 2, 3, 4, and so forth. Numbers can be represented in language with number words. More universally, individual numbers can be represented by symbols, called n 11 min read Is negative 12 a whole number? Numerals are the mathematical figures used in financial, professional as well as a social field in the social world. The digits and place value in the number and the base of the number system determine the value of a number. Numbers are used in various mathematical operations as summation, subtracti 4 min read Is 0.54 a rational number? Numerals are the mathematical figures used in financial, professional as well as a social field in the social world. The digits and place value in the number and the base of the number system determine the value of a number. Numbers are used in various mathematical operations as summation, subtracti 5 min read Like