Colorful Number

 

Check if the product of every contiguous subsequence is different or not in a number

N = 23
2 3 23
2 -> 2
3 -> 3
23 -> 6
this number is a COLORFUL number since product of every digit of a sub-sequence are different. 

Output : 1
 
 int Solution::colorful(int A) {
   string s= to_string(A); //convert the number to string
   int n=s.size(); //size of the string
   map<long long,bool> m; //map initialize
   for(auto i=0;i<n;i++)  //iteration to find mul of all subsequence
   {
       long long int mul=1;
       for(auto j=i;j<n;j++)
       {
           mul*=(long long int)(s[j]-'0'); //subtracted by '0' because its a string by subtracting with string it  //will be integer
           if(m.find(mul)!=m.end()) //if it will be not the end it means the mul is found hence not colorful
           {
               return 0; //so return 0
           }
           m[mul]=true; //else insert in map
       }
   }
   return 1;

}

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!