Check if the product of every contiguous subsequence is different or not in a number | Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Driver code
function productsDistinct ( N ) { var s = " " ; while ( N ) { s += String . fromCharCode ( N % 10 + ' ' . charCodeAt ( 0 ) ) ; N = parseInt ( N / 10 ) ; } s = s . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; var sz = s . length ; var se = new Set ( ) ; for ( var i = 0 ; i < sz ; i ++ ) { var product = 1 ; for ( var j = i ; j < sz ; j ++ ) { product *= ( s [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; if ( se . has ( product ) ) return false ; else se . add ( product ) ; } } return true ; } var N = 2345 ; if ( productsDistinct ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;