Thursday, May 2, 2019

Mirror Image / Reverse Palindrome

  • Problem Description

    Puck, the trickster, has again started troubling people in your city. 

    The people have turned on to you for getting rid of Puck. Puck presents to you a number consisting of numbers from 0 to 9 characters. 

    He wants you to reverse it from the final answer such that the number becomes Palindrome number. 

    A Palindrome is a number which equals its reverse. The hope of people are on you so you have to solve the riddle.

    You have to tell if some number exists which you would reverse to convert the number into palindrome.
  • Test Case 1

    Input (stdin)
    2112

    Expected Output
    Mirror Image
  • Test Case 2

    Input (stdin)
    1988

    Expected Output
    Not a Mirror Image
  • Solution
  • #include<stdio.h>

    char s[50008];
    int i,w,f,l,x,y,z,e,k,j;

    int main()
    {
    scanf("%s",s);
    //l=strlen(s);
    l=0;
    while(s[l]!='\0')
    {
    l++;
    }
    for(i=0;i<(l)/2-1&&s[i]==s[l-1-i];i++);
    f=i;
    if(i==(l)/2-1)
    {
    printf("Mirror Image");

    }
    else
    {
    for(i=(l)/2-1;i>=f&&s[i]==s[l-1-i];i--);
    w=i;
    if(w<f)
    {
    printf("Mirror Image");
    }
    else
    {
    i=l-1-f;
    for(;w>=f&&s[i]==s[w];w--,i--);
    if(w<f)
    {
    printf("Yes\n");
    }
    else
    {

    //cout<<"No\n w="<<w<<"f="<<f;
    e=0;
    for(k=l-1-f;k>=f;k--)
    {
    j=f;
    i=k;
    while(s[j]==s[i]&&j<=i)
    {
    j++;
    i--;
    }
    if(i<j)
    {
    x=k+1;
    y=l-1-f;
    e=1;
    //cout<<" e="<<e<<"x="<<x<<"y=<<y<<\n";
    break;
    }

    }
    if(e)
    {
    z=y-x+1;
    z/=2;
    //cout<<" z="<<z<<"x="<<x<<"y="<<y<<"\n";
    for(i=0;i<z;i++)
    {
    if(s[x+i]!=s[x+z+i])
    {
    //cout<<" i="<<i<<"x="<<x<<"y="<<y<<"\n";
    break;
    }
    }
    if(i==z)
    {
    printf("Mirror Image");
    return 0;
    }
    }
    e=0;
    for(k=f;k<=l-1-f;k++)
    {
    j=l-f-1;
    i=k;
    while(s[j]==s[i]&&i<=j)
    {
    j--;
    i++;
    }
    if(i>j)
    {
    y=k-1;
    x=f;
    e=1;
    //cout<<" e="<<e<<"x="<<x<<"y="<<y<<"\n";
    break;
    }
    }
    if(e)
    {
    z=y-x+1;
    z/=2;
    for(i=0;i<z;i++)
    {
    if(s[x+i]!=s[x+z+i])
    {
    break;
    }
    }
    if(i==z)
    {
    printf("Mirror Image");
    return 0;
    }
    }
    printf("Not a Mirror Image");


    }

    }
    }
    return 0;


    }

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...