Solved c++, easy based on simple theory


#1

int mod = 1000003;

int fact(int n)
{
if(n==0) return 1 ; else return (n*fact(n-1)) %mod;
}

int Solution::findRank(string A) {

                string s=A ;
                //cin>>s;
                int ans =0;
                int n=s.length();
                for(int i=0;i<n-1;i++)
                    {
                        int c=0;
                        for(int j=i+1;j<n;j++)
                            {
                                    if(s[j]<s[i])
                                        c++;
                            }   
                            ans+=( (c*fact(n-i-1)) )%mod;
                            //cout << c <<" "<<ans<< endl;
                    }
                    return (ans+1 )%mod;
                    //cout << ans+1 ;

}


#2

Hi! Can you explain the logic behind your approach?


#3

https://www.youtube.com/watch?v=iyB12fJjawY watch this video to get the logic


#4

If their large number test case for submit is taking the factorial mod 100003, I don’t think it actually has the correct answer, but that is what you have to do get the same answer they do. I don’t think you can represent factorials larger than 20! in a 64 bit integer.