codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum possible size of array with given rules for removing elements | Javascript program to find size of minimum possible array after removing elements according to given rules ; dp [ i ] [ j ] denotes the minimum number of elements left in the subarray arr [ i . . j ] . ; If already evaluated ; If size of array is less than 3 ; Initialize result as the case when first element is separated ( not removed using given rules ) ; Now consider all cases when first element forms a triplet and removed . Check for all possible triplets ( low , i , j ) ; Check if this triplet follows the given rules of removal . And elements between ' low ' and ' i ' , and between ' i ' and ' j ' can be recursively removed . ; Insert value in table and return result ; This function mainly initializes dp table and calls recursive function minSizeRec ; Driver code
let MAX = 1000 ; let dp = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = 0 ; } } function minSizeRec ( arr , low , high , k ) { if ( dp [ low ] [ high ] != - 1 ) { return dp [ low ] [ high ] ; } if ( ( high - low + 1 ) < 3 ) { return high - low + 1 ; } let res = 1 + minSizeRec ( arr , low + 1 , high , k ) ; for ( let i = low + 1 ; i <= high - 1 ; i ++ ) { for ( let j = i + 1 ; j <= high ; j ++ ) { if ( arr [ i ] == ( arr [ low ] + k ) && arr [ j ] == ( arr [ low ] + 2 * k ) && minSizeRec ( arr , low + 1 , i - 1 , k ) == 0 && minSizeRec ( arr , i + 1 , j - 1 , k ) == 0 ) { res = Math . min ( res , minSizeRec ( arr , j + 1 , high , k ) ) ; } } } return ( dp [ low ] [ high ] = res ) ; } function minSize ( arr , n , k ) { for ( let i = 0 ; i < MAX ; i ++ ) { for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return minSizeRec ( arr , 0 , n - 1 , k ) ; } let arr = [ 2 , 3 , 4 , 5 , 6 , 4 ] ; let n = arr . length ; let k = 1 ; document . write ( minSize ( arr , n , k ) ) ;
Find number of solutions of a linear equation of n variables | Recursive function that returns count of solutions for given RHS value and coefficients coeff [ start . . end ] ; Base case ; Initialize count of solutions ; One by subtract all smaller or equal coefficiants and recur ; Driver Code
function countSol ( coeff , start , end , rhs ) { if ( rhs == 0 ) return 1 ; let result = 0 ; for ( let i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } let coeff = [ 2 , 2 , 5 ] ; let rhs = 4 ; let n = coeff . length ; document . write ( countSol ( coeff , 0 , n - 1 , rhs ) ) ;
Maximum weight transformation of a given string | Returns weight of the maximum weight transformation ; Base case ; If this subproblem is already solved ; Don 't make pair, so  weight gained is 1 ; If we can make pair ; If elements are dissimilar , weight gained is 4 ; if elements are similar so for making a pair we toggle any of them . Since toggle cost is 1 so overall weight gain becomes 3 ; save and return maximum of above cases ; Initializes lookup table and calls getMaxRec ( ) ; Create and initialize lookup table ; Call recursive function ; Driver Code
function getMaxRec ( str , i , n , lookup ) { if ( i >= n ) { return 0 ; } if ( lookup [ i ] != - 1 ) { return lookup [ i ] ; } let ans = 1 + getMaxRec ( str , i + 1 , n , lookup ) ; if ( i + 1 < n ) { if ( str [ i ] != str [ i + 1 ] ) { ans = Math . max ( 4 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; } else { ans = Math . max ( 3 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; } } return lookup [ i ] = ans ; } function getMaxWeight ( str ) { let n = str . length ; let lookup = new Array ( n ) ; lookup . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { lookup [ i ] = - 1 ; } return getMaxRec ( str , 0 , str . length , lookup ) ; } let str = " " ; document . write ( " " + " " + str + " " + getMaxWeight ( str ) ) ;
Minimum steps to reach a destination | source -> source vertex step -> value of last step taken dest -> destination vertex ; base cases ; if we go on positive side ; if we go on negative side ; minimum of both cases ; Driver code
function steps ( source , step , dest ) { if ( Math . abs ( source ) > ( dest ) ) return Number . MAX_SAFE_INTEGER ; if ( source == dest ) return step ; let pos = steps ( source + step + 1 , step + 1 , dest ) ; let neg = steps ( source - step - 1 , step + 1 , dest ) ; return Math . min ( pos , neg ) ; } let dest = 11 ; document . write ( " " + dest + " " + steps ( 0 , 0 , dest ) ) ;
Longest Common Substring | DP | Function to find the length of the longest LCS ; Create DP table ; Driver Code ; Function call
function LCSubStr ( s , t , n , m ) { var dp = Array ( 2 ) . fill ( ) . map ( ( ) => Array ( m + 1 ) . fill ( 0 ) ) ; var res = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= m ; j ++ ) { if ( s . charAt ( i - 1 ) == t . charAt ( j - 1 ) ) { dp [ i % 2 ] [ j ] = dp [ ( i - 1 ) % 2 ] [ j - 1 ] + 1 ; if ( dp [ i % 2 ] [ j ] > res ) res = dp [ i % 2 ] [ j ] ; } else dp [ i % 2 ] [ j ] = 0 ; } } return res ; } var X = " " ; var Y = " " ; var m = X . length ; var n = Y . length ; document . write ( LCSubStr ( X , Y , m , n ) ) ;
Longest Common Substring | DP | Javascript program using to find length of the longest common substring recursion ; Returns length of function f or longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Driver code
let X , Y ; function lcs ( i , j , count ) { if ( i == 0 j == 0 ) return count ; if ( X [ i - 1 ] == Y [ j - 1 ] ) { count = lcs ( i - 1 , j - 1 , count + 1 ) ; } count = Math . max ( count , Math . max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) ; return count ; } let n , m ; X = " " ; Y = " " ; n = X . length ; m = Y . length ; document . write ( lcs ( n , m , 0 ) ) ;
Make Array elements equal by replacing adjacent elements with their XOR | Function to check if it is possible to make all the array elements equal using the given operation ; Stores the XOR of all elements of array A [ ] ; Case 1 , check if the XOR of the array A [ ] is 0 ; Maintains the XOR till the current element ; Iterate over the array ; If the current XOR is equal to the total XOR increment the count and initialize current XOR as 0 ; Print Answer ; Driver Code ; Function Call
function possibleEqualArray ( A , N ) { var tot_XOR = 0 ; for ( var i = 0 ; i < N ; i ++ ) { tot_XOR ^= A [ i ] ; } if ( tot_XOR == 0 ) { document . write ( " " ) ; return ; } var cur_XOR = 0 ; var cnt = 0 ; for ( var i = 0 ; i < N ; i ++ ) { cur_XOR ^= A [ i ] ; if ( cur_XOR == tot_XOR ) { cnt ++ ; cur_XOR = 0 ; } } if ( cnt > 2 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var A = [ 0 , 2 , 2 ] ; var N = ( A . length ) ; possibleEqualArray ( A , N ) ;
Count of palindromes that can be obtained by concatenating equal length prefix and substrings | Function to calculate the number of palindromes ; Calculation of Z - array ; Calculation of sigma ( Z [ i ] + 1 ) ; Return the count ; Given String
function countPalindromes ( S ) { var N = S . length ; var Z = Array ( N ) . fill ( 0 ) ; var l = 0 , r = 0 ; for ( var i = 1 ; i < N ; i ++ ) { if ( i <= r ) Z [ i ] = Math . min ( r - i + 1 , Z [ i - l ] ) ; while ( i + Z [ i ] < N && S [ Z [ i ] ] == S [ i + Z [ i ] ] ) { Z [ i ] ++ ; } if ( i + Z [ i ] - 1 > r ) { l = i ; r = i + Z [ i ] - 1 ; } } var sum = 0 ; for ( var i = 0 ; i < Z . length ; i ++ ) { sum += Z [ i ] + 1 ; } return sum ; } var S = " " ; document . write ( countPalindromes ( S ) ) ;
Extract substrings between any pair of delimiters | Function to print strings present between any pair of delimeters ; Stores the indices of ; If opening delimeter is encountered ; If closing delimeter is encountered ; Extract the position of opening delimeter ; Length of substring ; Extract the substring ; Driver Code
function printSubsInDelimeters ( str ) { let dels = [ ] ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { dels . push ( i ) ; } else if ( ( str [ i ] == ' ' ) && ( dels . length > 0 ) ) { let pos = dels [ dels . length - 1 ] ; dels . pop ( ) ; let len = i - 1 - pos ; let ans ; if ( pos < len ) { ans = str . substring ( pos + 1 , len + 1 ) ; } else { ans = str . substring ( pos + 1 , len + pos + 1 ) ; } document . write ( ans + " " ) ; } } } let str = " " ; printSubsInDelimeters ( str ) ;
Print matrix elements from top | Javascript program for the above approach ; Function to traverse the matrix diagonally upwards ; Store the number of rows ; Initialize queue ; Push the index of first element i . e . , ( 0 , 0 ) ; Get the front element ; Pop the element at the front ; Insert the element below if the current element is in first column ; Insert the right neighbour if it exists ; Given vector of vectors arr ; Function call
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function printDiagonalTraversal ( nums ) { let m = nums . length ; let q = [ ] ; q . push ( new pair ( 0 , 0 ) ) ; while ( q . length != 0 ) { let p = q [ 0 ] ; q . shift ( ) ; document . write ( nums [ p . first ] [ p . second ] + " " ) ; if ( p . second == 0 && p . first + 1 < m ) { q . push ( new pair ( p . first + 1 , p . second ) ) ; } if ( p . second + 1 < nums [ p . first ] . length ) q . push ( new pair ( p . first , p . second + 1 ) ) ; } } let arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; printDiagonalTraversal ( arr ) ;
Find original sequence from Array containing the sequence merged many times in order | Function that returns the restored permutation ; Vector to store the result ; Map to mark the elements which are taken in result ; Check if the element is coming first time ; Push in result vector ; Mark it in the map ; Return the answer ; Function to print the result ; Given Array ; Function Call
function restore ( arr , N ) { var result = [ ] ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( ! mp . has ( arr [ i ] ) ) { result . push ( arr [ i ] ) ; mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } } return result ; } function print_result ( result ) { for ( var i = 0 ; i < result . length ; i ++ ) { document . write ( result [ i ] + " " ) ; } } var arr = [ 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 ] ; var N = arr . length ; print_result ( restore ( arr , N ) ) ;
Find original sequence from Array containing the sequence merged many times in order | Function that returns the restored permutation ; Vector to store the result ; Set to insert unique elements ; Check if the element is coming first time ; Push in result vector ; Function to print the result ; Given Array ; Function call
function restore ( arr , N ) { let result = [ ] ; let count1 = 1 ; let s = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { s . add ( arr [ i ] ) ; if ( s . size == count1 ) { result . push ( arr [ i ] ) ; count1 ++ ; } } return result ; } function print_result ( result ) { for ( let i = 0 ; i < result . length ; i ++ ) document . write ( result [ i ] + " " ) ; } let arr = [ 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 ] ; let N = arr . length ; print_result ( restore ( arr , N ) ) ;
Program to print the pattern 1020304017018019020 * * 50607014015016 * * * * 809012013 * * * * * * 10011. . . | Function to find the sum of N integers from 1 to N ; Function to print the given pattern ; Iterate over [ 0 , N - 1 ] ; Sub - Pattern - 1 ; Sub - Pattern - 2 ; Count the number of element in rows and sub - pattern 2 and 3 will have same rows ; Increment Val to print the series 1 , 2 , 3 , 4 , 5 ... ; Finally , add the ( N - 1 ) th element i . e . , 5 and increment it by 1 ; Initial is used to give the initial value of the row in Sub - Pattern 3 ; Sub - Pattern 3 ; Skip printing zero at the last ; Given N ; Function Call
function sum ( n ) { return n * parseInt ( ( n - 1 ) / 2 ) ; } function BSpattern ( N ) { var Val = 0 , Pthree = 0 , cnt = 0 , initial ; var s = " " ; for ( var i = 0 ; i < N ; i ++ ) { cnt = 0 ; if ( i > 0 ) { document . write ( s ) ; s += " " ; } for ( var j = i ; j < N ; j ++ ) { if ( i > 0 ) { cnt ++ ; } document . write ( ++ Val ) ; document . write ( 0 ) ; } if ( i == 0 ) { var Sumbeforelast = sum ( Val ) * 2 ; Pthree = Val + Sumbeforelast + 1 ; initial = Pthree ; } initial = initial - cnt ; Pthree = initial ; for ( var k = i ; k < N ; k ++ ) { document . write ( Pthree ++ ) ; if ( k != N - 1 ) { document . write ( 0 ) ; } } document . write ( " " ) ; } } var N = 5 ; BSpattern ( N ) ;
Check if a number starts with another number or not | Function to check if B is a prefix of A or not ; Convert numbers into Strings ; Find the lengths of Strings s1 and s2 ; Base Case ; Traverse the Strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Given numbers ; Function call ; If B is a prefix of A , then print " Yes "
function checkprefix ( A , B ) { var s1 = A . toString ( ) ; var s2 = B . toString ( ) ; var n1 = s1 . length ; var n2 = s2 . length ; if ( n1 < n2 ) { return false ; } for ( var i = 0 ; i < n2 ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { return false ; } } return true ; } var A = 12345 , B = 12 ; var result = checkprefix ( A , B ) ; if ( result ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if it is possible to reach ( x , y ) from origin in exactly Z steps using only plus movements | Function to check if it is possible to reach ( x , y ) from origin in exactly z steps ; Condition if we can 't reach in Z steps ; Destination povar coordinate ; Number of steps allowed ; Function Call
function possibleToReach ( x , y , z ) { if ( z < Math . abs ( x ) + Math . abs ( y ) || ( z - Math . abs ( x ) - Math . abs ( y ) ) % 2 == 1 ) { document . write ( " " + " " ) ; } else document . write ( " " + " " ) ; } var x = 5 , y = 5 ; var z = 11 ; possibleToReach ( x , y , z ) ;
Number of cycles in a Polygon with lines from Centroid to Vertices | Function to find the Number of Cycles ; Driver code
function nCycle ( N ) { return ( N ) * ( N - 1 ) + 1 ; } let N = 4 ; document . write ( nCycle ( N ) ) ;
Sum of consecutive bit differences of first N non | Javascript program for the above problem ; Recursive function to count the sum of bit differences of numbers from 1 to pow ( 2 , ( i + 1 ) ) - 1 ; base cases ; Recursion call if the sum of bit difference of numbers around i are not calculated ; return the sum of bit differences if already calculated ; Function to calculate the sum of bit differences up to N ; nearest smaller power of 2 ; remaining numbers ; calculate the count of bit diff ; Driver code
let a = new Array ( 65 ) ; a . fill ( 0 ) ; function Count ( i ) { if ( i == 0 ) return 1 ; else if ( i < 0 ) return 0 ; if ( a [ i ] == 0 ) { a [ i ] = ( i + 1 ) + 2 * Count ( i - 1 ) ; return a [ i ] ; } else return a [ i ] ; } function solve ( n ) { let i , sum = 0 ; while ( n > 0 ) { i = parseInt ( Math . log ( n ) / Math . log ( 2 ) , 10 ) ; n = n - parseInt ( Math . pow ( 2 , i ) , 10 ) ; sum = sum + ( i + 1 ) + Count ( i - 1 ) ; } return sum ; } let n = 7 ; document . write ( solve ( n ) ) ;
Count of total Heads and Tails after N flips in a coin | Function to find count of head and tail ; Check if initially all the coins are facing towards head ; Check if initially all the coins are facing towards tail ; Driver code
function count_ht ( s , N ) { var p = [ 0 , 0 ] ; if ( s == ' ' ) { p [ 0 ] = Math . floor ( N / 2.0 ) ; p [ 1 ] = Math . ceil ( N / 2.0 ) ; } else if ( s == ' ' ) { p [ 0 ] = Math . ceil ( N / 2.0 ) ; p [ 1 ] = Math . floor ( N / 2.0 ) ; } return p ; } var C = ' ' ; var N = 5 ; var p = count_ht ( C , N ) ; document . write ( " " + ( p [ 0 ] ) + " " ) ; document . write ( " " + ( p [ 1 ] ) + " " ) ;
Longest palindromic string possible after removal of a substring | Function to find the longest palindrome from the start of the String using KMP match ; Append S ( reverse of C ) to C ; Use KMP algorithm ; Function to return longest palindromic String possible from the given String after removal of any subString ; Initialize three Strings A , B AND F ; Loop to find longest subStrings from both ends which are reverse of each other ; Proceed to third step of our approach ; Remove the subStrings A and B ; Find the longest palindromic subString from beginning of C ; Find the longest palindromic subString from end of C ; Store the maximum of D and E in F ; Find the readonly answer ; Driver Code
function findPalindrome ( C ) { var S = C ; S = reverse ( S ) ; C = C + " " + S ; var n = C . length ; var longestPalindrome = new Array ( n ) . fill ( 0 ) ; longestPalindrome [ 0 ] = 0 ; var len = 0 ; var i = 1 ; while ( i < n ) { if ( C [ i ] === C [ len ] ) { len ++ ; longestPalindrome [ i ] = len ; i ++ ; } else { if ( len !== 0 ) { len = longestPalindrome [ len - 1 ] ; } else { longestPalindrome [ i ] = 0 ; i ++ ; } } } var ans = C . substring ( 0 , longestPalindrome [ n - 1 ] ) ; return ans ; } function findAns ( s ) { var A = " " ; var B = " " ; var F = " " ; var i = 0 ; var j = s . length - 1 ; var len = s . length ; while ( i < j && s [ i ] === s [ j ] ) { i = i + 1 ; j = j - 1 ; } if ( i > 0 ) { A = s . substring ( 0 , i ) ; B = s . substring ( len - i , len ) ; } if ( len > 2 * i ) { var C = s . substring ( i , i + ( s . length - 2 * i ) ) ; var D = findPalindrome ( C ) ; C = reverse ( C ) ; var E = findPalindrome ( C ) ; if ( D . length > E . length ) { F = D ; } else { F = E ; } } var answer = A + F + B ; return answer ; } function reverse ( input ) { var a = input . split ( " " ) ; var r = a . length - 1 ; for ( var l = 0 ; l < r ; l ++ , r -- ) { var temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } var str = " " ; document . write ( findAns ( str ) ) ;
Find Nth term of the series 2 , 3 , 10 , 15 , 26. ... | Function to find Nth term ; Nth term ; Driver code
function nthTerm ( N ) { let nth = 0 ; if ( N % 2 == 1 ) nth = ( N * N ) + 1 ; else nth = ( N * N ) - 1 ; return nth ; } let N = 5 ; document . write ( nthTerm ( N ) ) ;
Find the Nth term in series 12 , 35 , 81 , 173 , 357 , ... | Function to find Nth term ; Nth term ; Driver code
function nthTerm ( N ) { let nth = 0 , first_term = 12 ; nth = ( first_term * ( Math . pow ( 2 , N - 1 ) ) ) + 11 * ( ( Math . pow ( 2 , N - 1 ) ) - 1 ) ; return nth ; } let N = 5 ; document . write ( nthTerm ( N ) ) ;
Find Nth term of the series 4 , 2 , 2 , 3 , 6 , ... | Function to find Nth term ; Nth term ; Driver code
function nthTerm ( N ) { let nth = 0 , first_term = 4 ; let pi = 1 , po = 1 ; let n = N ; while ( n > 1 ) { pi *= n - 1 ; n -- ; po *= 2 ; } nth = ( first_term * pi ) / po ; return nth ; } let N = 5 ; document . write ( nthTerm ( N ) ) ;
Find the final number obtained after performing the given operation | Function to return the final number obtained after performing the given operation ; Find the gcd of the array elements ; Driver code
function finalNum ( arr , n ) { var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { result = __gcd ( result , arr [ i ] ) ; } return result ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var arr = [ 3 , 9 , 6 , 36 ] ; var n = arr . length ; document . write ( finalNum ( arr , n ) ) ;
Check whether all the substrings have number of vowels atleast as that of consonants | Function that returns true if character ch is a vowel ; Compares two integers according to their digit sum ; Check if there are two consecutive consonants ; Check if there is any vowel surrounded by two consonants ; Driver code
function isVowel ( ch ) { switch ( ch ) { case " " : case " " : case " " : case " " : case " " : return true ; } return false ; } function isSatisfied ( str , n ) { for ( var i = 1 ; i < n ; i ++ ) { if ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i - 1 ] ) ) { return false ; } } for ( var i = 1 ; i < n - 1 ; i ++ ) { if ( isVowel ( str [ i ] ) && ! isVowel ( str [ i - 1 ] ) && ! isVowel ( str [ i + 1 ] ) ) { return false ; } } return true ; } var str = " " ; var n = str . length ; if ( isSatisfied ( str . split ( " " ) , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print the longest prefix of the given string which is also the suffix of the same string | Returns length of the longest prefix which is also suffix and the two do not overlap . This function mainly is copy of computeLPSArray ( ) in KMP Algorithm ; lps [ 0 ] is always 0 ; Length of the previous longest prefix suffix ; Loop to calculate lps [ i ] for i = 1 to n - 1 ; This is tricky . Consider the example . AAACAAAA and i = 7. The idea is similar to search step . ; Also , note that we do not increment i here ; If len = 0 ; Since we are looking for non overlapping parts ; Function that returns the prefix ; Get the length of the longest prefix ; Stores the prefix ; Traverse and add characters ; Returns the prefix ; Driver code
function LengthlongestPrefixSuffix ( s ) { var n = s . length ; var lps = Array . from ( { length : n } , ( _ , i ) => 0 ) ; lps [ 0 ] = 0 ; var len = 0 ; var i = 1 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } var res = lps [ n - 1 ] ; return ( res > n / 2 ) ? n / 2 : res ; } function longestPrefixSuffix ( s ) { var len = LengthlongestPrefixSuffix ( s ) ; var prefix = " " ; for ( var i = 0 ; i < len ; i ++ ) prefix += s . charAt ( i ) ; return prefix ; } var s = " " ; var ans = longestPrefixSuffix ( s ) ; if ( ans == " " ) document . write ( " " ) ; else document . write ( ans ) ;
Print a number as string of ' A ' and ' B ' in lexicographic order | Function to calculate number of characters in corresponding string of ' A ' and ' B ' ; Since the minimum number of characters will be 1 ; Calculating number of characters ; Since k length string can represent at most pow ( 2 , k + 1 ) - 2 that is if k = 4 , it can represent at most pow ( 2 , 4 + 1 ) - 2 = 30 so we have to calculate the length of the corresponding string ; return the length of the corresponding string ; Function to print corresponding string of ' A ' and ' B ' ; Find length of string ; Since the first number that can be represented by k length string will be ( pow ( 2 , k ) - 2 ) + 1 and it will be AAA ... A , k times , therefore , N will store that how much we have to print ; At a particular time , we have to decide whether we have to print ' A ' or ' B ' , this can be check by calculating the value of pow ( 2 , k - 1 ) ; Print new line ; Driver code
function no_of_characters ( M ) { let k = 1 ; while ( true ) { if ( Math . pow ( 2 , k + 1 ) - 2 < M ) k ++ ; else break ; } return k ; } function print_string ( M ) { let k , num , N ; k = no_of_characters ( M ) ; N = M - ( Math . pow ( 2 , k ) - 2 ) ; while ( k > 0 ) { num = Math . pow ( 2 , k - 1 ) ; if ( num >= N ) document . write ( " " ) ; else { document . write ( " " ) ; N -= num ; } k -- ; } document . write ( " " ) ; } let M ; M = 30 ; print_string ( M ) ; M = 55 ; print_string ( M ) ; M = 100 ; print_string ( M ) ;
Replace two substrings ( of a string ) with each other | Function to return the resultant string ; Iterate through all positions i ; Current sub - string of length = len ( A ) = len ( B ) ; If current sub - string gets equal to A or B ; Update S after replacing A ; Update S after replacing B ; Return the updated string ; Driver code
function updateString ( S , A , B ) { let l = A . length ; for ( let i = 0 ; i + l <= S . length ; i ++ ) { let curr = S . substring ( i , i + l ) ; if ( curr == A ) { let new_string = S . substring ( 0 , i ) + B + S . substring ( i + l ) ; S = new_string ; i += l - 1 ; } else { let new_string = S . substring ( 0 , i ) + A + S . substring ( i + l ) ; S = new_string ; i += l - 1 ; } } return S ; } let S = " " ; let A = " " ; let B = " " ; document . write ( updateString ( S , A , B ) ) ;