codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Given Input ; Function call to count pairs satisfying the given condition
function cntPairs ( arr , N ) { var res = 0 ; var bit = Array ( 32 ) . fill ( 0 ) ; var i ; for ( i = 0 ; i < N ; i ++ ) { var pos = Math . ceil ( Math . log2 ( arr [ i ] ) ) ; bit [ pos ] += 1 ; } for ( i = 0 ; i < 32 ; i ++ ) { res += Math . ceil ( ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ) ; } return res ; } arr = [ 1 , 2 , 3 , 4 ] ; N = arr . length ; document . write ( cntPairs ( arr , N ) ) ;
Minimum MEX from all subarrays of length K | Function to return minimum MEX from all K - length subarrays ; Stores element from [ 1 , N + 1 ] which are not present in subarray ; Store number 1 to N + 1 in set s ; Find the MEX of K - length subarray starting from index 0 ; Find the MEX of all subarrays of length K by erasing arr [ i ] and inserting arr [ i - K ] ; Store first element of set ; Updating the mex ; Print minimum MEX of all K length subarray ; Driver code
function minimumMEX ( arr , N , K ) { let s = new Set ( ) ; for ( let i = 1 ; i <= N + 1 ; i ++ ) s . add ( i ) ; for ( let i = 0 ; i < K ; i ++ ) s . delete ( arr [ i ] ) ; let entry = s . entries ( ) ; let mex = 1 ; for ( let i = K ; i < N ; i ++ ) { s . delete ( arr [ i ] ) ; s . add ( arr [ i - K ] ) ; let firstElem = entry . next ( ) . value mex = Math . min ( mex , 1 ) ; } document . write ( mex + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let K = 3 ; let N = arr . length ; minimumMEX ( arr , N , K ) ;
Count smaller elements present in the array for each array element | Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Initialize sum with 0 ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code
function smallerNumbers ( arr , N ) { let hash = new Array ( 100000 ) ; hash . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) hash [ arr [ i ] ] ++ ; let sum = 0 ; for ( let i = 1 ; i < 100000 ; i ++ ) { hash [ i ] += hash [ i - 1 ] ; } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { document . write ( " " + " " ) ; continue ; } document . write ( hash [ arr [ i ] - 1 ] + " " ) ; } } let arr = [ 3 , 4 , 1 , 1 , 2 ] ; let N = arr . length ; smallerNumbers ( arr , N ) ;
Modulo Operations in Programming With Negative Results | Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
function truncMod ( a , n ) { let q = Math . round ( a / n ) ; return a - ( n * q ) ; } let a , b ; a = 9 ; b = 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ; a = - 9 ; b = 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ; a = 9 ; b = - 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ; a = - 9 ; b = - 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ;
Program for average of an array without running into overflow | Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Input ; Function call
function average ( arr , N ) { var sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum > 2147483647 ) { sum = - 2147483647 + ( sum - 2147483649 ) } return parseInt ( sum / N ) ; } function mean ( arr , N ) { var avg = 0 ; for ( var i = 0 ; i < N ; i ++ ) { avg += parseFloat ( ( arr [ i ] - avg ) / ( i + 1 ) ) ; } return avg ; } var arr = [ 2147483647 , 1 , 2 ] ; var N = arr . length document . write ( " " + average ( arr , N ) . toFixed ( 10 ) + " " ) ; document . write ( " " + mean ( arr , N ) . toFixed ( 10 ) + " " ) ;
Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
function countPairs ( arr , N ) { var count = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { var val = 1.0 * arr [ i ] ; var idx = 1.0 * ( i + 1 ) ; count += mp . has ( val / idx ) ? mp . get ( val / idx ) : 0 if ( mp . has ( val / idx ) ) mp . set ( val / idx , mp . get ( val / idx ) + 1 ) else mp . set ( val / idx , 1 ) } document . write ( count ) ; } var arr = [ 1 , 3 , 5 , 6 , 5 ] ; var N = arr . length ; countPairs ( arr , N ) ;
Count ancestors with smaller value for each node of a Binary Tree | Function to add an edge between nodes u and v ; Function to perform the DFS Traversal and store parent of each node ; Store the immediate parent ; Traverse the children of the current node ; Recursively call for function dfs for the child node ; Function to count the number of ancestors with values smaller than that of the current node ; Stores the parent of each node ; Perform the DFS Traversal ; Traverse all the nodes ; Store the number of ancestors smaller than node ; Loop until parent [ node ] != - 1 ; If the condition satisfies , increment cnt by 1 ; Print the required result for the current node ; Driver Code ; Tree Formation
function add_edge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function dfs ( parent , adj , u , par = - 1 ) { parent [ u ] = par ; adj [ u ] . forEach ( child => { if ( child != par ) dfs ( parent , adj , child , u ) ; } ) ; } function countSmallerAncestors ( adj , n ) { var parent = Array ( 100000 ) . fill ( 0 ) ; dfs ( parent , adj , 1 ) ; for ( var i = 1 ; i <= n ; i ++ ) { var node = i ; var cnt = 0 ; while ( parent [ node ] != - 1 ) { if ( parent [ node ] < i ) cnt += 1 ; node = parent [ node ] ; } document . write ( cnt + " " ) ; } } var N = 6 ; var adj = Array . from ( Array ( 100000 ) , ( ) => Array ( ) ) ; add_edge ( adj , 1 , 5 ) ; add_edge ( adj , 1 , 4 ) ; add_edge ( adj , 4 , 6 ) ; add_edge ( adj , 5 , 3 ) ; add_edge ( adj , 5 , 2 ) ; countSmallerAncestors ( adj , N ) ;
Count subsequences having odd Bitwise XOR values from an array | Function to count the subsequences having odd bitwise XOR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array A [ ] ; If el is odd ; If count of odd elements is 0 ; Given array A [ ] ; Function call to count subsequences having odd bitwise XOR value
function countSubsequences ( A ) { var odd = 0 ; var even = 0 ; for ( var e1 = 0 ; e1 < A . length ; e1 ++ ) { if ( A [ e1 ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 ) document . write ( 0 ) ; else document . write ( ( 1 << ( A . length - 1 ) ) ) ; } var A = [ 1 , 3 , 4 ] ; countSubsequences ( A ) ;
Maximum subarray product modulo M | Function to find maximum subarray product modulo M and minimum length of the subarray ; Stores maximum subarray product modulo M and minimum length of the subarray ; Stores the minimum length of subarray having maximum product ; Traverse the array ; Stores the product of a subarray ; Calculate Subarray whose start index is i ; Multiply product by arr [ i ] ; If product greater than ans ; Update ans ; Update length ; Print maximum subarray product mod M ; Print minimum length of subarray having maximum product ; Driver Code
function maxModProdSubarr ( arr , n , M ) { var ans = 0 ; var length = n ; for ( i = 0 ; i < n ; i ++ ) { var product = 1 ; for ( j = i ; j < n ; j ++ ) { product = ( product * arr [ i ] ) % M ; if ( product > ans ) { ans = product ; if ( length > j - i + 1 ) { length = j - i + 1 ; } } } } document . write ( " " + ans + " " ) ; document . write ( " " + " " + length ) ; } var arr = [ 2 , 3 , 4 , 2 ] ; var N = arr . length ; var M = 5 ; maxModProdSubarr ( arr , N , M ) ;
Number of co | JavaScript program for the above approach ; Function to check whether given integers are co - prime or not ; Utility function to count number of co - prime pairs ; Traverse the array ; If co - prime ; Increment count ; Return count ; Function to count number of co - prime pairs ; Stores digits in string form ; Sort the list ; Keep two copies of list l ; Generate 2 digit numbers using d1 and d2 ; If current number does not exceed N ; Stores length of list ; Stores number of co - prime pairs ; Print number of co - prime pairs ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs
function GCD ( a , b ) { return b == 0 ? a : GCD ( b , a % b ) ; } function coprime ( a , b ) { if ( GCD ( a , b ) == 1 ) return true ; return false ; } function numOfPairs ( arr , N ) { let count = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( coprime ( parseInt ( arr [ i ] ) , parseInt ( arr [ j ] ) ) ) { count = count + 1 ; } } } return count ; } function noOfCoPrimePairs ( N , d1 , d2 ) { let l = [ ] ; l . push ( d1 . toString ( ) ) ; l . push ( d2 . toString ( ) ) ; l . sort ( ) ; if ( N < parseInt ( l [ 1 ] ) ) return ; let total = [ ... l ] ; let temp2 = [ ... l ] ; let flag = 0 ; let temp3 = [ ] ; while ( l [ 0 ] . length < 10 ) { for ( let i = 0 ; i < l . length ; i ++ ) { for ( let j = 0 ; j < 2 ; j ++ ) { if ( parseInt ( l [ i ] + temp2 [ j ] ) > N ) { flag = 1 ; break ; } total . push ( l [ i ] + temp2 [ j ] ) ; temp3 . push ( l [ i ] + temp2 [ j ] ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = [ ... temp3 ] ; temp3 = [ ] ; } let lenOfTotal = total . length ; let ans = numOfPairs ( total , lenOfTotal ) ; document . write ( ans ) ; } let N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ;
Minimize cost of placing tiles of dimensions 2 * 1 over a Matrix | Function to find the minimum cost in placing N tiles in a grid M [ ] [ ] ; Stores the minimum profit after placing i tiles ; Traverse the grid [ ] [ ] ; Update the orig_cost ; Traverse over the range [ 2 , N ] ; Place tiles horizentally or vertically ; Print the answer ; Driver Code
function tile_placing ( grid , N ) { let dp = new Array ( N + 5 ) ; dp . fill ( 0 ) ; let orig_cost = 0 ; for ( let i = 0 ; i < 2 ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { orig_cost += grid [ i ] [ j ] ; } } dp [ 0 ] = 0 ; dp [ 1 ] = Math . abs ( grid [ 0 ] [ 0 ] - grid [ 1 ] [ 0 ] ) ; for ( let i = 2 ; i <= N ; i ++ ) { dp [ i ] = Math . max ( dp [ i - 1 ] + Math . abs ( grid [ 0 ] [ i - 1 ] - grid [ 1 ] [ i - 1 ] ) , dp [ i - 2 ] + Math . abs ( grid [ 0 ] [ i - 2 ] - grid [ 0 ] [ i - 1 ] ) + Math . abs ( grid [ 1 ] [ i - 2 ] - grid [ 1 ] [ i - 1 ] ) ) ; } document . write ( ( orig_cost - dp [ N ] ) + " " ) ; } let M = [ [ 7 , 5 , 1 , 3 ] , [ 8 , 6 , 0 , 2 ] ] ; let N = M [ 0 ] . length ; tile_placing ( M , N ) ;
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing two elements ; Stores sum of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no pair exists , print - 1 ; Given array ; Size of the array
function findSplit ( arr , N ) { for ( let l = 1 ; l <= N - 4 ; l ++ ) { for ( let r = l + 2 ; r <= N - 2 ; r ++ ) { let lsum = 0 , rsum = 0 , msum = 0 ; for ( let i = 0 ; i <= l - 1 ; i ++ ) { lsum += arr [ i ] ; } for ( let i = l + 1 ; i <= r - 1 ; i ++ ) { msum += arr [ i ] ; } for ( let i = r + 1 ; i < N ; i ++ ) { rsum += arr [ i ] ; } if ( lsum == rsum && rsum == msum ) { document . write ( l + " " + r + " " ) ; return ; } } } document . write ( - 1 ) ; } let arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] ; let N = arr . length ; findSplit ( arr , N ) ;
Count the number of times a Bulb switches its state | Function to find the number of times a bulb switches its state ; Count of 1 s ; Traverse the array ; Update count of 1 s ; Update the status of bulb ; Traverse the array Q [ ] ; Stores previous state of the bulb ; Toggle the switch and update count of 1 s ; If the bulb switches state ; Return count ; Input ; Queries ; Function call to find number of times the bulb toggles
function solve ( A , n , Q , q ) { var one = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( A [ i ] == 1 ) one ++ ; var glows = 0 , count = 0 ; if ( one >= Math . ceil ( n / 2 ) ) glows = 1 ; for ( var i = 0 ; i < q ; i ++ ) { var prev = glows ; if ( A [ Q [ i ] - 1 ] == 1 ) one -- ; if ( A [ Q [ i ] - 1 ] == 0 ) one ++ ; A [ Q [ i ] - 1 ] ^= 1 ; if ( one >= Math . ceil ( n / 2.0 ) ) { glows = 1 ; } else { glows = 0 ; } if ( prev != glows ) count ++ ; } return count ; } var n = 3 ; var arr = [ 1 , 1 , 0 ] ; var q = 3 ; var Q = [ 3 , 2 , 1 ] ; document . write ( solve ( arr , n , Q , q ) ) ;
Count array elements having sum of digits equal to K | Function to calculate the sum of digits of the number N ; Stores the sum of digits ; Return the sum ; Function to count array elements ; Store the count of array elements having sum of digits K ; Traverse the array ; If sum of digits is equal to K ; Increment the count ; Print the count ; Given array ; Given value of K ; Size of the array ; Function call to count array elements having sum of digits equal to K
function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum += N % 10 ; N = parseInt ( N / 10 , 10 ) ; } return sum ; } function elementsHavingDigitSumK ( arr , N , K ) { let count = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( sumOfDigits ( arr [ i ] ) == K ) { count ++ ; } } document . write ( count ) ; } let arr = [ 23 , 54 , 87 , 29 , 92 , 62 ] ; let K = 11 ; let N = arr . length ; elementsHavingDigitSumK ( arr , N , K ) ;
The Stock Span Problem | method to calculate stock span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; A utility function to print elements of array ; Driver code ; Fill the span values in array S [ ] ; print the calculated span values
function calculateSpan ( price , n , S ) { S [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { S [ i ] = 1 ; for ( let j = i - 1 ; ( j >= 0 ) && ( price [ i ] >= price [ j ] ) ; j -- ) S [ i ] ++ ; } } function printArray ( arr ) { let result = arr . join ( " " ) ; document . write ( result ) ; } let price = [ 10 , 4 , 5 , 90 , 120 , 80 ] ; let n = price . length ; let S = new Array ( n ) ; S . fill ( 0 ) ; calculateSpan ( price , n , S ) ; printArray ( S ) ;
Next Greater Element | prints element and NGE pair for all elements of arr [ ] of size n ; Driver Code
function printNGE ( arr , n ) { var next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { next = arr [ j ] ; break ; } } document . write ( arr [ i ] + " " + next ) ; document . write ( " " ) ; } } var arr = [ 11 , 13 , 21 , 3 ] ; var n = arr . length ; printNGE ( arr , n ) ;
Number of NGEs to the right |  ; use of stl stack in c ++ ; push the ( n - 1 ) th index to the stack ; traverse in reverse order ; if no element is greater than arr [ i ] the number of NGEs to right is 0 ; number of NGEs to right of arr [ i ] is one greater than the number of NGEs to right of higher number to its right ; reverse again because values are in reverse order ; returns the vector of number of next greater elements to the right of each index . ; Driver code ; query 1 answered ; query 2 answered ; query 3 answered
function no_NGN ( arr , n ) { var nxt = [ ] ; var s = [ ] ; nxt . push ( 0 ) ; s . push ( n - 1 ) ; for ( var i = n - 2 ; i >= 0 ; i -- ) { while ( s . length != 0 && arr [ i ] >= arr [ s [ s . length - 1 ] ] ) s . pop ( ) ; if ( s . length == 0 ) nxt . push ( 0 ) ; else nxt . push ( nxt [ n - s [ s . length - 1 ] - 1 ] + 1 ) ; s . push ( i ) ; } nxt . reverse ( ) ; return nxt ; } var n = 8 ; var arr = [ 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 ] ; var nxt = no_NGN ( arr , n ) ; document . write ( nxt [ 3 ] + " " ) ; document . write ( nxt [ 6 ] + " " ) ; document . write ( nxt [ 1 ] + " " ) ;
Sorting array using Stacks | This function return the sorted stack ; pop out the first element ; while temporary stack is not empty and top of stack is smaller than temp ; pop from temporary stack and push it to the input stack ; push temp in tempory of stack ; Push array elements to stack ; Sort the temporary stack ; Put stack elements in arrp [ ] ; main function
function sortStack ( input ) { var tmpStack = [ ] ; while ( input . length != 0 ) { var tmp = input [ input . length - 1 ] ; input . pop ( ) ; while ( tmpStack . length != 0 && tmpStack [ tmpStack . length - 1 ] < tmp ) { input . push ( tmpStack [ tmpStack . length - 1 ] ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } return tmpStack ; } function sortArrayUsingStacks ( arr , n ) { var input = [ ] ; for ( var i = 0 ; i < n ; i ++ ) input . push ( arr [ i ] ) ; var tmpStack = sortStack ( input ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ i ] = tmpStack [ tmpStack . length - 1 ] ; tmpStack . pop ( ) ; } } var arr = [ 10 , 5 , 15 , 45 ] ; var n = arr . length ; sortArrayUsingStacks ( arr , n ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Program for Tower of Hanoi | javascript recursive function to solve tower of hanoi puzzle ; Number of disks ; A , B and C are names of rods
function towerOfHanoi ( n , from_rod , to_rod , aux_rod ) { if ( n == 1 ) { document . write ( " " + from_rod + " " + to_rod + " " ) ; return ; } towerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) ; document . write ( " " + n + " " + from_rod + " " + to_rod + " " ) ; towerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) ; } var n = 4 ; towerOfHanoi ( n , ' ' , ' ' , ' ' ) ;
Find maximum of minimum for every window size in a given array | A naive method to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Prvar max of min for current window size ; Driver method
var arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ] ; function printMaxOfMin ( n ) { for ( k = 1 ; k <= n ; k ++ ) { var maxOfMin = Number . MIN_VALUE ; for ( i = 0 ; i <= n - k ; i ++ ) { var min = arr [ i ] ; for ( j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; } if ( min > maxOfMin ) maxOfMin = min ; } document . write ( maxOfMin + " " ) ; } } printMaxOfMin ( arr . length ) ;
Find maximum of minimum for every window size in a given array | An efficient Javascript program to find maximum of all minimums of windows of different size ; Used to find previous and next smaller ; Arrays to store previous and next smaller ; Initialize elements of left [ ] and right [ ] ; Fill elements of left [ ] using logic discussed on www . geeksforgeeks . org / next - greater - element / https : ; Empty the stack as stack is going to be used for right [ ] ; Fill elements of right [ ] using same logic ; Create and initialize answer array ; Fill answer array by comparing minimums of all lengths computed using left [ ] and right [ ] ; length of the interval ; arr [ i ] is a possible answer for this length ' len ' interval , check x if arr [ i ] is more than max for ' len ' ; Some entries in ans [ ] may not be filled yet . Fill them by taking values from right side of ans [ ] ; Print the result
let arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ] ; function printMaxOfMin ( n ) { let s = [ ] ; let left = new Array ( n + 1 ) ; left . fill ( 0 ) ; let right = new Array ( n + 1 ) ; right . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { left [ i ] = - 1 ; right [ i ] = n ; } for ( let i = 0 ; i < n ; i ++ ) { while ( s . length > 0 && arr [ s [ s . length - 1 ] ] >= arr [ i ] ) { s . pop ( ) ; } if ( s . length > 0 ) { left [ i ] = s [ s . length - 1 ] ; } s . push ( i ) ; } while ( s . length > 0 ) { s . pop ( ) ; } for ( let i = n - 1 ; i >= 0 ; i -- ) { while ( s . length > 0 && arr [ s [ s . length - 1 ] ] >= arr [ i ] ) { s . pop ( ) ; } if ( s . length > 0 ) { right [ i ] = s [ s . length - 1 ] ; } s . push ( i ) ; } let ans = new Array ( n + 1 ) ; ans . fill ( 0 ) ; for ( let i = 0 ; i <= n ; i ++ ) { ans [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let len = right [ i ] - left [ i ] - 1 ; ans [ len ] = Math . max ( ans [ len ] , arr [ i ] ) ; } for ( let i = n - 1 ; i >= 1 ; i -- ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] ) ; } for ( let i = 1 ; i <= n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } printMaxOfMin ( arr . length ) ;
Length of the longest valid substring | Function to return the length of the longest valid substring ; Variables for left and right counter . maxlength to store the maximum length found so far ; Iterating the string from left to right ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Iterating the string from right to left ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Function call
function solve ( s , n ) { let left = 0 , right = 0 , maxlength = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = max ( maxlength , 2 * right ) ; else if ( right > left ) left = right = 0 ; } left = right = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . max ( maxlength , 2 * left ) ; else if ( left > right ) left = right = 0 ; } return maxlength ; } document . write ( solve ( " " , 16 ) ) ;