codeparrot/xlcost-text-to-code huggingface dataset:
Construct a string that has exactly K subsequences from given string | Function that computes the string s ; Length of the given string str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; Store the output ; Print the string ; Given String ; Function Call
function printSubsequenceString ( str , k ) { let n = str . length ; let i ; let factors = new Array ( ) ; for ( let i = 2 ; i <= Math . sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . push ( i ) ; k /= i ; } } if ( k > 1 ) factors . push ( k ) ; let count = new Array ( n ) . fill ( 1 ) ; let index = 0 ; while ( factors . length > 0 ) { count [ index ++ ] *= factors [ factors . length - 1 ] ; factors . pop ( ) ; if ( index == n ) index = 0 ; } let s = new String ( ) ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str [ i ] ; } } document . write ( s ) ; } let str = " " ; let k = 20 ; printSubsequenceString ( str , k ) ;
Replace each element of Array with it 's corresponding rank | Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; Map to store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Given array arr [ ] ; Function call ; Print the array elements
function changeArr ( input , N ) { var newArray = JSON . parse ( JSON . stringify ( input ) ) ; newArray . sort ( ( a , b ) => a - b ) ; var i ; var ranks = new Map ( ) ; var rank = 1 ; for ( var index = 0 ; index < N ; index ++ ) { var element = newArray [ index ] ; if ( ! ranks . has ( element ) ) { ranks . set ( element , rank ) ; rank ++ ; } } for ( var index = 0 ; index < N ; index ++ ) { var element = input [ index ] ; input [ index ] = ranks . get ( input [ index ] ) ; } return input ; } var arr = [ 100 , 2 , 70 , 2 ] ; var N = arr . length ; arr = changeArr ( arr , N ) ; document . write ( " " ) ; for ( var i = 0 ; i < N - 1 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( arr [ N - 1 ] + " " ) ;
Find pairs in array whose sum does not exist in Array | Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver code
function findPair ( arr , n ) { var i , j ; if ( n < 2 ) { document . write ( " " ) ; } var hashMap = new Set ( ) ; for ( var k = 0 ; k < n ; k ++ ) { hashMap . add ( arr [ k ] ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . has ( sum ) ) { document . write ( " " + arr [ i ] + " " + arr [ j ] + " " ) ; } } } } var arr = [ 2 , 4 , 2 , 6 ] ; var n = arr . length ; findPair ( arr , n ) ;
Largest subset with M as smallest missing number | Function to find and return the length of the longest subset whose smallest missing value is M ; Initialize a set ; If array element is not equal to M ; Insert into set ; Increment frequency ; Stores minimum missing number ; Iterate to find the minimum missing integer ; If minimum obtained is less than M ; Update answer ; Return answer ; Driver code
function findLengthOfMaxSubset ( arr , n , m ) { let s = new Set ( ) ; let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let tmp = arr [ i ] ; if ( tmp != m ) { s . add ( tmp ) ; answer ++ ; } } let min = 1 ; while ( s . has ( min ) ) { min ++ ; } if ( min != m ) { answer = - 1 ; } return answer ; } let arr = [ 1 , 2 , 4 ] ; let N = arr . length ; let M = 3 ; document . write ( findLengthOfMaxSubset ( arr , N , M ) ) ;
Count of submatrix with sum X in a given Matrix | Size of a column ; Function to find the count of submatrix whose sum is X ; Copying arr to dp and making it indexed 1 ; Precalculate and store the sum of all rectangles with upper left corner at ( 0 , 0 ) ; ; Calculating sum in a 2d grid ; Stores the answer ; Minimum length of square ; Maximum length of square ; Flag to set if sub - square with sum X is found ; Calculate lower right index if upper right corner is at { i , j } ; Calculate the sum of elements in the submatrix with upper left column { i , j } and lower right column at { ni , nj ] ; ; If sum X is found ; If sum > X , then size of the square with sum X must be less than mid ; If sum < X , then size of the square with sum X must be greater than mid ; If found , increment count by 1 ; ; Driver Code ; Given Matrix arr ; Function Call
var m = 5 ; function countSubsquare ( arr , n , X ) { var dp = Array ( n + 1 ) ; for ( var i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = Array ( m + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { dp [ i + 1 ] [ j + 1 ] = arr [ i ] [ j ] ; } } for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; } } var cnt = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { var lo = 1 ; var hi = Math . min ( n - i , m - j ) + 1 ; var found = false ; while ( lo <= hi ) { var mid = parseInt ( ( lo + hi ) / 2 ) ; var ni = i + mid - 1 ; var nj = j + mid - 1 ; var sum = dp [ ni ] [ nj ] - dp [ ni ] [ j - 1 ] - dp [ i - 1 ] [ nj ] + dp [ i - 1 ] [ j - 1 ] ; if ( sum >= X ) { if ( sum == X ) { found = true ; } hi = mid - 1 ; } else { lo = mid + 1 ; } } if ( found == true ) { cnt ++ ; } } } return cnt ; } var N = 4 , X = 10 ; var arr = [ [ 2 , 4 , 3 , 2 , 10 ] , [ 3 , 1 , 1 , 1 , 5 ] , [ 1 , 1 , 2 , 1 , 4 ] , [ 2 , 1 , 1 , 1 , 3 ] ] ; document . write ( countSubsquare ( arr , N , X ) + " " ) ;
Check if two arrays can be made equal by reversing any subarray once | Function to check whether two arrays can be made equal by reversing a sub - array only once ; Integer variable for storing the required starting and ending indices in the array ; Finding the smallest index for which A [ i ] != B [ i ] i . e the starting index of the unequal sub - array ; Finding the largest index for which A [ i ] != B [ i ] i . e the ending index of the unequal sub - array ; Reversing the sub - array A [ start ] , A [ start + 1 ] . . A [ end ] ; Checking whether on reversing the sub - array A [ start ] ... A [ end ] makes the arrays equal ; If any element of the two arrays is unequal print No and return ; Print Yes if arrays are equal after reversing the sub - array ; Driver code
function checkArray ( A , B , N ) { var start = 0 ; var end = N - 1 ; for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { start = i ; break ; } } for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { end = i ; break ; } } var l = start ; var r = end ; var d = parseInt ( ( r - l + 2 ) / 2 ) ; for ( var i = 0 ; i < d ; i ++ ) { var t = A [ l + i ] ; A [ l + i ] = A [ r - i ] ; A [ r - i ] = t ; } for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var A = [ 1 , 3 , 2 , 4 ] ; var B = [ 1 , 2 , 3 , 4 ] ; var N = A . length ; checkArray ( A , B , N ) ;
Count of substrings having all distinct characters | Function to count total number of valid substrings ; Stores the count of substrings ; Stores the frequency of characters ; Initialised both pointers to beginning of the string ; If all characters in substring from index i to j are distinct ; Increment count of j - th character ; Add all substring ending at j and starting at any index between i and j to the answer ; Increment 2 nd pointer ; If some characters are repeated or j pointer has reached to end ; Decrement count of j - th character ; Increment first pointer ; Return the final count of substrings ; Driver Code
function countSub ( str ) { var n = str . length ; var ans = 0 ; var cnt = Array ( 26 ) . fill ( 0 ) ; var i = 0 , j = 0 ; while ( i < n ) { if ( j < n && ( cnt [ str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) ) { cnt [ str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; ans += ( j - i + 1 ) ; j ++ ; } else { cnt [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; i ++ ; } } return ans ; } var str = " " ; document . write ( countSub ( str ) ) ;
Find largest factor of N such that N / F is less than K | Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Given N and K ; Function call
function solve ( n , k ) { let ans = 0 ; for ( let j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = Math . max ( ans , j ) ; } if ( n / j <= k ) { ans = Math . max ( ans , n / j ) ; } } } return ans ; } let N = 8 , K = 7 ; document . write ( ( N / solve ( N , K ) ) ) ;
Maximum sum subarray of size range [ L , R ] | function to find Maximum sum subarray of size between L and R ; calculating prefix sum ; maintain 0 for initial values of i upto R Once i = R , then we need to erase that 0 from our multiset as our first index of subarray cannot be 0 anymore . ; we maintain flag to counter if that initial 0 was erased from set or not . ; erase 0 from multiset once i = b ; insert pre [ i - L ] ; find minimum value in multiset . ; erase pre [ i - R ] ; Driver code
function max_sum_subarray ( arr , L , R ) { let n = arr . length ; let pre = new Array ( n ) ; pre [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr [ i ] ; } let s1 = [ ] s1 . push ( 0 ) ; let ans = Number . MIN_VALUE ; ans = Math . max ( ans , pre [ L - 1 ] ) ; let flag = 0 ; for ( let i = L ; i < n ; i ++ ) { if ( i - R >= 0 ) { if ( flag == 0 ) { let it = s1 . indexOf ( 0 ) ; s1 . splice ( it , 1 ) ; flag = 1 ; } } if ( i - L >= 0 ) s1 . push ( pre [ i - L ] ) ; ans = Math . max ( ans , pre [ i ] - s1 [ 0 ] ) ; if ( i - R >= 0 ) { let it = s1 . indexOf ( pre [ i - R ] ) ; s1 . splice ( it , 1 ) ; } } document . write ( ans ) ; } let L , R ; L = 1 ; R = 3 ; let arr = [ 1 , 2 , 2 , 1 ] ; max_sum_subarray ( arr , L , R ) ;
Count of ways to select K consecutive empty cells from a given Matrix | Function to Traverse the matrix row wise ; Initialize ans ; Traverse row wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , then increment countcons ; Check if number of empty consecutive cells is greater or equal to K , increment the ans ; Return the count ; Function to Traverse the matrix column wise ; Initialize ans ; Traverse column wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , increment countcons ; Check if number of empty consecutive cells is greater than or equal to K , increment the ans ; Return the count ; Driver Code ; If k = 1 only traverse row wise ; Traverse both row and column wise
function rowWise ( v , n , m , k ) { var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var countcons = 0 ; for ( var j = 0 ; j < m ; j ++ ) { if ( ( v + i * m + j ) == ' ' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } function colWise ( v , n , m , k ) { var ans = 0 ; for ( var i = 0 ; i < m ; i ++ ) { var countcons = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( ( v + j * n + i ) == ' ' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } var n = 3 , m = 3 , k = 1 ; var v = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; if ( k == 1 ) { document . write ( rowWise ( v [ 0 ] , n , m , k ) ) ; } else { document . write ( colWise ( v [ 0 ] , n , m , k ) + rowWise ( v [ 0 ] , n , m , k ) ) ; }
Check if the count of inversions of two given types on an Array are equal or not | Function to check if the count of inversion of two types are same or not ; If maximum value is found to be greater than a [ j ] , then that pair of indices ( i , j ) will add extra value to inversion of Type 1 ; Update max ; Driver Code
function solve ( a , n ) { let mx = 0 ; for ( let j = 1 ; j < n ; j ++ ) { if ( mx > a [ j ] ) return false ; mx = Math . max ( mx , a [ j - 1 ] ) ; } return true ; } let a = [ 1 , 0 , 2 ] ; let n = a . length ; let possible = solve ( a , n ) ; if ( possible ) document . write ( " " ) ; else document . write ( " " ) ;
Count of prime factors of N to be added at each step to convert N to M | JavaScript program to find the minimum steps required to convert a number N to M . ; Array to store shortest prime factor of every integer ; Function to precompute shortest prime factors ; Function to insert distinct prime factors of every integer into a set ; Store distinct prime factors ; Function to return minimum steps using BFS ; Queue of pairs to store the current number and distance from root . ; Set to store distinct prime factors ; Run BFS ; Find out the prime factors of newNum ; Iterate over every prime factor of newNum . ; If M is obtained ; Return number of operations ; If M is exceeded ; Otherwise ; Update and store the new number obtained by prime factor ; If M cannot be obtained ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } var spf = Array ( 100009 ) . fill ( 0 ) ; function sieve ( ) { for ( var i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( var i = 2 ; i * i <= 100005 ; i ++ ) { for ( var j = i ; j <= 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } function findPrimeFactors ( s , n ) { while ( n > 1 ) { s . add ( spf [ n ] ) ; n /= spf [ n ] ; } return s ; } function MinimumSteps ( n , m ) { var q = [ ] ; var s = new Set ( ) ; q . push ( new pair ( n , 0 ) ) ; while ( q . length != 0 ) { var newNum = q [ 0 ] . first ; var distance = q [ 0 ] . second ; q . shift ( ) ; var k = findPrimeFactors ( s , newNum ) ; for ( var i of k ) { if ( newNum == m ) { return distance ; } else if ( newNum > m ) { break ; } else { q . push ( new pair ( newNum + i , distance + 1 ) ) ; } } } return - 1 ; } var N = 7 , M = 16 ; sieve ( ) ; document . write ( MinimumSteps ( N , M ) ) ;
Find number of pairs ( x , y ) in an Array such that x ^ y > y ^ x | Set 2 | Function to return the count of pairs ; Compute suffix sums till i = 3 ; Base Case : x = 0 ; No valid pairs ; Base Case : x = 1 ; Store the count of 0 's ; Base Case : x = 2 ; Store suffix sum upto 5 ; Base Case : x = 3 ; Store count of 2 and suffix sum upto 4 ; For all other values of x ; For all x >= 2 , every y = 0 and every y = 1 makes a valid pair ; Return the count of pairs ; Driver code
function countPairs ( X , Y , m , n ) { let suffix = Array . from ( { length : 1005 } , ( _ , i ) => 0 ) ; let total_pairs = 0 ; for ( let i = 0 ; i < n ; i ++ ) suffix [ Y [ i ] ] ++ ; for ( let i = 1e3 ; i >= 3 ; i -- ) suffix [ i ] += suffix [ i + 1 ] ; for ( let i = 0 ; i < m ; i ++ ) { if ( X [ i ] == 0 ) continue ; else if ( X [ i ] == 1 ) { total_pairs += suffix [ 0 ] ; continue ; } else if ( X [ i ] == 2 ) total_pairs += suffix [ 5 ] ; else if ( X [ i ] == 3 ) total_pairs += suffix [ 2 ] + suffix [ 4 ] ; else total_pairs += suffix [ X [ i ] + 1 ] ; total_pairs += suffix [ 0 ] + suffix [ 1 ] ; } return total_pairs ; } let X = [ 10 , 19 , 18 ] ; let Y = [ 11 , 15 , 9 ] ; let m = X . length ; let n = Y . length ; document . write ( countPairs ( X , Y , m , n ) ) ;
Min number of moves to traverse entire Matrix through connected cells with equal values | Function to find the minimum number of moves to traverse the given matrix ; Constructing another matrix consisting of distinct values ; Updating the array B by checking the values of A that if there are same values connected through an edge or not ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Store all distinct elements in a set ; Return answer ; Driver Code ; Function Call
function solve ( A , N , M ) { var B = Array . from ( Array ( N ) , ( ) => Array ( M ) ) ; var c = 1 ; var s = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) B [ i ] [ j ] = c ++ ; } for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { if ( i != 0 ) { if ( A [ i - 1 ] [ j ] == A [ i ] [ j ] ) B [ i - 1 ] [ j ] = B [ i ] [ j ] ; } if ( i != N - 1 ) { if ( A [ i + 1 ] [ j ] == A [ i ] [ j ] ) B [ i + 1 ] [ j ] = B [ i ] [ j ] ; } if ( j != 0 ) { if ( A [ i ] [ j - 1 ] == A [ i ] [ j ] ) B [ i ] [ j - 1 ] = B [ i ] [ j ] ; } if ( j != M - 1 ) { if ( A [ i ] [ j + 1 ] == A [ i ] [ j ] ) B [ i ] [ j + 1 ] = B [ i ] [ j ] ; } } } for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) s . add ( B [ i ] [ j ] ) ; } return s . size ; } var N = 2 , M = 3 ; var A = [ [ 2 , 1 , 3 ] , [ 1 , 1 , 2 ] ] ; document . write ( solve ( A , N , M ) ) ;
Number of times an array can be partitioned repetitively into two subarrays with equal sum | Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Given array ; Function call
function splitArray ( start , end , arr , prefix_sum ) { if ( start >= end ) return 0 ; for ( let k = start ; k < end ; ++ k ) { if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) { return 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ; } } return 0 ; } function solve ( arr , n ) { let prefix_sum = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; prefix_sum [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; ++ i ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i - 1 ] ; } document . write ( splitArray ( 1 , n , arr , prefix_sum ) ) ; } let arr = [ 12 , 3 , 3 , 0 , 3 , 3 ] ; let N = arr . length ; solve ( arr , N ) ;
Minimise the maximum element of N subarrays of size K | Function to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Condition to check if it is not possible to choose k sized N subarrays ; Using binary search ; calculating mid ; Loop to find the count of the K sized subarrays possible with elements less than mid ; Condition to check if the answer is in right subarray ; Driver Code ; Function Call
function minDays ( arr , n , k ) { let l = arr . length , left = 1 , right = 1000000000 ; if ( n * k > l ) return - 1 ; while ( left < right ) { let mid = parseInt ( ( left + right ) / 2 ) , cnt = 0 , product = 0 ; for ( let j = 0 ; j < l ; ++ j ) { if ( arr [ j ] > mid ) { cnt = 0 ; } else if ( ++ cnt >= k ) { product ++ ; cnt = 0 ; } } if ( product < n ) { left = mid + 1 ; } else { right = mid ; } } return left ; } let arr = [ 1 , 10 , 3 , 10 , 2 ] ; let n = 3 , k = 1 ; document . write ( minDays ( arr , n , k ) ) ;
Count of distinct power of prime factor of N | Function to count the number of distinct positive power of prime factor of integer N ; Iterate for all prime factor ; If it is a prime factor , count the total number of times it divides n . ; Find the Number of distinct possible positive numbers ; Return the final count ; Given Number N ; Function Call
function countFac ( n ) { var m = n ; var count = 0 ; for ( var i = 2 ; ( i * i ) <= m ; ++ i ) { var total = 0 ; while ( n % i == 0 ) { n /= i ; ++ total ; } var temp = 0 ; for ( var j = 1 ; ( temp + j ) <= total ; ++ j ) { temp += j ; ++ count ; } } if ( n != 1 ) ++ count ; return count ; } var N = 24 ; document . write ( countFac ( N ) ) ;
Minimum total sum from the given two arrays | Function that prints minimum sum after selecting N elements ; Initialise the dp array ; Base Case ; Adding the element of array a if previous element is also from array a ; Adding the element of array a if previous element is from array b ; Adding the element of array b if previous element is from array a with an extra penalty of integer C ; Adding the element of array b if previous element is also from array b ; Print the minimum sum ; Given array arr1 [ ] and arr2 [ ] ; Given cost ; Function call
function minimumSum ( a , b , c , n ) { let dp = new Array ( n ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 1e6 ; } } dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = b [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 0 ] = Math . min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 1 ] + a [ i ] + c ) ; dp [ i ] [ 1 ] = Math . min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + b [ i ] + c ) ; dp [ i ] [ 1 ] = Math . min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 1 ] + b [ i ] ) ; } document . write ( Math . min ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) + " " ) ; } let arr1 = [ 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 ] ; let arr2 = [ 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 ] ; let C = 2 ; let N = arr1 . length ; minimumSum ( arr1 , arr2 , C , N ) ;
Minimum flips required in a binary string such that all K | Function to calculate and return the minimum number of flips to make string valid ; Stores the count of required flips ; Stores the last index of '1' in the string ; Check for the first substring of length K ; If i - th character is '1' ; If the substring had no '1' ; Increase the count of required flips ; Flip the last index of the window ; Update the last index which contains 1 ; Check for remaining substrings ; If last_idx does not belong to current window make it - 1 ; If the last character of the current substring is '1' , then update last_idx to i + k - 1 ; ; If last_idx == - 1 , then the current substring has no 1 ; Increase the count of flips ; Update the last index of the current window ; Store the last index of current window as the index of last '1' in the string ; Return the number of operations ; Driver Code
function minimumMoves ( S , K ) { var N = S . length ; var ops = 0 ; var last_idx = - 1 ; for ( var i = 0 ; i < K ; i ++ ) { if ( S [ i ] == ' ' ) last_idx = i ; } if ( last_idx == - 1 ) { ++ ops ; S [ K - 1 ] = ' ' ; last_idx = K - 1 ; } for ( var i = 1 ; i < N - K + 1 ; i ++ ) { if ( last_idx < i ) last_idx = - 1 ; if ( S [ i + K - 1 ] == ' ' ) last_idx = i + K - 1 ; if ( last_idx == - 1 ) { ++ ops ; S [ i + K - 1 ] = ' ' ; last_idx = i + K - 1 ; } } return ops ; } var S = " " ; var K = 3 ; document . write ( minimumMoves ( S , K ) ) ;