Jatin and Mirror Numbers (DCUS ) question

here is question link :-

my solution link(partially accepted):-
https://www.codechef.com/viewsolution/28587596

i need a better answer and if you can please tell me in which case is it failing it will be just amazing.

here is the code:-



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

int main()
{
    long int k;
    cin>>k;
    
    while(k--)
    {
        unsigned long long int n;
        cin>>n;
        
        if(n<10)
        {  
            if(n!=0)
           {
            cout<<"true"<<endl;
           }
           else
           {
               cout<<"false"<<endl;
               //cout<<" # #";
           }
        }
        else
        {
        
        long long int q=1,f=0,c=0,t1,t2;
        t1=n;
        t2=n;
        while(q)
        {
            q=t2%10;
            t2=t2/10;
            if(q==0&&t2!=0)
            {
                f=1;
                break;
            }
            c++;
            if(t2==0)
            {
                break;
            }
        }
        if(f==1)
        {
            cout<<"false"<<endl;
         
        }
        else
        {
            if(c%2==0)
            {  
                long long int A[(c/2)-1],B[(c/2)-1],i=0,j,q2,flag1=0;
                
                while(i<c)
                {  
                    q2=t1%10;
                    t1=t1/10;
                   if(i>=(c/2))
                   { 
                       B[i-((c/2))]=q2;
                       i++;
                   }
                   else
                   {
                       A[i]=q2;
                       i++;
                   }
                }
                
                for(i=0;i<c/2;++i)
                {
                    
                    if(A[i]==1&&B[((c/2)-1)-i]!=1)
                    {   
                        flag1=1;
                        break;
                    }
                    if(A[i]==2&&B[((c/2)-1)-i]!=7)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==3&&B[((c/2)-1)-i]!=8)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==4&&B[((c/2)-1)-i]!=5)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==5&&B[((c/2)-1)-i]!=4)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==6&&B[((c/2)-1)-i]!=9)
                    
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==7&&B[((c/2)-1)-i]!=2)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==8&&B[((c/2)-1)-i]!=3)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==9&&B[((c/2)-1)-i]!=6)
                    {
                        flag1=1;
                        break;
                    }
                    
                }
                if(flag1==0)
                {
                    cout<<"true"<<endl;
                }
                else
                {
                    cout<<"false"<<endl;
                }
            }
            else
            {
                long long int A[(c-1)/2],B[(c-1)/2],i=0,j,q2,flag1=0;
                
                while(i<c)
                {   
                    
                   if(i>(c/2))
                   {   
                       q2=t1%10;
                       t1=t1/10;
                       B[i-((c/2)+1)]=q2;
                       i++;
                       
                   }
                   else if(i<(c/2))
                   {  
                       q2=t1%10;
                       t1=t1/10;
                       A[i]=q2;
                       i++;
                       
                   }
                   if(i==(c/2))
                   { 
                       q2=t1%10;
                        t1=t1/10;
                       i++;
                       
                   }
                }
                
                for(i=0;i<=((c-1)/2)-1;++i)
                {
                    if(A[i]==1&&B[(((c-1)/2)-1)-i]!=1)
                    {   
                        flag1=1;
                        break;
                    }
                    if(A[i]==2&&B[(((c-1)/2)-1)-i]!=7)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==3&&B[(((c-1)/2)-1)-i]!=8)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==4&&B[(((c-1)/2)-1)-i]!=5)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==5&&B[(((c-1)/2)-1)-i]!=4)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==6&&B[(((c-1)/2)-1)-i]!=9)
                    
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==7&&B[(((c-1)/2)-1)-i]!=2)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==8&&B[(((c-1)/2)-1)-i]!=3)
                    {
                        flag1=1;
                        break;
                    }
                    if(A[i]==9&&B[(((c-1)/2)-1)-i]!=6)
                    {
                        flag1=1;
                        break;
                    }
                }
                if(flag1==0)
                {
                    cout<<"true"<<endl;
                }
                else
                {
                    cout<<"false"<<endl;
                }
            }
           }
        }
    }

    return 0;
}

Your code cannot be accessed. Put it in pastebin or ideone and add the link

ohk ,can you share your code ,mine is partially accepted

My code
//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;

bool ismirror(int x,int y)
{
    if(x>y)
        swap(x,y);
    if(x==1 && y==1)
        return true;
    if(x==2 && y==7)
        return true;
    if(x==3 && y==8)
        return true;
    if(x==4 && y==5)
        return true;
    if(x==6 && y==9)
        return true;
    return false;
}

int main()
{
    std::ios::sync_with_stdio(false);
    int T;
    cin>>T;
    // cin.ignore(); must be there when using getline(cin, s)
    while(T--)
    {
        string s;
        cin>>s;
        int i=0,j=s.length()-1,flag=0;
        while(i<j)
        {
            if(!ismirror(s[i]-'0',s[j]-'0'))
            {
                flag=1;
                break;
            }
            ++i,--j;
        }
        if(i==j && s[i]=='0')
            flag=1;
        if(flag)
            cout<<"false"<<endl;
        else
            cout<<"true"<<endl;
    }
    return 0;
}

The error in urs is that ur n is an unsigned long long but n can be upto 1e5 digits, which means it must accepted as a string

2 Likes

can you please ellaborate ,i didn’t got your point.

Use map code will be much simpler

You have to take it as String since its a big number and you can’t store such big numbers in long long.

2 Likes

oh but can a string hold upto 10^(10^5)?

You can store the number as a string of at most 10^{5} + 1 characters, and then just simply iterate over it to check if it is a Mirror Number in O(\log_{10}{N}) time (which is actually the number of digits in N!).
All solutions have been made public now, so you can also refer to others’ solutions now.

1 Like

thanks mr admin:grinning:

1 Like