Be the first user to complete this post

  • 0
Add to List

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 for an empty set (Power Set)
  • Multiply all the digits in the individual powerset and insert them into Hash Table.
  • If any point, number already present in the Hash table, return false

Code:


Output:

326 Colorful?? false
3245 Colorful?? true



Also Read: