File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * InterviewBit
3
+ * For Given Number N find if its COLORFUL number or not
4
+ *
5
+ * This solution got 60 points
6
+ * Problem link: https://www.interviewbit.com/problems/colorful-number
7
+ */
8
+
9
+ /**
10
+ * Check if a number is colorful
11
+ * @param {number } A Number to be checked
12
+ */
13
+ function colorful ( A ) {
14
+ const digits = getDigits ( A ) ;
15
+ const prods = new Set ( ) ;
16
+ let chunkSize = 1 ;
17
+ while ( chunkSize <= digits . length ) {
18
+ let i = 0 ;
19
+ while ( i + chunkSize <= digits . length ) {
20
+ const getProd = digits . slice ( i , i + chunkSize )
21
+ . reduce ( ( a , b ) => a * b , 1 ) ;
22
+ if ( prods . has ( getProd ) ) {
23
+ return 0 ;
24
+ } else {
25
+ prods . add ( getProd ) ;
26
+ }
27
+ i ++ ;
28
+ }
29
+ chunkSize ++ ;
30
+ }
31
+ return 1 ;
32
+ }
33
+
34
+ /**
35
+ * Get digits of a numnber
36
+ * @param {number } n Number to be decomposed
37
+ */
38
+ function getDigits ( n ) {
39
+ const digits = [ ] ;
40
+ while ( n > 0 ) {
41
+ digits . push ( n % 10 )
42
+ n = parseInt ( n / 10 ) ;
43
+ }
44
+ return digits ;
45
+ }
You can’t perform that action at this time.
0 commit comments