codeparrot/xlcost-text-to-code huggingface dataset:
Largest Square in a Binary Matrix with at most K 1 s for multiple Queries | Javascript implementation to find the largest square in the matrix such that it contains atmost K 1 's ; Function to find the largest square in the matrix such that it contains atmost K 1 's ; Precomputing the countDP prefix sum of the matrix ; Loop to solve Queries ; Calculating the maximum possible distance of the centre from edge ; Calculating the number of 1 s in the submatrix ; Driver Code
let MAX = 100 ; 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 ; for ( let k = 0 ; k <= min_dist ; k ++ ) { let x1 = i - k , x2 = i + k ; let y1 = j - k , y2 = j + k ; 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 ) break ; ans = 2 * k + 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 ) ;
N consecutive ropes problem | Function to return the minimum cost to connect the given ropes ; dp [ i ] [ j ] = minimum cost in range ( i , j ) sum [ i ] [ j ] = sum of range ( i , j ) ; Initializing the sum table memset ( sum , 0 , sizeof ( 0 ) ) ; ; Computing minimum cost for all the possible interval ( i , j ) Left range ; Right range ; No cost for a single rope ; Driver code
function MinCost ( arr , n ) { let dp = new Array ( n + 5 ) ; let sum = new Array ( n + 5 ) ; for ( let i = 0 ; i < n + 5 ; i ++ ) { dp [ i ] = [ ] ; sum [ i ] = [ ] ; for ( let j = 0 ; j < n + 5 ; j ++ ) { dp [ i ] . push ( 0 ) sum [ i ] . push ( 0 ) } } console . log ( dp ) for ( let i = 0 ; i < n ; i ++ ) { let k = arr [ i ] ; for ( let j = i ; j < n ; j ++ ) { if ( i == j ) sum [ i ] [ j ] = k ; else { k += arr [ j ] ; sum [ i ] [ j ] = k ; } } } for ( let i = n - 1 ; i >= 0 ; i -- ) { for ( let j = i ; j < n ; j ++ ) { dp [ i ] [ j ] = Number . MAX_SAFE_INTEGER ; if ( i == j ) dp [ i ] [ j ] = 0 ; else { for ( let k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum [ i ] [ j ] ) ; } } } } return dp [ 0 ] [ n - 1 ] ; } let arr = [ 7 , 6 , 8 , 6 , 1 , 1 ] ; let n = arr . length ; document . write ( MinCost ( arr , n ) ) ;
Length of the longest subsegment which is UpDown after inserting atmost one integer | Function to recursively fill the dp array ; If f ( i , state ) is already calculated then return the value ; Calculate f ( i , state ) according to the recurrence relation and store in dp [ ] [ ] ; Function that calls the resucrsive function to fill the dp array and then returns the result ; dp [ ] [ ] array for storing result of f ( i , 1 ) and f ( 1 , 2 ) ; Populating the array dp [ ] with - 1 ; Make sure that longest UD and DU sequence starting at each index is calculated ; Assume the answer to be - 1 This value will only increase ; y is the length of the longest UD sequence starting at i ; If length is even then add an integer and then a DU sequence starting at i + y ; If length is odd then add an integer and then a UD sequence starting at i + y ; Driver code
function f ( i , state , A , dp , N ) { if ( i >= N ) return 0 ; else if ( dp [ i ] [ state ] != - 1 ) { return dp [ i ] [ state ] ; } else { if ( i == N - 1 ) dp [ i ] [ state ] = 1 ; else if ( state == 1 && A [ i ] > A [ i + 1 ] ) dp [ i ] [ state ] = 1 ; else if ( state == 2 && A [ i ] < A [ i + 1 ] ) dp [ i ] [ state ] = 1 ; else if ( state == 1 && A [ i ] <= A [ i + 1 ] ) dp [ i ] [ state ] = 1 + f ( i + 1 , 2 , A , dp , N ) ; else if ( state == 2 && A [ i ] >= A [ i + 1 ] ) dp [ i ] [ state ] = 1 + f ( i + 1 , 1 , A , dp , N ) ; return dp [ i ] [ state ] ; } } function maxLenSeq ( A , N ) { let i , j , tmp , y , ans ; let dp = new Array ( 1000 ) ; for ( i = 0 ; i < 1000 ; i ++ ) { dp [ i ] = new Array ( 3 ) ; for ( j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( i = 0 ; i < N ; i ++ ) { tmp = f ( i , 1 , A , dp , N ) ; tmp = f ( i , 2 , A , dp , N ) ; } ans = - 1 ; for ( i = 0 ; i < N ; i ++ ) { y = dp [ i ] [ 1 ] ; if ( i + y >= N ) ans = Math . max ( ans , dp [ i ] [ 1 ] + 1 ) ; else if ( y % 2 == 0 ) { ans = Math . max ( ans , dp [ i ] [ 1 ] + 1 + dp [ i + y ] [ 2 ] ) ; } else if ( y % 2 == 1 ) { ans = Math . max ( ans , dp [ i ] [ 1 ] + 1 + dp [ i + y ] [ 1 ] ) ; } } return ans ; } let A = [ 1 , 10 , 3 , 20 , 25 , 24 ] ; let n = A . length ; document . write ( maxLenSeq ( A , n ) ) ;
Remove an element to maximize the GCD of the given array | Recursive function to return gcd of a and b ; Function to return the maximized gcd after removing a single element from the given array ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing gcd of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing gcd of all the elements having greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be removed ; If any other element is replaced ; Return the maximized gcd ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function MaxGCD ( a , n ) { let Prefix = new Array ( n + 2 ) ; let Suffix = new Array ( n + 2 ) ; Prefix [ 1 ] = a [ 0 ] ; for ( let i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = gcd ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( let i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = gcd ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } let ans = Math . max ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( let i = 2 ; i < n ; i += 1 ) { ans = Math . max ( ans , gcd ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } let a = [ 14 , 17 , 28 , 70 ] ; let n = a . length ; document . write ( MaxGCD ( a , n ) ) ;
Maximum subarray sum by flipping signs of at most K array elements | Javascript implementation of the approach ; Function to find the maximum subarray sum with flips starting from index i ; If the number of flips have exceeded ; Complete traversal ; If the state has previously been visited ; Initially ; Use Kadane 's algorithm and call two states ; Memoize the answer and return it ; Utility function to call flips from index and return the answer ; Create DP array int dp [ n , k + 1 ] ; ; Iterate and call recursive function from every index to get the maximum subarray sum ; corner case ; Driver Code
let right = 2 ; let left = 4 ; let dp = new Array ( left ) ; function findSubarraySum ( ind , flips , n , a , k ) { if ( flips > k ) return ( - 1e9 ) ; if ( ind == n ) return 0 ; if ( dp [ ind ] [ flips ] != - 1 ) return dp [ ind ] [ flips ] ; let ans = 0 ; ans = Math . max ( 0 , a [ ind ] + findSubarraySum ( ind + 1 , flips , n , a , k ) ) ; ans = Math . max ( ans , - a [ ind ] + findSubarraySum ( ind + 1 , flips + 1 , n , a , k ) ) ; return dp [ ind ] [ flips ] = ans ; } function findMaxSubarraySum ( a , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( k ) ; for ( let j = 0 ; j < k + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } let ans = ( - 1e9 ) ; for ( let i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , findSubarraySum ( i , 0 , n , a , k ) ) ; if ( ans == 0 && k == 0 ) { let max = Number . MIN_VALUE ; for ( let i = 0 ; i < a . length ; i ++ ) { max = Math . max ( max , a [ i ] ) ; } return max ; } return ans ; } let a = [ - 1 , - 2 , - 100 , - 10 ] ; let n = a . length ; let k = 1 ; document . write ( findMaxSubarraySum ( a , n , k ) ) ;
Find the sum of first N odd Fibonacci numbers | javascript program to Find the sum of first N odd Fibonacci numbers ; Function to calculate sum of first N odd Fibonacci numbers ; base values ; Driver code
var mod = 1000000007 ; function sumOddFibonacci ( n ) { var Sum = Array ( n + 1 ) . fill ( 0 ) ; Sum [ 0 ] = 0 ; Sum [ 1 ] = 1 ; Sum [ 2 ] = 2 ; Sum [ 3 ] = 5 ; Sum [ 4 ] = 10 ; Sum [ 5 ] = 23 ; for ( i = 6 ; i <= n ; i ++ ) { Sum [ i ] = ( ( Sum [ i - 1 ] + ( 4 * Sum [ i - 2 ] ) % mod - ( 4 * Sum [ i - 3 ] ) % mod + mod ) % mod + ( Sum [ i - 4 ] - Sum [ i - 5 ] + mod ) % mod ) % mod ; } return Sum [ n ] ; } var n = 6 ; document . write ( sumOddFibonacci ( n ) ) ;
Minimize the total number of teddies to be distributed | Javascript implementation of the above approach ; Initializing one tablet for each student ; if left adjacent is having higher marks review and change all the dp values assigned before until assigned dp values are found wrong according to given constrains ; if right adjacent is having higher marks add one in dp of left adjacent and assign to right one ; n number of students ; marks of students ; solution of problem
function fun ( marks , n ) { let dp = new Array ( n ) ; let temp ; for ( let i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( marks [ i ] > marks [ i + 1 ] ) { temp = i ; while ( true ) { if ( ( marks [ temp ] > marks [ temp + 1 ] ) && temp >= 0 ) { if ( dp [ temp ] > dp [ temp + 1 ] ) { temp -= 1 ; continue ; } else { dp [ temp ] = dp [ temp + 1 ] + 1 ; temp -= 1 ; } } else break ; } } else if ( marks [ i ] < marks [ i + 1 ] ) dp [ i + 1 ] = dp [ i ] + 1 ; } let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += dp [ i ] ; return sum ; } let n = 6 ; let marks = [ 1 , 4 , 5 , 2 , 2 , 1 ] ; document . write ( fun ( marks , n ) ) ;
Number of ways to reach Nth floor by taking at | Javascript program to reach N - th stair by taking a maximum of K leap ; elements of combo [ ] stores the no . of possible ways to reach it by all combinations of k leaps or less ; assuming leap 0 exist and assigning its value to 1 for calculation ; loop to iterate over all possible leaps upto k ; ; in this loop we count all possible leaps to reach the jth stair with the help of ith leap or less ; if the leap is not more than the i - j ; calculate the value and store in combo [ j ] to reuse it for next leap calculation for the jth stair ; returns the no of possible number of leaps to reach the top of building of n stairs ; N i the no of total stairs K is the value of the greatest leap
function solve ( N , K ) { let combo = new Array ( 50 ) ; combo . fill ( 0 ) ; combo [ 0 ] = 1 ; for ( let i = 1 ; i <= K ; i ++ ) { for ( let j = 0 ; j <= N ; j ++ ) { if ( j >= i ) { combo [ j ] += combo [ j - i ] ; } } } return combo [ N ] ; } let N = 29 ; let K = 5 ; document . write ( solve ( N , K ) ) ; solve ( N , K ) ;
Find the Longest Increasing Subsequence in Circular manner | Utility method to find LIS using Dynamic programming ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Set j on the basis of current window i . e . first element of the current window ; Pick maximum of all LIS values ; Function to find Longest Increasing subsequence in Circular manner ; Make a copy of given array by appending same array elements to itself ; Perform LIS for each window of size n ; Driver Code
function computeLIS ( circBuff , start , end , n ) { let LIS = new Array ( n + end - start ) ; for ( let i = start ; i < end ; i ++ ) LIS [ i ] = 1 ; for ( let i = start + 1 ; i < end ; i ++ ) for ( let j = start ; j < i ; j ++ ) if ( circBuff [ i ] > circBuff [ j ] && LIS [ i ] < LIS [ j ] + 1 ) LIS [ i ] = LIS [ j ] + 1 ; let res = Number . MIN_VALUE ; for ( let i = start ; i < end ; i ++ ) res = Math . max ( res , LIS [ i ] ) ; return res ; } function LICS ( arr , n ) { let circBuff = new Array ( 2 * n ) ; for ( let i = 0 ; i < n ; i ++ ) circBuff [ i ] = arr [ i ] ; for ( let i = n ; i < 2 * n ; i ++ ) circBuff [ i ] = arr [ i - n ] ; let res = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) res = Math . max ( computeLIS ( circBuff , i , i + n , n ) , res ) ; return res ; } let arr = [ 1 , 4 , 6 , 2 , 3 ] ; document . write ( " " + LICS ( arr , arr . length ) ) ;
Counts paths from a point to reach Origin | utility min ; Function for binomial Coefficient ; Constructing Pascal 's Triangle ; Driver Code
function min ( a , b ) { return a < b ? a : b ; } function binomialCoeff ( n , k ) { var C = Array ( k + 1 ) . fill ( 0 ) ; C [ 0 ] = 1 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } var n = 3 , m = 2 ; document . write ( " " + binomialCoeff ( n + m , n ) ) ;
Longest Common Increasing Subsequence ( LCS + LIS ) | Returns the length and the LCIS of two arrays arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] ; table [ j ] is going to store length of LCIS ending with arr2 [ j ] . We initialize it as 0 , ; Traverse all elements of arr1 [ ] ; Initialize current length of LCIS ; For each element of arr1 [ ] , trvarse all elements of arr2 [ ] . ; If both the array have same elements . Note that we don 't  break the loop here. ; Now seek for previous smaller common element for current element of arr1 ; The maximum value in table [ ] is out result ; Driver Code
function LCIS ( arr1 , n , arr2 , m ) { let table = [ ] ; for ( let j = 0 ; j < m ; j ++ ) table [ j ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let current = 0 ; for ( let j = 0 ; j < m ; j ++ ) { if ( arr1 [ i ] == arr2 [ j ] ) if ( current + 1 > table [ j ] ) table [ j ] = current + 1 ; if ( arr1 [ i ] > arr2 [ j ] ) if ( table [ j ] > current ) current = table [ j ] ; } } let result = 0 ; for ( let i = 0 ; i < m ; i ++ ) if ( table [ i ] > result ) result = table [ i ] ; return result ; } let arr1 = [ 3 , 4 , 9 , 1 ] ; let arr2 = [ 5 , 3 , 8 , 9 , 10 , 2 , 1 ] ; let n = arr1 . length ; let m = arr2 . length ; document . write ( " " + LCIS ( arr1 , n , arr2 , m ) ) ;
Length of longest common prefix possible by rearranging strings in a given array | Function to get the length of the longest common prefix by rearranging the Strings ; freq [ i ] [ j ] : stores the frequency of a character ( = j ) in a String arr [ i ] ; Traverse the given array ; Stores length of current String ; Traverse current String of the given array ; Update the value of freq [ i ] [ arr [ i ] [ j ] ] ; Stores the length of longest common prefix ; Count the minimum frequency of each character in in all the Strings of arr [ ] ; Stores minimum value in each row of freq [ ] [ ] ; Calculate minimum frequency of current character in all the Strings . ; Update minRowVal ; Update maxLen ; Driver Code
function longComPre ( arr , N ) { let freq = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ i ] = new Array ( 256 ) ; for ( let j = 0 ; j < 256 ; j ++ ) { freq [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < N ; i ++ ) { let M = arr [ i ] . length ; for ( let j = 0 ; j < M ; j ++ ) { freq [ i ] [ arr [ i ] [ j ] . charCodeAt ( 0 ) ] ++ ; } } let maxLen = 0 ; for ( let j = 0 ; j < 256 ; j ++ ) { let minRowVal = Number . MAX_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { minRowVal = Math . min ( minRowVal , freq [ i ] [ j ] ) ; } maxLen += minRowVal ; } return maxLen ; } let arr = [ " " , " " , " " ] ; let N = 3 ; document . write ( longComPre ( arr , N ) ) ;
Remove characters from a String that appears exactly K times | Javascript program to remove characters from a String that appears exactly K times ; Function to reduce the String by removing the characters which appears exactly k times ; Hash table initialised to 0 ; Increment the frequency of the character ; To store answer ; Next index in reduced string ; Append the characters which appears exactly k times ; Driver code ; Function call
let MAX_CHAR = 26 ; function removeChars ( arr , k ) { let hash = Array . from ( { length : MAX_CHAR } , ( _ , i ) => 0 ) ; let n = arr . length ; for ( let i = 0 ; i < n ; ++ i ) hash [ arr [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let ans = " " ; for ( let i = 0 ; i < n ; ++ i ) { if ( hash [ arr [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] != k ) { ans += arr [ i ] ; } } return ans ; } let str = " " . split ( ' ' ) ; let k = 2 ; document . write ( removeChars ( str , k ) ) ;
Contiguous subsegments of a string having distinct subsequent characters | Function that prints the segments ; New array for every iteration ; Check if the character is in the array ; Driver code
function sub_segments ( str , n ) { let l = str . length ; for ( let x = 0 ; x < l ; x += n ) { let newlist = str . substr ( x , n ) ; let arr = [ ] ; for ( let y of newlist ) { if ( ! arr . includes ( y ) ) arr . push ( y ) ; } for ( let y of arr ) document . write ( y ) ; document . write ( " " ) ; } } let str = " " ; let n = 4 ; sub_segments ( str , n ) ;
Check if two strings are same ignoring their cases | Function to compare two strings ignoring their cases ; length of first string ; length of second string ; if length is not same simply return false since both string can not be same if length is not equal ; loop to match one by one all characters of both string ; if current characters of both string are same , increase value of i to compare next character ; if any character of first string is some special character or numeric character and not same as corresponding character of second string then return false ; do the same for second string ; this block of code will be executed if characters of both strings are of different cases ; compare characters by ASCII value ; if characters matched , increase the value of i to compare next char ; if all characters of the first string are matched with corresponding characters of the second string , then return true ; Function to print the same or not same if strings are equal or not equal ; Driver Code
function equalIgnoreCase ( str1 , str2 ) { let i = 0 ; let len1 = str1 . length ; let len2 = str2 . length ; if ( len1 != len2 ) return false ; while ( i < len1 ) { if ( str1 [ i ] == str2 [ i ] ) { i ++ ; } else if ( ! ( ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) { return false ; } else if ( ! ( ( str2 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str2 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) { return false ; } else { if ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) { if ( str1 [ i ] . charCodeAt ( ) - 32 != str2 [ i ] . charCodeAt ( ) ) return false ; } else if ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) { if ( str1 [ i ] . charCodeAt ( ) + 32 != str2 [ i ] . charCodeAt ( ) ) return false ; } i ++ ; return true ; function equalIgnoreCaseUtil ( str1 , str2 ) { let res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let str1 , str2 ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ;
Maximize the value of A by replacing some of its digits with digits of B | Function to return the maximized value of A ; Sort digits in ascending order ; j points to largest digit in B ; If all the digits of b have been used ; Current digit has been used ; Return the maximized value ; Driver code
function maxValue ( a , b ) { b . sort ( function ( x , y ) { return x - y ; } ) ; let n = a . length ; let m = b . length ; let j = m - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( j < 0 ) break ; if ( b [ j ] > a [ i ] ) { a [ i ] = b [ j ] ; j -- ; } } return ( a ) . join ( " " ) ; } let a = " " ; let b = " " ; document . write ( maxValue ( a . split ( " " ) , b . split ( " " ) ) ) ;
Count numbers in range such that digits in it and it 's product with q are unequal | Function to check if all of the digits in a number and it 's product with q  are unequal or not ; convert first number into string ; Insert elements from 1 st number to hash ; Calculate corresponding product ; Convert the product to string ; Using the hash check if any digit of product matches with the digits of input number ; If yes , return false ; else , return true ; Function to count numbers in the range [ l , r ] such that all of the digits of the number and it 's product with q are unequal ; check for every number between l and r ; Driver Code ; Function Call
function checkIfUnequal ( n , q ) { let s1 = n . toString ( ) ; let a = new Array ( 26 ) ; for ( let i = 0 ; i < a . length ; i ++ ) { a [ i ] = 0 ; } for ( let i = 0 ; i < s1 . length ; i ++ ) a [ s1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; let prod = n * q ; let s2 = prod . toString ( ) ; for ( let i = 0 ; i < s2 . length ; i ++ ) { if ( a [ s2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > 0 ) return false ; } return true ; } function countInRange ( l , r , q ) { let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { if ( checkIfUnequal ( i , q ) ) count ++ ; } return count ; } let l = 10 , r = 12 , q = 2 ; document . write ( countInRange ( l , r , q ) ) ;
Check if it is possible to rearrange a binary string with alternate 0 s and 1 s | function to check the binary string ; length of string ; count zero 's ; count one 's ; if length is even ; if length is odd
function is_possible ( s ) { let l = s . length ; let one = 0 , zero = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( s [ i ] == ' ' ) zero ++ ; else one ++ ; } if ( l % 2 == 0 ) return ( one == zero ) ; else return ( Math . abs ( one - zero ) == 1 ) ; } let s = " " ; if ( is_possible ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Frequency Measuring Techniques for Competitive Programming | Javascript program to count frequencies of array items ; Create an array to store counts . The size of array is limit + 1 and all values are initially 0 ; Traverse through string characters and count frequencies ; Driver Code
let limit = 255 ; function countFreq ( str ) { let count = new Array ( limit + 1 ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < str . length ; i ++ ) count [ str [ i ] . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i <= limit ; i ++ ) { if ( count [ i ] > 0 ) document . write ( String . fromCharCode ( i ) + " " + count [ i ] + " " ) ; } } let str = " " ; countFreq ( str ) ;
gOOGLE cASE of a given sentence | Function to remove spaces and convert into camel case ; check for spaces in the sentence ; conversion into upper case ; If not space , copy character ; return string to main ; Driver code
function convert ( s ) { var n = s . length ; var s1 = " " ; s1 = s1 + s . charAt ( 0 ) . toLowerCase ( ) ; for ( i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ' && i < n ) { s1 = s1 + " " + s . charAt ( i + 1 ) . toLowerCase ( ) ; i ++ ; } else s1 = s1 + s . charAt ( i ) . toUpperCase ( ) ; } return s1 ; } var str = " " ; document . write ( convert ( str ) ) ;