Count of subarrays of an Array having all unique digits | Dynamic programming table ; Function to obtain the mask for any integer ; Function to count the number of ways ; Subarray must not be empty ; If subproblem has been solved ; Excluding this element in the subarray ; If there are no common digits then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; initializing dp ; Driver code
var dp = Array . from ( Array ( 5000 ) , ( ) => Array ( ( 1 << 10 ) + 5 ) . fill ( - 1 ) ) ; function getmask ( val ) { var mask = 0 ; if ( val == 0 ) return 1 ; while ( val ) { var d = val % 10 ; mask |= ( 1 << d ) ; val = parseInt ( val / 10 ) ; } return mask ; } function countWays ( pos , mask , a , n ) { if ( pos == n ) return ( mask > 0 ? 1 : 0 ) ; if ( dp [ pos ] [ mask ] != - 1 ) return dp [ pos ] [ mask ] ; var count = 0 ; count = count + countWays ( pos + 1 , mask , a , n ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { var new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; count = count + countWays ( pos + 1 , new_mask , a , n ) ; } return dp [ pos ] [ mask ] = count ; } function numberOfSubarrays ( a , n ) { dp = Array . from ( Array ( 5000 ) , ( ) => Array ( ( 1 << 10 ) + 5 ) . fill ( - 1 ) ) ; return countWays ( 0 , 0 , a , n ) ; } var N = 4 ; var A = [ 1 , 12 , 23 , 34 ] ; document . write ( numberOfSubarrays ( A , N ) ) ;