codeparrot/xlcost-text-to-code huggingface dataset:
Minimize steps required to move all 1 's in a matrix to a given index | Function to calculate and return the minimum number of steps required to move all 1 s to ( X , Y ) ; Iterate the given matrix ; Update the answer with minimum moves required for the given element to reach the given index ; Return the number of steps ; Given matrix ; Given position ; Function Call
function findAns ( mat , x , y , n , m ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { ans += Math . abs ( x - i ) + Math . abs ( y - j ) ; } } } return ans ; } let mat = [ [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 1 ] ] ; let x = 0 , y = 2 ; document . write ( findAns ( mat , x , y , mat . length , mat [ 0 ] . length ) + " " ) ;
Count of all possible reverse bitonic subarrays | Function that counts all the reverse bitonic subarray in arr [ ] ; To store the count of reverse bitonic subarray ; Iterate the array and select the starting element ; Iterate for selecting the ending element for subarray ; Subarray arr [ i to j ] ; For 1 length , increment the count and continue ; For decreasing subarray ; Check if only decreasing ; For increasing subarray ; Print the total count of subarrays ; Given array arr [ ] ; Function Call
function countReversebitonic ( arr , n ) { let c = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } let k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k <= j && temp < arr [ k ] && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } document . write ( c + " " ) ; } let arr = [ 2 , 3 , 1 , 4 ] ; let N = arr . length ; countReversebitonic ( arr , N ) ;
Longest increasing sequence by the boundary elements of an Array | Function to return the length of Longest Increasing subsequence ; Set pointers at both ends ; Stores the recent value added to the subsequence ; Stores the length of the subsequence ; Check if both elements can be added to the subsequence ; Check if the element on the left can be added to the subsequence only ; Check if the element on the right can be added to the subsequence only ; If none of the values can be added to the subsequence ; Driver Code ; Length of array
function longestSequence ( n , arr ) { var l = 0 , r = n - 1 ; var prev = - 1000000000 ; var ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } var arr = [ 3 , 5 , 1 , 4 , 2 ] ; var n = arr . length ; document . write ( longestSequence ( n , arr ) ) ;
Total number of cells covered in a matrix after D days | Function to return the total infected cells after d days ; Top extension ; Bottom extension ; Left extension ; Right extension ; Calculating the cells in each quadrilateral ; Sum all of them to get total cells in each quadrilateral ; Add the singleblocks along the lines of top , down , left , right ; Return the ans ; Dimensions of cell ; Starting coordinates ; Number of days ; Function call
function solve ( n , m , x , y , d ) { let top = Math . min ( d , x - 1 ) ; let down = Math . min ( d , n - x ) ; let left = Math . min ( d , y - 1 ) ; let right = Math . min ( d , m - y ) ; let quad1 = top * left ; let quad2 = left * down ; let quad3 = down * right ; let quad4 = right * top ; let totalsq = quad1 + quad2 + quad3 + quad4 ; let singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } let n = 10 , m = 10 ; let x = 7 , y = 8 ; let d = 4 ; d -- ; document . write ( solve ( n , m , x , y , d ) ) ;
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Javascript implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Drive Code ; Function Call
var M = 3 var N = 3 ; function minchanges ( mat ) { var count = 0 ; var left = 0 , right = N + M - 2 ; while ( left < right ) { var mp = new Map ( ) ; var totalsize = 0 ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { if ( i + j == left i + j == right ) { if ( mp . has ( mat [ i ] [ j ] ) ) mp . set ( mat [ i ] [ j ] , mp . get ( mat [ i ] [ j ] ) + 1 ) else mp . set ( mat [ i ] [ j ] , 1 ) totalsize ++ ; } } } var changes = 0 ; mp . forEach ( ( value , key ) => { changes = Math . max ( changes , value ) ; } ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } var mat = [ [ 1 , 4 , 1 ] , [ 2 , 5 , 3 ] , [ 1 , 3 , 1 ] ] ; document . write ( minchanges ( mat ) ) ;
Count of longest possible subarrays with sum not divisible by K | Function to find the count of longest subarrays with sum not divisible by K ; Sum of all elements in an array ; If overall sum is not divisible then return 1 , as only one subarray of size n is possible ; Index of the first number not divisible by K ; Index of the last number not divisible by K ; Subarray doesn 't exist ; Sum of the window ; Calculate the sum of rest of the windows of size len ; Driver Code
function CountLongestSubarrays ( arr , n , k ) { let i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( ( s % k ) != 0 ) { return 1 ; } else { let ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } let fin = n - 1 ; while ( fin >= 0 && arr [ fin ] % k == 0 ) { -- fin ; } let len , sum = 0 , count = 0 ; if ( ini == n ) { return - 1 ; } else { len = Math . max ( n - 1 - ini , fin ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } let arr = [ 3 , 2 , 2 , 2 , 3 ] ; let n = arr . length ; let k = 3 ; document . write ( CountLongestSubarrays ( arr , n , k ) ) ;
Count of triplets in a given Array having GCD K | Javascript program to count the number of triplets in the array with GCD equal to K ; frequency array ; mul [ i ] stores the count of multiples of i ; cnt [ i ] stores the count of triplets with gcd = i ; Return nC3 ; Function to count and return the number of triplets in the array with GCD equal to K ; Store frequency of array elements ; Store the multiples of i present in the array ; Count triplets with gcd equal to any multiple of i ; Remove all triplets which have gcd equal to a multiple of i ; Driver Program
var MAXN = 1000001 ; var freq = Array ( MAXN ) . fill ( 0 ) ; var mul = Array ( MAXN ) . fill ( 0 ) ; var cnt = Array ( MAXN ) . fill ( 0 ) ; function nC3 ( n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } function count_triplet ( arr , N , K ) { for ( var i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( var i = 1 ; i <= 1000000 ; i ++ ) { for ( var j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( var i = 1000000 ; i >= 1 ; i -- ) { for ( var j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } document . write ( " " + " " + K ) ; document . write ( " " + cnt [ K ] ) ; } var arr = [ 1 , 7 , 12 , 6 , 15 , 9 ] ; var N = 6 , K = 3 ; count_triplet ( arr , N , K ) ;
Check if a subsequence of length K with odd sum exists | Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd elements exists or no even elements exists when K is even ; Subsequence is not possible ; Possible otherwise ; Driver Code
function isSubseqPossible ( arr , N , K ) { let i ; let odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } let arr = [ 2 , 3 , 5 , 7 , 4 ] ; let N = arr . length ; let K = 3 ; document . write ( isSubseqPossible ( arr , N , K ) ? " " : " " ) ;
Path traversed using exactly M coins in K jumps | Function that print the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver Code ; Function Call
function print_path ( N , jump , coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { document . write ( " " ) ; } else { let pos = 1 ; while ( jump > 0 ) { let tmp = Math . min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } document . write ( pos + " " ) ; ; coin -= tmp ; jump -= 1 ; } } } let N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ;
Minimum salary hike for each employee such that no employee feels unfair | Javascript program for the above approach ; Function that print minimum hike ; Insert INF at begin and end of array ; To store hike of each employee ; for Type 1 employee ; For Type 2 employee ; For Type 3 employee ; For Type 4 employee ; Print the min hike for each employee ; Given array of rating of employees ; Function Call
let INF = 10000009 ; function findMinHike ( arr , n ) { arr . unshift ( INF ) ; arr . push ( INF ) ; let hike = new Array ( n + 2 ) ; for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = Math . max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( let i = 1 ; i <= n ; i ++ ) { document . write ( hike [ i ] + " " ) ; } } let arr = [ 5 , 3 , 4 , 2 , 1 , 6 ] ; findMinHike ( arr , arr . length ) ;
Minimize the cost to make all the adjacent elements distinct in an Array | Function that prints minimum cost required ; Dp - table ; Base case Not increasing the first element ; Increasing the first element by 1 ; Increasing the first element by 2 ; Condition if current element is not equal to previous non - increased element ; Condition if current element is not equal to previous element after being increased by 1 ; Condition if current element is not equal to previous element after being increased by 2 ; Take the minimum from all cases ; Finding the minimum cost ; Printing the minimum cost required to make all adjacent elements distinct ; Driver Code
function minimumCost ( arr , cost , N ) { var dp = Array ( N ) . fill ( ) . map ( ( ) => Array ( 3 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { var minimum = parseInt ( 1e6 ) ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } var ans = parseInt ( 1e6 ) ; for ( i = 0 ; i < 3 ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; document . write ( ans + " " ) ; } var arr = [ 1 , 1 , 2 , 2 , 3 , 4 ] ; var cost = [ 3 , 2 , 5 , 4 , 2 , 1 ] ; var N = arr . length ; minimumCost ( arr , cost , N ) ;
Subarray with largest sum after excluding its maximum element | Function to find the maximum sum subarray by excluding the maximum element from the array ; Loop to store all the positive elements in the map ; Loop to iterating over the map and considering as the maximum element of the current including subarray ; Make the current element maximum ; Iterate through array and apply kadane 's algorithm ; Condition if current element is greater than mx then make the element - infinity ; Store the indices in some variable ; Driver Code ; Function Call
function maximumSumSubarray ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 ) ; } var first = 0 ; var last = 0 ; var ans = 0 ; var INF = 1000000 ; mp . forEach ( ( value , key ) => { var mx = key ; var curr = 0 ; var curr_start ; for ( var j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; var val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } ) ; document . write ( first + 1 + " " + ( last + 1 ) ) ; } var arr = [ 5 , - 2 , 10 , - 1 , 4 ] ; var size = arr . length ; maximumSumSubarray ( arr , size ) ;
Extended Knapsack Problem | To store the dp values ; for each element given ; For each possible weight value ; For each case where the total elements are less than the constraint ; To ensure that we dont go out of the array ; Driver Code
var dp = Array . from ( Array ( 100 ) , ( ) => Array ( 100 ) ) ; for ( var i = 0 ; i < 100 ; i ++ ) for ( var j = 0 ; j < 100 ; j ++ ) dp [ i ] [ j ] = new Array ( 100 ) . fill ( 0 ) ; function maxProfit ( profit , weight , n , max_W , max_E ) { for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= max_W ; j ++ ) { for ( var k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = Math . max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } var n = 5 ; var profit = [ 2 , 7 , 1 , 5 , 3 ] ; var weight = [ 2 , 5 , 2 , 3 , 4 ] ; var max_weight = 8 ; var max_element = 2 ; document . write ( maxProfit ( profit , weight , n , max_weight , max_element ) + " " ) ;
Minimum insertions to make XOR of an Array equal to half of its sum | Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver Code
function make_xor_half ( arr ) { let sum = 0 , xr = 0 ; for ( let a = 0 ; a < arr . length ; a ++ ) { sum += arr [ a ] ; xr ^= arr [ a ] ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . push ( sum ) ; return 1 ; } arr . push ( xr ) ; arr . push ( sum + xr ) ; return 2 ; } let N = 7 ; let nums = [ 3 , 4 , 7 , 1 , 2 , 5 , 6 ] ; let count = make_xor_half ( nums ) ; if ( count == - 1 ) document . write ( " " ) ; else if ( count == 1 ) document . write ( nums [ N ] + " " ) ; else document . write ( nums [ N ] + " " + nums [ N + 1 ] ) ;
Count number of triangles possible for the given sides range | Function to count the number of possible triangles for the given sides ranges ; Iterate for every possible of x ; Range of y is [ b , c ] From this range First we will find the number of x + y greater than d ; For x + y greater than d we can choose all z from [ c , d ] Total permutation will be ; Now we will find the number of x + y in between the [ c , d ] ; [ l , r ] will be the range from total [ c , d ] x + y belongs For any r such that r = x + y We can choose z , in the range [ c , d ] only less than r , Thus total permutation be ; Driver Code
function count_triangles ( a , b , c , d ) { var ans = 0 ; for ( x = a ; x <= b ; ++ x ) { var num_greater_than_d = Math . max ( d , c + x ) - Math . max ( d , b + x - 1 ) ; ans += num_greater_than_d * ( d - c + 1 ) ; var r = Math . min ( Math . max ( c , c + x ) , d ) - c ; var l = Math . min ( Math . max ( c , b + x - 1 ) , d ) - c ; var x1 = ( r * ( r + 1 ) ) / 2 ; var x2 = ( l * ( l + 1 ) ) / 2 ; ans += x1 - x2 ; } return ans ; } var a = 2 , b = 3 , c = 4 , d = 5 ; document . write ( count_triangles ( a , b , c , d ) ) ;
Minimum number of swaps required to make the string K periodic | Javascript code for the above approach . ; Mark all allowed characters as true ; Initialize the freq array to 0 ; Increase the frequency of each character ; Total number of periods of size K in the String ; Check if the current character is present in allowed and whether the current frequency is greater than all previous frequencies for this position ; Update the answer by subtracting the maxfrequency from total positions if there exist extra character at the end of the String apart from the n / k characters then add 1. ; Driver code
function minFlip ( s , n , k , a , p ) { let allowed = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < p ; i ++ ) { allowed [ a [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = true ; } let freq = new Array ( k ) ; for ( var i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < k ; i ++ ) for ( let j = 0 ; j < 26 ; j ++ ) freq [ i ] [ j ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { freq [ i % k ] [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] += 1 ; } let ans = 0 ; let totalpositions = Math . floor ( n / k ) ; for ( let i = 0 ; i < k ; i ++ ) { let maxfrequency = 0 ; for ( let j = 0 ; j < 26 ; j ++ ) { if ( freq [ i ] [ j ] > maxfrequency && allowed [ j ] == true ) maxfrequency = freq [ i ] [ j ] ; } ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } document . write ( ans + " " ) ; } let S = " " ; let n = S . length ; let K = 3 ; let A = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let p = A . length ; minFlip ( S , n , K , A , p ) ;
Minimum number of colors required to color a Circular Array | Function that finds minimum number of colors required ; To check that if all the elements are same or not ; To check if only one adjacent exist ; Traverse the array ; If adjacent elements found different means all are not same ; If two adjacent elements found to be same then make one_adjacent_same true ; If all elements are same then print 1 ; If total number of elements are even or there exist two adjacent elements that are same then print 2 ; Else 3 type of colors are required ; Driver Code ; Function call
function colorRequired ( arr , n ) { var all_same = true ; var one_adjacent_same = false ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { document . write ( 1 + " " ) ; return ; } if ( n % 2 == 0 one_adjacent_same == true ) { document . write ( 2 + " " ) ; return ; } document . write ( 3 + " " ) ; } var arr = [ 1 , 2 , 1 , 1 , 2 ] ; var n = arr . length ; colorRequired ( arr , n ) ;
Maximize the sum of sum of the Array by removing end elements | Function to find the maximum sum of sum ; compute the sum of whole array ; Traverse and remove the minimum value from an end to maximum the sum value ; If the left end element is smaller than right end ; remove the left end element ; If the right end element is smaller than left end ; remove the right end element ; Add the remaining element sum in the result ; Return the maximum sum of sum ; Driver code
function maxRemainingSum ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; var i = 0 ; var j = n - 1 ; var result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } var arr = [ 3 , 1 , 7 , 2 , 1 ] ; var N = arr . length ; document . write ( maxRemainingSum ( arr , N ) ) ;
Split a number as sum of K numbers which are not divisible by K | Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code
function printKParts ( N , K ) { if ( N % K == 0 ) { for ( var i = 1 ; i < K ; i ++ ) document . write ( " " ) ; document . write ( ( N - ( K - 1 ) ) + " " ) ; } else { if ( K == 2 ) { document . write ( " " + " " ) ; return ; } for ( var i = 1 ; i < K - 1 ; i ++ ) document . write ( 1 + " " ) ; document . write ( 2 + " " + ( N - K ) + " " ) ; } } var N = 18 , K = 5 ; printKParts ( N , K ) ;
Minimum operation required to make a balanced sequence | Function to return the minimum operations required ; Condition where we got only one closing bracket instead of 2 , here we have to add one more closing bracket to make the sequence balanced ; Add closing bracket that costs us one operation ; Remove the top opening bracket because we got the 1 opening and 2 continuous closing brackets ; Inserting the opening bracket to stack ; After making the sequence balanced closing is now set to 0 ; Case when there is no opening bracket the sequence starts with a closing bracket and one opening bracket is required Now we have one opening and one closing bracket ; Add opening bracket that costs us one operation ; Assigning 1 to cntClosing because we have one closing bracket ; Case where we got two continuous closing brackets Need to pop one opening bracket from stack top ; Condition where stack is not empty This is the case where we have only opening brackets ( st . size ( ) * 2 ) will give us the total closing bracket needed cntClosing is the count of closing bracket that we already have ; Driver code
function minOperations ( s , len ) { var operationCnt = 0 ; var st = [ ] ; var cntClosing = 0 ; for ( var i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' ' ) { if ( cntClosing > 0 ) { operationCnt ++ ; st . pop ( ) ; } st . push ( s [ i ] ) ; cntClosing = 0 ; } else if ( st . length == 0 ) { st . push ( ' ' ) ; operationCnt ++ ; cntClosing = 1 ; } else { cntClosing = ( cntClosing + 1 ) % 2 ; if ( cntClosing == 0 ) { st . pop ( ) ; } } } operationCnt += st . length * 2 - cntClosing ; return operationCnt ; } var str = " " ; var len = str . length ; document . write ( minOperations ( str , len ) ) ;
Longest subsequence with different adjacent characters | Function to find the longest Subsequence with different adjacent character ; Length of the string s ; Previously picked character ; If the current character is different from the previous then include this character and update previous character ; Driver Code ; Function call
function longestSubsequence ( s ) { var n = s . length ; var answer = 0 ; var prev = ' ' ; for ( var i = 0 ; i < n ; i ++ ) { if ( prev != s [ i ] ) { prev = s [ i ] ; answer ++ ; } } return answer ; } var str = " " ; document . write ( longestSubsequence ( str ) ) ;
Length of longest subarray of length at least 2 with maximum GCD | Function to calculate GCD of two numbers ; Function to find maximum size subarray having maximum GCD ; Base Case ; Let the maximum GCD be 1 initially ; Loop thourgh array to find maximum GCD of subarray with size 2 ; Traverse the array ; Is a multiple of k , increase cnt ; Else update maximum length with consecutive element divisible by k Set cnt to 0 ; Update the maxLength ; Return the maxLength ; Driver Code ; Function Call
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function maximumGcdSubarray ( arr , n ) { if ( n == 1 ) return 0 ; let k = 1 ; for ( let i = 1 ; i < n ; ++ i ) { k = Math . max ( k , gcd ( arr [ i ] , arr [ i - 1 ] ) ) ; } let cnt = 0 ; let maxLength = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { cnt ++ ; } else { maxLength = Math . max ( maxLength , cnt ) ; cnt = 0 ; } } maxLength = Math . max ( maxLength , cnt ) ; return maxLength ; } let arr = [ 18 , 3 , 6 , 9 ] ; let n = arr . length ; document . write ( maximumGcdSubarray ( arr , n ) ) ;