Colorful Numbers

Objective: Given a number, find out whether its colorful or not.

Colorful Number: When in a given number, product of every digit of a sub-sequence are different. That number is called Colorful Number. See Example

Example:

Given Number : 3245
Output : Colorful
Number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245.
this number is a colorful number, since product of every digit of a sub-sequence are different.
That is, 3 2 4 5 (3*2)=6 (2*4)=8 (4*5)=20, (3*2*4)= 24 (2*4*5)= 40

Given Number : 326
Output : Not Colorful.
326 is not a colorful number as it generates 3 2 6 (3*2)=6 (2*6)=12.

Reference : http://www.careercup.com/question?id=4863869499473920

Approach:

  • Insert all the digits into hast table
  • Create a powerset of digits except empty set (Power Set)
  • Multiply all the digits in the individual powerset and insert into Hash Table.
  • If any point, number already present in the Hash table, return false

Code:


Output:

326 Colorful?? false
3245 Colorful?? true

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

  • Sai Yashodhar Vinay

    here we can do in different way.

    1. if 0 is there then return False;
    Eg: 123065
    2.If numbers are repeating then return False;
    Eg: 13345
    3.no we have to worry about the contribution of composite numbers only.Because prime cannot be split.
    so we have to check for
    6-> check 3,2
    8-> 4,2

%d bloggers like this: