Fascinating Number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not
< ? php function isFascinating ( $ num ) { $ freq = array_fill ( 0 , 10 , NULL ) ; $ val = " " . $ num . ( $ num * 2 ) . ( $ num * 3 ) ; for ( $ i = 0 ; $ i < strlen ( $ val ) ; $ i ++ ) { $ digit = $ val [ $ i ] - '0' ; if ( $ freq [ $ digit ] > 0 && $ digit != 0 ) return false ; else $ freq [ $ digit ] ++ ; } for ( $ i = 1 ; $ i < 10 ; $ i ++ ) { if ( $ freq [ $ i ] == 0 ) return false ; } return true ; } $ num = 192 ; if ( $ num < 100 ) echo " No " ; else { $ ans = isFascinating ( $ num ) ; if ( $ ans ) echo " Yes " ; else echo " No " ; } ? >