codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string can be split into two strings with same number of K | Function to print the arrangement of characters ; Stores frequency of characters ; Count the character having frequency K ; Count the character having frequency greater than K and not equal to 2 K ; Case 1 ; Case 2 ; Case 3 ; If all cases fail ; Driver code
function DivideString ( s , n , k ) { var i , c = 0 , no = 1 ; var c1 = 0 , c2 = 0 ; var fr = new Array ( 26 ) . fill ( 0 ) ; var ans = [ ] ; for ( i = 0 ; i < n ; i ++ ) { fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } var ch = " " , ch1 = " " ; for ( i = 0 ; i < 26 ; i ++ ) { if ( fr [ i ] === k ) { c ++ ; } if ( fr [ i ] > k && fr [ i ] !== 2 * k ) { c1 ++ ; ch = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; } if ( fr [ i ] === 2 * k ) { c2 ++ ; ch1 = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; } } for ( i = 0 ; i < n ; i ++ ) ans . push ( " " ) ; var mp = { } ; if ( c % 2 === 0 c1 > 0 c2 > 0 ) { for ( i = 0 ; i < n ; i ++ ) { if ( fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === k ) { if ( mp . hasOwnProperty ( s [ i ] ) ) { ans [ i ] = " " ; } else { if ( no <= parseInt ( c / 2 ) ) { ans [ i ] = " " ; no ++ ; mp [ s [ i ] ] = 1 ; } } } } if ( c % 2 === 1 && c1 > 0 ) { no = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] === ch && no <= k ) { ans [ i ] = " " ; no ++ ; } } } if ( c % 2 === 1 && c1 === 0 ) { no = 1 ; var flag = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] === ch1 && no <= k ) { ans [ i ] = " " ; no ++ ; } if ( fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === k && flag === 0 && ans [ i ] === " " ) { ans [ i ] = " " ; flag = 1 ; } } } document . write ( ans . join ( " " ) ) ; } else { document . write ( " " ) ; } } var S = " " ; var N = S . length ; var K = 1 ; DivideString ( S , N , K ) ;
Check if two items can be selected from two different categories without exceeding price | Function to check if two items can be selected from two different categories without exceeding the total price ; Loop to choose two different pairs using two nested loops ; Condition to check if the price of these two elements is less than S ; Driver code ; Function call
function check ( S , prices , type , n ) { for ( let j = 0 ; j < n ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) { if ( ( type [ j ] == 0 && type [ k ] == 1 ) || ( type [ j ] == 1 && type [ k ] == 0 ) ) { if ( prices [ j ] + prices [ k ] <= S ) { return " " ; } } } } return " " ; } let prices = [ 3 , 8 , 6 , 5 ] ; let type = [ 0 , 1 , 1 , 0 ] ; let S = 10 ; let n = 4 ; document . write ( check ( S , prices , type , n ) ) ;
Find the maximum sum ( a + b ) for a given input integer N satisfying the given condition | Function to return the maximum sum of a + b satisfying the given condition ; Initialize max_sum ; Consider all possible pairs and check the sum divides product property ; To find the largest factor k ; Check if the product is divisible by the sum ; Storing the maximum sum in the max_sum variable ; Return the max_sum value ; Driver code
function getLargestSum ( N ) { let max_sum = 0 ; for ( let i = 1 ; i * i <= N ; i ++ ) { for ( let j = i + 1 ; j * j <= N ; j ++ ) { let k = parseInt ( N / j , 10 ) ; let a = k * i ; let b = k * j ; if ( a <= N && b <= N && a * b % ( a + b ) == 0 ) max_sum = Math . max ( max_sum , a + b ) ; } } return max_sum ; } let N = 25 ; let max_sum = getLargestSum ( N ) ; document . write ( max_sum + " " ) ;
Encrypt a string by repeating i | Function to return the encrypted String ; Number of times the current character will be repeated ; Repeat the current character in the encrypted String ; Driver code
function encryptString ( str , n ) { let i = 0 , cnt = 0 ; let encryptedStr = " " ; while ( i < n ) { cnt = i + 1 ; while ( cnt -- > 0 ) encryptedStr += str [ i ] ; i ++ ; } return encryptedStr ; } let str = " " ; let n = str . length ; document . write ( encryptString ( str , n ) ) ;
Minimize the difference between the maximum and minimum values of the modified array | Function to return required minimum difference ; finding minimum and maximum values ; returning minimum possible difference ; Driver program ; function to return the answer
function minDiff ( n , x , A ) { var mn = A [ 0 ] , mx = A [ 0 ] ; for ( var i = 0 ; i < n ; ++ i ) { mn = Math . min ( mn , A [ i ] ) ; mx = Math . max ( mx , A [ i ] ) ; } return Math . max ( 0 , mx - mn - 2 * x ) ; } var n = 3 , x = 3 ; var A = [ 1 , 3 , 6 ] ; document . write ( minDiff ( n , x , A ) ) ;
Minimum Swaps for Bracket Balancing | Javascript Program to count swaps required to balance string ; stores the total number of Left and Right brackets encountered ; swap stores the number of swaps required imbalance maintains the number of imbalance pair ; increment count of Left bracket ; swaps count is last swap count + total number imbalanced brackets ; imbalance decremented by 1 as it solved only one imbalance of Left and Right ; increment count of Right bracket ; imbalance is reset to current difference between Left and Right brackets ; Driver code
function swapCount ( s ) { let chars = s . split ( ' ' ) ; let countLeft = 0 , countRight = 0 ; let swap = 0 , imbalance = 0 ; for ( let i = 0 ; i < chars . length ; i ++ ) { if ( chars [ i ] == ' ' ) { countLeft ++ ; if ( imbalance > 0 ) { swap += imbalance ; imbalance -- ; } } else if ( chars [ i ] == ' ' ) { countRight ++ ; imbalance = ( countRight - countLeft ) ; } } return swap ; } let s = " " ; document . write ( swapCount ( s ) + " " ) ; s = " " ; document . write ( swapCount ( s ) ) ;
Longest subsequence from an array of pairs having first element increasing and second element decreasing . | Function to find the length of the longest subsequence of pairs whose first element is increasing and second is decreasing ; dp [ i ] : Stores the longest subsequence upto i ; Base case ; When the conditions hold ; Finally , print the required answer ; Given Input ; Function Call
function longestSubSequence ( A , N ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = 1 ; for ( let j = 0 ; j < i ; j ++ ) { if ( A [ j ] [ 0 ] < A [ i ] [ 0 ] && A [ j ] [ 1 ] > A [ i ] [ 1 ] ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ; } } } document . write ( dp [ N - 1 ] + " " ) ; } let A = [ [ 1 , 2 ] , [ 2 , 2 ] , [ 3 , 1 ] ] ; let N = A . length ; longestSubSequence ( A , N ) ;
Count ways to obtain given sum by repeated throws of a dice | Function to calculate the total number of ways to have sum N ; Base Case ; Return already stored result ; Recur for all 6 states ; Return the result ; Given sum N ; Initialize the dp array ; Function Call
function findWays ( N , dp ) { if ( N == 0 ) { return 1 ; } if ( dp [ N ] != - 1 ) { return dp [ N ] ; } let cnt = 0 ; for ( let i = 1 ; i <= 6 ; i ++ ) { if ( N - i >= 0 ) { cnt = cnt + findWays ( N - i , dp ) ; } } return dp [ N ] = cnt ; } let N = 4 ; let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) dp [ i ] = - 1 ; document . write ( findWays ( N , dp ) ) ;
Count ways to obtain given sum by repeated throws of a dice | Function to calculate the total number of ways to have sum N ; Initialize dp array ; Iterate over all the possible intermediate values to reach N ; Calculate the sum for all 6 faces ; Print the total number of ways ; Given sum N ; Function call
function findWays ( N ) { let dp = new Array ( N + 1 ) ; dp [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; for ( let j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 ) { dp [ i ] = dp [ i ] + dp [ i - j ] ; } } } document . write ( dp [ N ] ) ; } let N = 4 ; findWays ( N ) ;
Split the string into minimum parts such that each part is in the another string | Javascript implementation to split the String into minimum number of parts such that each part is also present in the another String ; Node of Trie ; Function to insert a node in the Trie Data Structure ; Inserting every character from idx till end to String into trie ; If there is no edge corresponding to the ith character , then make a new node ; Function to find the minimum number of parts such that each part is present into another String ; Making a new trie ; Inserting every subString of S2 in trie ; Creating dp array and init it with infinity ; Base Case ; Starting the cut from ith character taking temporary node pointer for checking whether the subString [ i , j ) is present in trie of not ; If the jth character is not in trie we 'll break ; Updating the the ending of jth character with dp [ i ] + 1 ; Descending the trie pointer ; Answer not possible ; Driver Code
let INF = ( 1e9 + 9 ) ; class Node { constructor ( ) { } } function TrieNode ( ) { let temp = new Node ( ) ; temp . child = new Node ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { temp . child [ i ] = null ; } return temp ; } function insert ( idx , s , root ) { let temp = root ; for ( let i = idx ; i < s . length ; i ++ ) { if ( temp . child [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] == null ) temp . child [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = new TrieNode ( ) ; temp = temp . child [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ; } } function minCuts ( S1 , S2 ) { let n1 = S1 . length ; let n2 = S2 . length ; let root = new TrieNode ( ) ; for ( let i = 0 ; i < n2 ; i ++ ) { insert ( i , S2 , root ) ; } let dp = new Array ( n1 + 1 ) ; dp . fill ( INF ) ; dp [ 0 ] = 0 ; for ( let i = 0 ; i < n1 ; i ++ ) { let temp = root ; for ( let j = i + 1 ; j <= n1 ; j ++ ) { if ( temp . child [ S1 [ j - 1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] == null ) break ; dp [ j ] = Math . min ( dp [ j ] , dp [ i ] + 1 ) ; temp = temp . child [ S1 [ j - 1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ; } } if ( dp [ n1 ] >= INF ) return - 1 ; else return dp [ n1 ] ; } let S1 = " " ; let S2 = " " ; document . write ( minCuts ( S1 , S2 ) ) ;
Largest Square in a Binary Matrix with at most K 1 s for multiple Queries | Function to find the largest square in the matrix such that it contains atmost K 1 's ; Precomputation of the countDP prefix sum of the matrix ; Loop to solve each query ; Binary Search to the side which have atmost in K 1 's in square ; Count total number of 1 s in the sub square considered ; If the count is less than or equals to the maximum move to right half ; Driver code
function largestSquare ( matrix , R , C , q_i , q_j , K , Q ) { let countDP = new Array ( R ) ; for ( let i = 0 ; i < R ; i ++ ) { countDP [ i ] = new Array ( C ) ; for ( let j = 0 ; j < C ; j ++ ) countDP [ i ] [ j ] = 0 ; } countDP [ 0 ] [ 0 ] = matrix [ 0 ] [ 0 ] ; for ( let i = 1 ; i < R ; i ++ ) countDP [ i ] [ 0 ] = countDP [ i - 1 ] [ 0 ] + matrix [ i ] [ 0 ] ; for ( let j = 1 ; j < C ; j ++ ) countDP [ 0 ] [ j ] = countDP [ 0 ] [ j - 1 ] + matrix [ 0 ] [ j ] ; for ( let i = 1 ; i < R ; i ++ ) for ( let j = 1 ; j < C ; j ++ ) countDP [ i ] [ j ] = matrix [ i ] [ j ] + countDP [ i - 1 ] [ j ] + countDP [ i ] [ j - 1 ] - countDP [ i - 1 ] [ j - 1 ] ; for ( let q = 0 ; q < Q ; q ++ ) { let i = q_i [ q ] ; let j = q_j [ q ] ; let min_dist = Math . min ( Math . min ( i , j ) , Math . min ( R - i - 1 , C - j - 1 ) ) ; let ans = - 1 , l = 0 , u = min_dist ; while ( l <= u ) { let mid = Math . floor ( ( l + u ) / 2 ) ; let x1 = i - mid , x2 = i + mid ; let y1 = j - mid , y2 = j + mid ; let count = countDP [ x2 ] [ y2 ] ; if ( x1 > 0 ) count -= countDP [ x1 - 1 ] [ y2 ] ; if ( y1 > 0 ) count -= countDP [ x2 ] [ y1 - 1 ] ; if ( x1 > 0 && y1 > 0 ) count += countDP [ x1 - 1 ] [ y1 - 1 ] ; if ( count <= K ) { ans = 2 * mid + 1 ; l = mid + 1 ; } else u = mid - 1 ; } document . write ( ans + " " ) ; } } let matrix = [ [ 1 , 0 , 1 , 0 , 0 ] , [ 1 , 0 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] , [ 1 , 0 , 0 , 1 , 0 ] ] ; let K = 9 , Q = 1 ; let q_i = [ 1 ] ; let q_j = [ 2 ] ; largestSquare ( matrix , 4 , 5 , q_i , q_j , K , Q ) ;