codeparrot/xlcost-text-to-code huggingface dataset:
Length of the longest substring consisting only of vowels in non | Function to find length of the longest substring consisting only of vowels in non - increasing order ; Stores all vowels in decreasing order ; Stores current index of array ch [ ] ; Stores the result ; Stores the count of current substring ; Declare a HashSet to store the vowels ; Traverse the string , S ; If S [ i ] is equal to ch [ j ] ; Increment count by 1 ; Add S [ i ] in the mp ; If length of mp is 5 , update res ; Else if j + 1 is less than 5 and S [ i ] is equal to ch [ j + 1 ] ; Add the S [ i ] in the mp ; Increment count by 1 ; Increment j by 1 ; If length of mp is 5 , update res ; Clear the mp ; If S [ i ] is ' u ' ; Add S [ i ] in the mp ; Update j and assign 1 to count ; Else assign 0 to j and count ; Return the result ; Given Input ; Function Call
function count ( S , N ) { let ch = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let j = 0 ; let res = 0 ; let count = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ch [ j ] ) { ++ count ; mp . set ( S [ i ] , " " ) ; if ( mp . size == 5 ) { res = Math . max ( res , count ) ; } } else if ( j + 1 < 5 && S [ i ] == ch [ j + 1 ] ) { mp . set ( S [ i ] , " " ) ; ++ count ; j ++ ; if ( mp . size == 5 ) { res = Math . max ( res , count ) ; } } else { mp . clear ( ) ; if ( S [ i ] == ' ' ) { mp . set ( ' ' , " " ) ; j = 0 ; count = 1 ; } else { j = 0 ; count = 0 ; } } } return res ; } let S = " " ; let N = S . length ; document . write ( count ( S , N ) ) ;
Maximize length of subsequence consisting of single distinct character possible by K increments in a string | Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of s ; Sort the given string sort ( S . begin ( ) , S . end ( ) ) ; convert input string to char array ; sort tempArray ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver code
function maxSubsequenceLen ( s , K ) { var N = s . length ; var start = 0 , end = 0 ; var S = s . split ( ' ' ) ; S . sort ( ) ; var ans = Number . MIN_VALUE , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; while ( sum + K < ( S [ end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; start ++ ; } ans = Math . max ( ans , end - start + 1 ) ; } document . write ( ans ) ; } var S = " " ; var K = 1 ; maxSubsequenceLen ( S , K ) ;
Check if a given integer is the product of K consecutive integers | Function to check if N can be expressed as the product of K consecutive integers ; Stores the K - th root of N ; Stores the product of K consecutive integers ; Traverse over the range [ 1 , K ] ; Update the product ; If product is N , then return " Yes " ; Otherwise , traverse over the range [ 2 , Kthroot ] ; Update the value of product ; If product is equal to N ; Otherwise , return " No " ; Driver Code
function checkPro ( n , k ) { var exp = 1.0 / k ; var KthRoot = parseInt ( Math . pow ( n , exp ) ) ; var product = 1 ; for ( i = 1 ; i < k + 1 ; i ++ ) { product = product * i ; } if ( product == n ) return " " ; else { for ( j = 2 ; j < KthRoot + 1 ; j ++ ) { product = product * ( j + k - 1 ) ; product = product / ( j - 1 ) ; if ( product == n ) return " " ; } } return " " ; } var N = 210 ; var K = 3 ; document . write ( checkPro ( N , K ) ) ;
Convert a Binary String to another by flipping prefixes minimum number of times | Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Function to reverse a [ ] from start to end ; Driver Code
function minOperations ( s1 , s2 , n ) { var a = s1 . split ( " " ) ; var b = s2 . split ( " " ) ; var ops = [ ] ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] !== b [ i ] ) { if ( a [ 0 ] === b [ i ] ) { ops . push ( 1 ) ; a [ 0 ] = a [ 0 ] === " " ? " " : " " ; } reverse ( a , 0 , i ) ; for ( var j = 0 ; j <= i ; j ++ ) { a [ j ] = a [ j ] === " " ? " " : " " ; } ops . push ( i + 1 ) ; } } document . write ( ops . length + " " ) ; for ( const x of ops ) { document . write ( x + " " ) ; } } function reverse ( a , start , end ) { while ( start < end ) { var temp = a [ start ] ; a [ start ] = a [ end ] ; a [ end ] = temp ; start ++ ; end -- ; } } var a = " " , b = " " ; var N = a . length ; minOperations ( a , b , N ) ;
Count possible removals to make absolute difference between the sum of odd and even indexed elements equal to K | Function to check if difference between the sum of odd and even indexed elements after removing the first element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to check if difference between the sum of odd and even indexed elements after removing the second element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to count number of elements to be removed to make sum of differences between odd and even indexed elements equal to K ; Size of given array ; Base Conditions ; Stores prefix and suffix sums ; Base assignments ; Store prefix sums of even indexed elements ; Store prefix sums of odd indexed elements ; Similarly , store suffix sums of elements at even and odd indices ; Stores the count of possible removals ; Traverse and remove the ith element ; If the current element is excluded , then previous index ( i - 1 ) points to ( i + 2 ) and ( i - 2 ) points to ( i + 1 ) ; Find count when 0 th element is removed ; Find count when 1 st element is removed ; Count gives the required answer ; Driver Code ; Function call
function findCount0th ( arr , N , K ) { let oddsum = 0 , evensum = 0 ; for ( let i = 1 ; i < N ; i += 2 ) { oddsum += arr [ i ] ; } for ( let i = 2 ; i < N ; i += 2 ) { evensum += arr [ i ] ; } if ( Math . abs ( oddsum - evensum ) == K ) return 1 ; else return 0 ; } function findCount1st ( arr , N , K ) { let evensum = arr [ 0 ] , oddsum = 0 ; for ( let i = 3 ; i < N ; i += 2 ) { evensum += arr [ i ] ; } for ( let i = 2 ; i < N ; i += 2 ) { oddsum += arr [ i ] ; } if ( Math . abs ( oddsum - evensum ) == K ) return 1 ; else return 0 ; } function countTimes ( arr , K ) { let N = arr . length ; if ( N == 1 ) return 1 ; if ( N < 3 ) return 0 ; if ( N == 3 ) { let cnt = 0 ; cnt += ( Math . abs ( arr [ 0 ] - arr [ 1 ] ) == K ? 1 : 0 ) + ( Math . abs ( arr [ 2 ] - arr [ 1 ] ) == K ? 1 : 0 ) + ( Math . abs ( arr [ 0 ] - arr [ 2 ] ) == K ? 1 : 0 ) ; return cnt ; } let prefix = new Array ( N + 2 ) ; let suffix = new Array ( N + 2 ) ; for ( let i = 0 ; i < N + 2 ; i ++ ) { prefix [ i ] = 0 ; } for ( let i = 0 ; i < N + 2 ; i ++ ) { suffix [ i ] = 0 ; } prefix [ 0 ] = arr [ 0 ] ; prefix [ 1 ] = arr [ 1 ] ; suffix [ N - 1 ] = arr [ N - 1 ] ; suffix [ N - 2 ] = arr [ N - 2 ] ; for ( let i = 2 ; i < N ; i += 2 ) { prefix [ i ] = arr [ i ] + prefix [ i - 2 ] ; } for ( let i = 3 ; i < N ; i += 2 ) { prefix [ i ] = arr [ i ] + prefix [ i - 2 ] ; } for ( let i = N - 3 ; i >= 0 ; i -= 2 ) { suffix [ i ] = arr [ i ] + suffix [ i + 2 ] ; } for ( let i = N - 4 ; i >= 0 ; i -= 2 ) { suffix [ i ] = arr [ i ] + suffix [ i + 2 ] ; } let count = 0 ; for ( let i = 2 ; i < N ; i ++ ) { if ( Math . abs ( prefix [ i - 1 ] + suffix [ i + 2 ] - prefix [ i - 2 ] - suffix [ i + 1 ] ) == K ) { count ++ ; } } count += findCount0th ( arr , N , K ) ; count += findCount1st ( arr , N , K ) ; return count ; } let arr = [ 1 , 2 , 4 , 5 , 6 ] ; let K = 2 ; document . write ( countTimes ( arr , K ) ) ;
Replace ' ? ' in a string such that no two adjacent characters are same | Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given String ; If the first character is ' ? ' ; Traverse the String [ 1 , N - 1 ] ; If the current character is ' ? ' ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is ' ? ' ; Change character ; Check with previous character ; Return the resultant String ; Given String S ; Function Call
function changeString ( S ) { let s = S . split ( " " ) ; let N = S . length ; if ( s [ 0 ] == ' ' ) { s [ 0 ] = ' ' ; if ( s [ 0 ] == s [ 1 ] ) { s [ 0 ] = String . fromCharCode ( s [ 0 ] . charCodeAt ( 0 ) + 1 ) ; } } for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ' ' ) { s [ i ] = ' ' ; if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } if ( s [ i ] == s [ i + 1 ] ) { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } } } if ( s [ N - 1 ] == ' ' ) { s [ N - 1 ] = ' ' ; if ( s [ N - 1 ] == s [ N - 2 ] ) { s [ N - 1 ] ++ ; } } let ans = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { ans += s [ i ] ; } return ans ; } let S = " " ; document . write ( changeString ( S ) ) ;
Check if a given string is a Reverse Bitonic String or not | Function to check if the given string is reverse bitonic ; Check for decreasing sequence ; If end of string has been reached ; Check for increasing sequence ; If the end of string hasn 't been reached ; If the string is reverse bitonic ; Driver Code
function checkReverseBitonic ( s ) { var i , j ; for ( i = 1 ; i < s . length ; i ++ ) { if ( s [ i ] < s [ i - 1 ] ) continue ; if ( s [ i ] >= s [ i - 1 ] ) break ; } if ( i == s . length - 1 ) return 1 ; for ( j = i + 1 ; j < s . length ; j ++ ) { if ( s [ j ] > s [ j - 1 ] ) continue ; if ( s [ j ] <= s [ j - 1 ] ) break ; } i = j ; if ( i != s . length ) return 0 ; return 1 ; } var s = " " ; ( checkReverseBitonic ( s ) == 1 ) ? document . write ( " " ) : document . write ( " " ) ;
Smallest number whose square has N digits | Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver Code
function smallestNum ( N ) { x = Math . pow ( 10.0 , ( N - 1 ) / 2.0 ) ; return Math . ceil ( x ) ; } let N = 4 ; document . write ( smallestNum ( N ) ) ;
Count of K | Function to to count the number of K - countdowns for multiple queries ; flag which stores the current value of value in the countdown ; count of K - countdowns ; Loop to iterate over the elements of the array ; condition check if the elements of the array is equal to K ; condition check if the elements of the array is in continuous order ; condition check if the elements of the array are not in continuous order ; condition check to increment the counter if the there is a K - countdown present in the array ; returning the count of K - countdowns ; Driver code ; Function Call
function countKCountdown ( arr , N , K ) { var flag = - 1 ; var count = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) flag = K ; if ( arr [ i ] == flag ) flag -- ; else flag = - 1 ; if ( flag == 0 ) count ++ ; } return count ; } var N = 8 ; var K = 3 ; var arr = [ 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 ] ; document . write ( countKCountdown ( arr , N , K ) ) ;
Convert the given RGB color code to Hex color code | function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver Code
function decToHexa ( n ) { let hexaDeciNum = Array . from ( { length : 2 } , ( _ , i ) => 0 ) ; let i = 0 ; while ( n != 0 ) { let temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = String . fromCharCode ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = String . fromCharCode ( temp + 55 ) ; i ++ ; } n = Math . floor ( n / 16 ) ; } let hexCode = " " ; if ( i == 2 ) { hexCode += hexaDeciNum [ 0 ] ; hexCode += hexaDeciNum [ 1 ] ; } else if ( i == 1 ) { hexCode = " " ; hexCode += hexaDeciNum [ 0 ] ; } else if ( i == 0 ) hexCode = " " ; return hexCode ; } function convertRGBtoHex ( R , G , B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { let hexCode = " " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " " ; } let R = 0 , G = 0 , B = 0 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 255 ; G = 255 ; B = 255 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 25 ; G = 56 ; B = 123 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 2 ; G = 3 ; B = 4 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ; R = 255 ; G = 255 ; B = 256 ; document . write ( convertRGBtoHex ( R , G , B ) + " " ) ;
Check whether an array can be made strictly decreasing by modifying at most one element | Function that returns true if the array can be made strictly decreasing with at most one change ; To store the number of modifications required to make the array strictly decreasing ; Check whether the last element needs to be modify or not ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; If more than 1 modification is required ; Driver code
function check ( arr , n ) { let modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( let i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = parseInt ( ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 , 10 ) ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } let arr = [ 10 , 5 , 11 , 3 ] ; let n = arr . length ; if ( check ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to build a DFA to accept strings that start and end with same character | Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
function q1 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q2 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q3 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q4 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q0 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } var s = " " ; q0 ( s , 0 ) ;
Longest prefix in a string with highest frequency | Function to find Longest prefix string with the highest frequency ; storing all indices where first element is found ; if the first letter in the string does not occur again then answer will be the whole string ; loop till second appearance of the first element ; check one letter after every stored index ; If there is no mismatch we move forward ; otherwise we stop ; Driver Code
function prefix ( str ) { let k = 1 , j ; let n = str . length ; let g = [ ] ; let flag = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) { g . push ( i ) ; flag = 1 ; } } if ( flag == 0 ) { document . write ( ( str . join ( " " ) ) ) ; } else { let len = g . length ; while ( k < g [ 0 ] ) { let cnt = 0 ; for ( j = 0 ; j < len ; j ++ ) { if ( ( g [ j ] + k ) < n && str [ g [ j ] + k ] == str [ k ] ) { cnt ++ ; } } if ( cnt == len ) { k ++ ; } else { break ; } } for ( let i = 0 ; i < k ; i ++ ) { document . write ( str [ i ] ) ; } document . write ( " " ) ; } } let str = " " ; prefix ( str ) ;
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers ; To store the permutation ; For sequence 1 2 3. . . ; For sequence N , N - 1 , N - 2. . . ; Flag is used to alternate between the above if else statements ; If last element added was r + 1 ; If last element added was l - 1 ; Print the permutation ; Driver code
function printPermutation ( N , K ) { var res = [ ] ; var l = 1 , r = N , flag = 0 ; for ( var i = 0 ; i < K ; i ++ ) { if ( ! flag ) { res . push ( l ) ; l ++ ; } else { res . push ( r ) ; r -- ; } flag ^= 1 ; } if ( ! flag ) { for ( var i = r ; i >= l ; i -- ) res . push ( i ) ; } else { for ( var i = l ; i <= r ; i ++ ) res . push ( i ) ; } for ( var i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } } var N = 10 , K = 4 ; printPermutation ( N , K ) ;
Find Nth term of the series 1 , 8 , 54 , 384. . . | calculate factorial of N ; calculate Nth term of series ; Driver Function
function fact ( N ) { let i , product = 1 ; for ( i = 1 ; i <= N ; i ++ ) product = product * i ; return product ; } function nthTerm ( N ) { return ( N * N ) * fact ( N ) ; } let N = 4 ; document . write ( nthTerm ( N ) ) ;
Rabin | d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; Hash value for pattern Hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern . If the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative value of t , converting it to positive ; Driver code ; A prime number ; Function Call
let d = 256 ; function search ( pat , txt , q ) { let M = pat . length ; let N = txt . length ; let i , j ; let p = 0 ; let t = 0 ; let h = 1 ; for ( i = 0 ; i < M - 1 ; i ++ ) h = ( h * d ) % q ; for ( i = 0 ; i < M ; i ++ ) { p = ( d * p + pat [ i ] . charCodeAt ( ) ) % q ; t = ( d * t + txt [ i ] . charCodeAt ( ) ) % q ; } for ( i = 0 ; i <= N - M ; i ++ ) { if ( p == t ) { for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) break ; } if ( j == M ) document . write ( " " + i + " " ) ; } if ( i < N - M ) { t = ( d * ( t - txt [ i ] . charCodeAt ( ) * h ) + txt [ i + M ] . charCodeAt ( ) ) % q ; if ( t < 0 ) t = ( t + q ) ; } } } let txt = " " ; let pat = " " ; let q = 101 ; search ( pat , txt , q ) ;
Find the final string after flipping bits at the indices that are multiple of prime factors of array elements | JavaScript program for the above approach ; Stores smallest prime factor ; Function to find the smallest prime factor for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Function to find all the distinct prime factors of the given number x ; Find the prime factors for X ; Find the spf [ ] of x ; Return the prime factors for x ; Function to find string after flipping the characters at indices of prime factors of array elements arr [ ] ; Precalculating Smallest Prime Factor ; Stores the frequency of each prime factor ; Iterate over all elements of the array arr [ ] ; Stores prime factors of arr [ i ] ; Increment the frequency of all prime factors of arr [ i ] ; Iterate over all elements of the array frequency [ ] ; If frequency [ i ] is odd ; Flip bits of all indices that are multiple of i ; Return Answer ; Driver Code
const MAXN = 100001 ; let spf = new Array ( MAXN ) . fill ( 0 ) ; const sieve = ( ) => { spf [ 1 ] = 1 ; for ( let i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( let i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( let i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAXN ; j += i ) { if ( spf [ j ] == j ) spf [ j ] = i ; } } } } const getFactorization = ( x ) => { let ret = [ ] ; while ( x != 1 ) { ret . push ( spf [ x ] ) ; let value = spf [ x ] ; while ( x % value == 0 ) x = parseInt ( x / value ) ; } return ret ; } const flipString = ( S , arr , M ) => { sieve ( ) ; let frequency = new Array ( MAXN ) . fill ( 0 ) ; for ( let i = 0 ; i < M ; i ++ ) { let primeFactors = getFactorization ( arr [ i ] ) ; for ( let factors in primeFactors ) { frequency [ primeFactors [ factors ] ] ++ ; frequency [ factors ] %= 2 ; } } let N = S . length ; for ( let i = 0 ; i < MAXN ; i ++ ) { if ( frequency [ i ] & 1 ) { for ( let j = i ; j <= N ; j += i ) { S [ j - 1 ] = ( S [ j - 1 ] == ' ' ? ' ' : ' ' ) ; } } } return S ; } let S = " " ; S = S . split ( " " ) ; let arr = [ 2 , 4 , 6 ] ; let M = arr . length ; document . write ( flipString ( S , arr , M ) . join ( ' ' ) ) ;
Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
function isOnesGreater ( S , N ) { let cnt0 = 0 ; let cnt1 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) cnt1 ++ ; else cnt0 ++ ; } for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { if ( i > 0 && S [ i - 1 ] == ' ' ) { S [ i - 1 ] = ' ' ; cnt0 -- ; } else if ( i < N && S [ i + 1 ] == ' ' ) { S [ i + 1 ] = ' ' ; cnt0 -- ; } } } if ( cnt1 > cnt0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let S = " " ; let N = S . length ; isOnesGreater ( S , N ) ;