algorithms.tutorialhorizon.com

Colorful Numbers | Algorithms

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 : 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:Run This Code Run