codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Deci | Function to find the count of minimum Deci - Binary numbers required to obtain S ; Stores the minimum count ; Iterate over the string s ; Convert the char to its equivalent integer ; If current character is the maximum so far ; Update the maximum digit ; Print the required result ; Driver code
function minimum_deci_binary_number ( s ) { let m = Number . MIN_VALUE ; for ( let i = 0 ; i < s . length ; i ++ ) { let temp = s [ i ] - ' ' ; if ( temp > m ) { m = temp ; } } return m ; } let S = " " ; document . write ( minimum_deci_binary_number ( S ) ) ;
Make all the elements of array odd by incrementing odd | Function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; Stores the minimum number of operations required ; Iterate over even - indices ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment number of operations ; Iterate over odd indexed positions of arr [ ] ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment the number of operations ; Prlet the number of operations ; Driver Code ; Function Call
function minOperations ( arr , n ) { let flips = 0 ; for ( let i = 0 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } for ( let i = 1 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } document . write ( flips ) ; } let arr = [ 2 , 3 , 4 , 3 , 5 , 3 , 2 ] ; let N = arr . length ; minOperations ( arr , N ) ;
Minimum substring reversals required to make given Binary String alternating | Function to count the minimum number of substrings required to be reversed to make the string S alternating ; Store count of consecutive pairs ; Stores the count of 1 s and 0 s ; Traverse through the string ; Increment 1 s count ; Increment 0 s count ; Increment K if consecutive same elements are found ; Increment 1 s count ; else Increment 0 s count ; Check if it is possible or not ; Otherwise , print the number of required operations ; Driver code ; Function Call
function minimumReverse ( s , n ) { let k = 0 , l = 0 ; let sum1 = 0 , sum0 = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) sum1 ++ ; else sum0 ++ ; if ( s [ i ] == s [ i - 1 ] && s [ i ] == ' ' ) k ++ ; else if ( s [ i ] == s [ i - 1 ] && s [ i ] == ' ' ) l ++ ; } if ( s [ 0 ] == ' ' ) sum1 ++ ; sum0 ++ ; if ( Math . abs ( sum1 - sum0 ) > 1 ) return - 1 ; return Math . max ( k , l ) ; } let S = " " ; let N = S . length ; document . write ( minimumReverse ( S , N ) ) ;
Count pairs with Even Product from two given arrays | Function to count pairs ( arr [ i ] , brr [ j ] ) whose product is an even number ; Stores count of odd numbers in arr [ ] ; Stores count of odd numbers in brr [ ] ; Traverse the array , arr [ ] ; If arr [ i ] is an odd number ; Update cntOddArr ; Traverse the array , brr [ ] ; If brr [ i ] is an odd number ; Update cntOddArr ; Return pairs whose product is an even number ; Driver Code
function cntPairsletwoArray ( arr , brr , N , M ) { let cntOddArr = 0 ; let cntOddBrr = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { cntOddArr += 1 ; } } for ( let i = 0 ; i < M ; i ++ ) { if ( brr [ i ] % 2 == 1 ) { cntOddBrr += 1 ; } } return ( N * M ) - ( cntOddArr * cntOddBrr ) ; } let arr = [ 1 , 2 , 3 ] ; let N = arr . length ; let brr = [ 1 , 2 ] ; let M = brr . length ; document . write ( cntPairsletwoArray ( arr , brr , N , M ) ) ;
Maximize frequency of an element by at most one increment or decrement of all array elements | Function to maximize the frequency of an array element by incrementing or decrementing array elements at most once ; Stores the largest array element ; Stores the smallest array element ; freq [ i ] : Stores frequency of ( i + Min ) in the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores maximum frequency of an array element by incrementing or decrementing array elements ; Iterate all the numbers over the range [ Min , Max ] ; Stores sum of three consecutive numbers ; Update maxSum ; Prlet maxSum ; Driver Code ; Function call
function max_freq ( arr , N ) { arr . sort ( ) ; let Max = arr [ N - 1 ] ; let Min = arr [ 0 ] ; let freq = [ ] ; for ( let i = 0 ; i < Max - Min + 1 ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] - Min ] ++ ; } let maxSum = 0 ; for ( let i = 0 ; i < ( Max - Min - 1 ) ; i ++ ) { let val = freq [ i ] + freq [ i + 1 ] + freq [ i + 2 ] ; maxSum = Math . max ( maxSum , val ) ; } document . write ( maxSum ) ; } let arr = [ 3 , 1 , 4 , 1 , 5 , 9 , 2 ] ; let N = arr . length ; max_freq ( arr , N ) ;
Longest increasing subsequence consisting of elements from indices divisible by previously selected indices | Function to find length of longest subsequence generated that satisfies the specified conditions ; Stores the length of longest subsequences of all lengths ; Iterate through the given array ; Iterate through the multiples i ; Update dp [ j ] as maximum of dp [ j ] and dp [ i ] + 1 ; Return the maximum element in dp as the length of longest subsequence ; Driver Code ; Function Call
function findMaxLength ( N , arr ) { var dp = Array ( N + 1 ) . fill ( 1 ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 2 * i ; j <= N ; j += i ) { if ( arr [ i - 1 ] < arr [ j - 1 ] ) { dp [ j ] = Math . max ( dp [ j ] , dp [ i ] + 1 ) ; } } } return Math . max . apply ( Math , dp ) ; } var arr = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; var N = arr . length ; document . write ( findMaxLength ( N , arr ) ) ;
Queries to check if any pair exists in an array having values at most equal to the given pair | Function that performs binary search to find value less than or equal to first value of the pair ; Perform binary search ; Find the mid ; Update the high ; Else update low ; Return the low index ; Function to modify the second value of each pair ; start from index 1 ; Function to evaluate each query ; Find value less than or equal to the first value of pair ; check if we got the required pair or not ; Function to find a pair whose values is less than the given pairs in query ; Find the size of the vector ; sort the vector based on the first value ; Function Call to modify the second value of each pair ; Traverse each queries ; Evaluate each query ; Print the result ; Driver function ; Function Call
function binary_search ( vec , a , n ) { let low , high , mid ; low = 0 ; high = n - 1 ; while ( low < high ) { mid = low + Math . floor ( ( high - low + 1 ) / 2 ) ; if ( vec [ mid ] [ 0 ] > a ) { high = mid - 1 ; } else if ( vec [ mid ] [ 1 ] <= a ) { low = mid ; } } return low ; } function modify_vec ( v , n ) { for ( let i = 1 ; i < n ; i ++ ) { v [ i ] [ 1 ] = Math . min ( v [ i ] [ 1 ] , v [ i - 1 ] [ 1 ] ) ; } } function evaluate_query ( v , n , m1 , m2 ) { let temp = binary_search ( v , n , m1 ) ; if ( ( v [ temp ] [ 0 ] <= m1 ) && ( v [ temp ] [ 1 ] <= m2 ) ) { return 1 ; } return 0 ; } function checkPairs ( v , queries ) { let n = v . length ; v . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; modify_vec ( v , n ) ; let k = queries . length ; for ( let i = 0 ; i < k ; i ++ ) { let m1 = queries [ i ] [ 0 ] ; let m2 = queries [ i ] [ 1 ] ; let result = evaluate_query ( v , n , m1 , m2 ) ; if ( result > 0 ) document . write ( " " ) ; else document . write ( " " ) ; } } let arr = [ [ 3 , 5 ] , [ 2 , 7 ] , [ 2 , 3 ] , [ 4 , 9 ] ] ; let queries = [ [ 3 , 4 ] , [ 3 , 2 ] , [ 4 , 1 ] , [ 3 , 7 ] ] ; checkPairs ( arr , queries ) ;
Count ways to split array into two equal sum subarrays by changing sign of any one array element | Function to count ways of splitting the array in two subarrays of equal sum by changing sign of any 1 element ; Stores the count of elements in prefix and suffix of array ; Stores the total sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto an index ; Stores sum of suffix from an index ; Stores the count of ways to split the array in 2 subarrays having equal sum ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix Map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix Map ; Store the difference between the subarrays ; Check if diff is even or not ; Count number of ways to split array at index i such that subarray sums are same ; Update the count ; Return the count ; Driver Code ; Function Call
function countSubArraySignChange ( arr , N ) { let prefixCount = new Map ( ) ; let suffixCount = new Map ( ) ; let total = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { total += arr [ i ] ; if ( suffixCount . has ( arr [ i ] ) ) { suffixCount . set ( arr [ i ] , suffixCount . get ( arr [ i ] ) + 1 ) ; } else { suffixCount . set ( arr [ i ] , 1 ) ; } } let prefixSum = 0 ; let suffixSum = 0 ; let count = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixCount . has ( arr [ i ] ) ) { prefixCount . set ( arr [ i ] , prefixCount . get ( arr [ i ] ) + 1 ) ; } else { prefixCount . set ( arr [ i ] , 1 ) ; } suffixSum = total - prefixSum ; if ( suffixCount . has ( arr [ i ] ) ) { suffixCount . set ( arr [ i ] , suffixCount . get ( arr [ i ] ) - 1 ) ; } let diff = prefixSum - suffixSum ; if ( diff % 2 == 0 ) { let x = ( prefixCount . has ( diff / 2 ) ? prefixCount . get ( diff / 2 ) : 0 ) + ( suffixCount . has ( - diff / 2 ) ? suffixCount . get ( - diff / 2 ) : 0 ) ; count = count + x ; } } return count ; } let arr = [ 2 , 2 , - 3 , 3 ] ; let N = arr . length ; document . write ( countSubArraySignChange ( arr , N ) ) ;
Partition string into two substrings having maximum number of common non | Function to count maximum common non - repeating characters that can be obtained by partitioning the string into two non - empty substrings ; Stores count of non - repeating characters present in both the substrings ; Stores distinct characters in left substring ; Stores distinct characters in right substring ; Traverse left substring ; Insert S [ i ] into ls ; Traverse right substring ; Insert S [ i ] into rs ; Traverse distinct characters of left substring ; If current character is present in right substring ; Update cnt ; Return count ; Function to partition the string into two non - empty substrings in all possible ways ; Stores maximum common distinct characters present in both the substring partitions ; Traverse the string ; Update ans ; Print count of maximum common non - repeating characters ; Driver Code
function countCommonChar ( ind , S ) { var cnt = 0 ; var ls = new Set ( ) ; var rs = new Set ( ) ; for ( var i = 0 ; i < ind ; ++ i ) { ls . add ( S [ i ] ) ; } for ( var i = ind ; i < S . length ; ++ i ) { rs . add ( S [ i ] ) ; } ls . forEach ( v => { if ( rs . has ( v ) ) { ++ cnt ; } } ) ; return cnt ; } function partitionStringWithMaxCom ( S ) { var ans = 0 ; for ( var i = 1 ; i < S . length ; ++ i ) { ans = Math . max ( ans , countCommonChar ( i , S ) ) ; } document . write ( ans ) ; } var str = " " ; partitionStringWithMaxCom ( str ) ;
Longest Substring of 1 's after removing one character | Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Add '0' at the end ; Iterator to traverse the string ; Stores maximum length of required substring ; Stores length of substring of '1' preceding the current character ; Stores length of substring of '1' succeeding the current character ; Counts number of '0' s ; Traverse the string S ; If current character is '1' ; Increase curr_one by one ; Otherwise ; Increment numberofZeros by one ; Count length of substring obtained y concatenating preceding and succeeding substrings of '1' ; Store maximum size in res ; Assign curr_one to prev_one ; Reset curr_one ; If string contains only one '0' ; Return the answer ; Driver code
function longestSubarray ( s ) { s += ' ' ; let i ; let res = 0 ; let prev_one = 0 ; let curr_one = 0 ; let numberOfZeros = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { curr_one += 1 ; } else { numberOfZeros += 1 ; prev_one += curr_one ; res = Math . max ( res , prev_one ) ; prev_one = curr_one ; curr_one = 0 ; } } if ( numberOfZeros == 1 ) { res -= 1 ; } return res ; } let S = " " ; document . write ( longestSubarray ( S ) ) ;
Longest Substring of 1 's after removing one character | Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Initializing i and j as left and right boundaries of sliding window ; If current character is '0' ; Decrement k by one ; If k is less than zero and character at ith index is '0' ; Return result ; Driver Code
function longestSubarray ( s ) { var i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . length ; ++ j ) { if ( s [ j ] == ' ' ) k -- ; if ( k < 0 && s [ i ++ ] == ' ' ) k ++ ; } return j - i - 1 ; } var S = " " ; document . write ( longestSubarray ( S ) ) ;
Lexicographically smallest string possible by inserting given character | Function to obtain lexicographically smallest String possible by inserting character c in the String s ; Traverse the String ; If the current character is greater than the given character ; Insert the character before the greater character ; Return the String ; Append the character at the end ; Return the String ; Driver code
function SmallestString ( s , c ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] > c ) { let temp = s ; s = s . substring ( 0 , i ) ; s += c ; s += temp . substring ( i , temp . length ) ; return s ; } } s += c ; return s ; } let S = " " ; let C = ' ' ; document . write ( SmallestString ( S , C ) ) ;
Largest Non | Function to find the largest unique element of the array ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Stores largest non - repeating element present in the array ; Stores index of the largest unique element of the array ; Traverse the array ; If frequency of arr [ i ] is equal to 1 and arr [ i ] exceeds LNRElem ; Update ind ; Update LNRElem ; If no array element is found with frequency equal to 1 ; Print the largest non - repeating element ; Driver Code
function LarUnEl ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) ; } var LNRElem = - 1000000000 ; var ind = - 1 ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . get ( arr [ i ] ) == 1 && arr [ i ] > LNRElem ) { ind = i ; LNRElem = arr [ i ] ; } } if ( ind == - 1 ) { cout << ind ; return ; } document . write ( arr [ ind ] ) ; } var arr = [ 3 , 1 , 8 , 8 , 4 ] ; var N = arr . length ; LarUnEl ( arr , N ) ;
Minimum shifts of substrings of 1 s required to group all 1 s together in a given Binary string | Function to count indices substrings of 1 s need to be shifted such that all 1 s in the string are grouped together ; Stores first occurrence of '1' ; Stores last occurrence of '1' ; Count of 0 s between firstOne and lastOne ; Traverse the string to find the first and last occurrences of '1' ; Count number of 0 s present between firstOne and lastOne ; Prlet minimum operations ; Given string
function countShifts ( str ) { let firstOne = - 1 ; let lastOne = - 1 ; let count = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { if ( firstOne == - 1 ) firstOne = i ; lastOne = i ; } } if ( ( firstOne == - 1 ) || ( firstOne == lastOne ) ) { Console . Write ( 0 ) ; return ; } for ( let i = firstOne ; i <= lastOne ; i ++ ) { if ( str [ i ] == ' ' ) { count ++ ; } } document . write ( count ) ; } let str = " " ; countShifts ( str ) ;
Check if a non | Utility Function to check whether a subsequence same as the given subarray exists or not ; Check if first element of the subarray is also present before ; Check if last element of the subarray is also present later ; If above two conditions are not satisfied , then no such subsequence exists ; Function to check and prlet if a subsequence which is same as the given subarray is present or not ; Driver code ; Function Call
function checkSubsequenceUtil ( arr , L , R , N ) { for ( let i = 0 ; i < L ; i ++ ) if ( arr [ i ] == arr [ L ] ) return true ; for ( let i = R + 1 ; i < N ; i ++ ) if ( arr [ i ] == arr [ R ] ) return true ; return false ; } function checkSubsequence ( arr , L , R , N ) { if ( checkSubsequenceUtil ( arr , L , R , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let arr = [ 1 , 7 , 12 , 1 , 7 , 5 , 10 , 11 , 42 ] ; let N = arr . length ; let L = 3 , R = 6 ; checkSubsequence ( arr , L , R , N ) ;
Search an element in a Doubly Linked List | Structure of a node of the doubly linked list ; Stores data value of a node ; Stores pointer to next node ; Stores pointer to previous node ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate memory for new node ; Insert the data ; Since node is added at the beginning , prev is always null ; Link the old list to the new node ; If pointer to head is not null ; Change the prev of head node to new node ; Move the head to point to the new node ; Function to find the position of an integer in doubly linked list ; Stores head Node ; Stores position of the integer in the doubly linked list ; Traverse the doubly linked list ; Update pos ; Update temp ; If the integer not present in the doubly linked list ; If the integer present in the doubly linked list ; Driver Code ; Create the doubly linked list 18 < . 15 < . 8 < . 9 < . 14
class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } ; function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) { ( head_ref ) . prev = new_node ; } ( head_ref ) = new_node ; return head_ref ; } function search ( head_ref , x ) { var temp = head_ref ; var pos = 0 ; while ( temp . data != x && temp . next != null ) { pos ++ ; temp = temp . next ; } if ( temp . data != x ) return - 1 ; return ( pos + 1 ) ; } var head = null ; var X = 8 ; head = push ( head , 14 ) ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 15 ) ; head = push ( head , 18 ) ; document . write ( search ( head , X ) ) ;
Longest subarray in which all elements are smaller than K | Function to find the length of the longest subarray with all elements smaller than K ; Stores the length of maximum consecutive sequence ; Stores maximum length of subarray ; Iterate through the array ; Check if array element smaller than K ; Store the maximum of length and count ; Reset the counter ; Prlet the maximum length ; Given array arr [ ] ; Size of the array ; Given K ; Function Call
function largestSubarray ( arr , N , K ) { let count = 0 ; let len = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < K ) { count += 1 ; } else { len = Math . max ( len , count ) ; count = 0 ; } } if ( count != 0 ) { len = Math . max ( len , count ) ; } document . write ( len ) ; } let arr = [ 1 , 8 , 3 , 5 , 2 , 2 , 1 , 13 ] ; let N = arr . length ; let K = 6 ; largestSubarray ( arr , N , K ) ;
Smallest pair of indices with product of subarray co | Function to calculate GCD of two letegers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod [ ] ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then prlet - 1 ; Driver code ; Function Call
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function findPair ( A , N ) { let right_prod = [ ] ; let flag = 0 ; right_prod [ N - 1 ] = A [ N - 1 ] ; for ( let i = N - 2 ; i >= 0 ; i -- ) right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] ; let total_prod = right_prod [ 0 ] ; let product ; for ( let i = 1 ; i < N - 1 ; i ++ ) { product = 1 ; for ( let j = i ; j < N - 1 ; j ++ ) { product *= A [ j ] ; if ( gcd ( product , right_prod [ j + 1 ] ) == 1 || gcd ( product , total_prod / right_prod [ i ] ) == 1 ) { flag = 1 ; document . write ( " " + ( i - 1 ) + " " + ( j + 1 ) + " " ) ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) document . write ( - 1 ) ; } let arr = [ 2 , 4 , 1 , 3 , 7 ] ; let N = arr . length ; findPair ( arr , N ) ;
Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' | Function to count the strings consisting of at least 1 set bit ; Initialize count ; Iterate through string ; The answer is 2 ^ N - 1 ; Given string ; Function call
function countString ( S ) { var count = 0 ; for ( var it = 0 ; it < S . length ; it ++ ) { if ( S [ it ] == ' ' && count > 0 ) { count -- ; } else { count ++ ; } } document . write ( ( 1 << count ) - 1 ) ; } var S = " " ; countString ( S ) ;
Append digits to the end of duplicate strings to make all strings in an array unique | Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call
function replaceDuplicates ( names ) { var hash = new Map ( ) ; for ( var i = 0 ; i < names . length ; i ++ ) { if ( ! hash . has ( names [ i ] ) ) hash . set ( names [ i ] , 1 ) ; else { var count = hash . get ( names [ i ] ) ; hash . set ( names [ i ] , hash . get ( names [ i ] ) + 1 ) ; names [ i ] += count . toString ( ) ; } } for ( var i = 0 ; i < names . length ; i ++ ) { document . write ( names [ i ] + " " ) ; } } var str = [ " " , " " , " " , " " , " " , " " , " " ] ; replaceDuplicates ( str ) ;
Minimum cost of flipping characters required to convert Binary String to 0 s only | Function to find the minimum cost to convert given string to 0 s only ; Length of string ; Stores the index of leftmost '1' in str ; Update the index of leftmost '1' in str ; Stores the index of rightmost '1' in str ; Update the index of rightmost '1' in str ; If str does not contain any '1' s ; No changes required ; Stores minimum cost ; Iterating through str form left_1 to right_1 ; Stores length of consecutive 0 s ; Calculate length of consecutive 0 s ; If a substring of 0 s exists ; Update minimum cost ; Prleting the minimum cost ; Driver Code
function convert_to_allzeroes ( str , a , b ) { let len = str . length ; let left_1 , i = 0 ; while ( i < len && str [ i ] == ' ' ) i ++ ; left_1 = i ; let right_1 ; i = len - 1 ; while ( i >= 0 && str [ i ] == ' ' ) i -- ; right_1 = i ; if ( left_1 == len && right_1 == - 1 ) { document . write ( 0 ) ; return ; } let cost = a , zeroes ; for ( i = left_1 ; i <= right_1 ; i ++ ) { zeroes = 0 ; while ( i < len && str [ i ] == ' ' ) { zeroes ++ ; i ++ ; } if ( zeroes != 0 ) cost += Math . min ( zeroes * b , a ) ; } document . write ( cost ) ; } let str = " " ; let A = 5 , B = 1 ; convert_to_allzeroes ( str , A , B ) ;