Mirror of Mahatma Gandhi

On the way to Dandi March, Gandhijee carried a mirror with himself.

When he reached Dandi, he decided to play a game with the tired people to give them some strength.
At each turn of the game, he pointed out a person and told him to say a number N(possibly huge) of his choice.
The number was called lucky if that equals its a mirror image.

Example:

Input:  s = "101"
Output: YES

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void mirror(string s)
{
    int flag = 0;
    for (int i = 0i < s.size(); i++)
    {
        if (s[i] != '0' && s[i] != '1' && s[i] != '8')
        {
            flag = 1;
            break;
        }
    }
    if (flag)
        cout << "NO\n";
    else
    {
        int i = 0;
        while (i < s.size() / 2)
        {
            if (s[i] != s[s.size() - i - 1])
            {
                flag = 1;
                break;
            }
            i++;
        }
        if (flag)
            cout << "NO\n";
        else
            cout << "YES\n";
    }
}
int main()
{

    string s = "101";

    mirror(s);

    return 0;
}


No comments:

Post a Comment