Largest Prime Number possible from a subsequence of a Binary String | Function to check if a number is prime or not ; Return not prime ; If prime return true ; Function to find the largest prime number possible from a subsequence ; Stores pairs of subsequences and their respective decimal value ; Stores the answer ; Traverse the string ; Stores the size of the vector ; Traverse the vector ; Extract the current pair ; Get the binary string from the pair ; Stores equivalent decimal values ; If the current character is '1' ; Add the character to the subsequence ; Update the value by left shifting the current value and adding 1 to it ; If s [ i ] = = '0' ; Add the character to the subsequence ; Update the value by left shifting the current value and adding 0 to it ; Store the subsequence in the vector ; Check if the decimal representation of current subsequence is prime or not ; If prime ; Update the answer with the largest one ; If no prime number could be obtained ; Input String
function isPrime ( x ) { if ( x <= 1 ) return false ; for ( let i = 2 ; i * i <= x ; i ++ ) { if ( i * i > x ) { break } if ( x % i == 0 ) return false ; } return true ; } function largestPrime ( s ) { let vec = [ [ " " , 0 ] ] ; let ans = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { let n = vec . length ; for ( let j = 0 ; j < n ; j ++ ) { let temp = vec [ j ] ; let str = temp [ 0 ] ; let val = temp [ 1 ] ; if ( s [ i ] == ' ' ) { temp [ 0 ] = str + ' ' ; temp [ 1 ] = ( ( val << 1 ) + 1 ) ; } else { temp [ 0 ] = str + ' ' ; temp [ 1 ] = ( ( val << 1 ) + 0 ) ; } vec . push ( temp ) ; let check = temp [ 1 ] ; if ( isPrime ( check ) ) { ans = Math . max ( ans , check ) ; break } } } if ( ans == 0 ) document . write ( - 1 + " " ) ; else document . write ( ans + " " ) ; } let s = " " ; largestPrime ( s ) ;