Given an arbitrarily large number, how to check if it is same as it’s mirror image.
For example 101 is a mirror image of itself, where as 1811 is not a mirror image of itself.
This problem is from Hacker earth (A website for competitive programming). If you want to solve this problem on your own, and execute it on different possible test cases for it’s correctness, follow this link. If you just want to know the solution, read on…
The solution is simple; the number should only contain the digits from the set {0,1,8} because they look the same when read in a mirror and it should also be a palindrome.
Here is the simple program to do this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int T; | |
cin >> T; | |
while ( T-- > 0) | |
{ | |
string input; | |
string result = "YES"; | |
cin >> input; | |
int i,j; | |
for( i = 0; i < input.size(); i++ ) | |
{ | |
if( input[i] != '0' && input[i] != '1' && input[i] != '8' ) | |
{ | |
result = "NO"; | |
break; | |
} | |
} | |
for( i = 0, j = input.size()-1; i <= j; i++, j--) | |
{ | |
if( input[i] != input[j] ) | |
{ | |
result = "NO"; | |
break; | |
} | |
} | |
cout << result << endl; | |
} | |
return 0; | |
} |