Check whether an array of strings can correspond to a particular number X | Function to find the maximum base possible for the number N ; Function to find the decimal equivalent of the number ; Condition to check if the number is convertible to another base ; Function to check that the array can correspond to a number X ; counter to count the numbers those are convertible to X ; Loop to iterate over the array ; Convert the current String to every base for checking whether it will correspond to X from any base ; Condition to check if every number of the array can be converted to X ; Driver Code ; The set of Strings in base from [ 2 , 36 ]
function val ( c ) { if ( c >= ' ' && c <= ' ' ) return c - ' ' ; else return c - ' ' + 10 ; } function toDeci ( str , base ) { let len = str . length ; let power = 1 ; let num = 0 ; let i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { return - 1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } function checkCorrespond ( str , x ) { let counter = 0 ; let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 2 ; j <= 36 ; j ++ ) { if ( toDeci ( str [ i ] , j ) == x ) { counter ++ ; break ; } } } if ( counter == n ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let x = 16 ; let str = [ " " , " " , " " ] ; checkCorrespond ( str , x ) ;