codeparrot/xlcost-text-to-code huggingface dataset:
Replace every element in a circular array by sum of next K elements | Function to print the required resultant array ; Reverse the array ; Traverse the range ; Store prefix sum ; Find the prefix sum ; Store the answer ; Calculate the answers ; Count of remaining elements ; Add the sum of all elements y times ; Add the remaining elements ; Update ans [ i ] ; If array is reversed print ans [ ] in reverse ; Given array arr [ ] ; Given K ; Function
function sumOfKElements ( arr , n , k ) { let rev = false ; if ( k < 0 ) { rev = true ; k *= - 1 ; let l = 0 , r = n - 1 ; while ( l < r ) { let tmp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = tmp ; l ++ ; r -- ; } } let dp = new Uint8Array ( n ) ; dp [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] += dp [ i - 1 ] + arr [ i ] ; } let ans = new Uint8Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i + k < n ) ans [ i ] = dp [ i + k ] - dp [ i ] ; else { let x = k - ( n - 1 - i ) ; let y = Math . floor ( x / n ) ; let rem = x % n ; ans [ i ] = dp [ n - 1 ] - dp [ i ] + y * dp [ n - 1 ] + ( rem - 1 >= 0 ? dp [ rem - 1 ] : 0 ) ; } } if ( rev ) { for ( let i = n - 1 ; i >= 0 ; i -- ) { document . write ( ans [ i ] + " " ) ; } } else { for ( let i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } } let arr = [ 4 , 2 , - 5 , 11 ] ; let N = arr . length ; let K = 3 ; sumOfKElements ( arr , N , K ) ;
Check if digit cube limit of an integer arrives at fixed point or a limit cycle | Define the limit ; Function to get the sum of cube of digits of a number ; Convert to String to get sum of the cubes of its digits ; return sum ; Function to check if the number arrives at a fixed point or a cycle ; Stores the values obtained ; Insert N to set s ; Get the next number using F ( N ) ; Check if the next number is repeated or not ; Function to check if digit cube limit of an integer arrives at fixed point or in a limit cycle ; N is a non negative integer ; Function Call ; If the value received is greater than limit ; If the value received is an Armstrong number ; Driver Code ; Function Call
let limit = 1000000000 ; function F ( N ) { let str = ( N ) . toString ( ) ; let sum = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let val = ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; sum += val * val * val ; } return sum ; } function findDestination ( N ) { let s = new Set ( ) ; let prev = N , next = 0 ; s . add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . has ( next ) ) { return next ; } prev = next ; s . add ( prev ) ; N = next ; } return next ; } function digitCubeLimit ( N ) { if ( N < 0 ) document . write ( " " ) ; else { let ans = findDestination ( N ) ; if ( ans > limit ) document . write ( " " ) ; else if ( ans == F ( ans ) ) { document . write ( N ) ; document . write ( " " + " " ) ; document . write ( ans ) ; } else { document . write ( N ) ; document . write ( " " + " " ) ; document . write ( ans ) ; } } } let N = 3 ; digitCubeLimit ( N ) ;
Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; Elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Given array arr [ ] ; Function Call
function countTuples ( arr , N ) { var ans = 0 , val = 0 ; var freq = new Map ( ) ; for ( var j = 0 ; j < N - 2 ; j ++ ) { val = 0 ; for ( var l = j + 1 ; l < N ; l ++ ) { if ( arr [ j ] == arr [ l ] ) { ans += val ; } if ( freq . has ( arr [ l ] ) ) { val += freq . get ( arr [ l ] ) ; } } if ( freq . has ( arr [ j ] ) ) { freq . set ( arr [ j ] , freq . get ( arr [ j ] ) + 1 ) ; } else { freq . set ( arr [ j ] , 1 ) ; } } return ans ; } var arr = [ 1 , 2 , 1 , 2 , 2 , 2 ] ; var N = arr . length ; document . write ( countTuples ( arr , N ) ) ;
Minimize subarray increments / decrements required to reduce all array elements to 0 | Function to count the minimum number of operations required ; Traverse the array ; If array element is negative ; Update minimum negative ; Update maximum positive ; Return minOp ; Driver code
function minOperation ( arr ) { let minOp = Number . MIN_VALUE ; let minNeg = 0 , maxPos = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return Math . abs ( minNeg ) + maxPos ; } let arr = [ 1 , 3 , 4 , 1 ] ; document . write ( minOperation ( arr ) ) ;
Pair of integers having least GCD among all given pairs having GCD exceeding K | Function to calculate the GCD of two numbers ; Function to print the pair having a gcd value just greater than the given integer ; Initialize variables ; Iterate until low less than equal to high ; Calculate mid ; Reducing the search space ; Print the answer ; Given array and K ; Function Call
function GCD ( a , b ) { if ( b == 0 ) { return a ; } return GCD ( b , a % b ) ; } function GcdPair ( arr , k ) { var lo = 0 , hi = arr . length - 1 , mid ; var ans = [ - 1 , 0 ] ; while ( lo <= hi ) { mid = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( GCD ( arr [ mid ] [ 0 ] , arr [ mid ] [ 1 ] ) > k ) { ans = arr [ mid ] ; hi = mid - 1 ; } else lo = mid + 1 ; } if ( ans [ 0 ] == - 1 ) document . write ( " " ) ; else document . write ( " " + ans [ 0 ] + " " + ans [ 1 ] + " " ) ; return ; } var arr = [ [ 3 , 6 ] , [ 15 , 30 ] , [ 25 , 75 ] , [ 30 , 120 ] ] ; var K = 16 ; GcdPair ( arr , K ) ;
Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array | Function to check if it is possible to sort array or not ; Store the smallest element ; Copy the original array ; Iterate over the given array ; Update smallest element ; Copy elements of arr [ ] to array B [ ] ; Sort original array ; Iterate over the given array ; If the i - th element is not in its sorted place ; Not possible to swap ; Given array ; Function Call
function isPossible ( arr , N ) { let mn = Number . MAX_VALUE ; let B = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { mn = Math . min ( mn , arr [ i ] ) ; B [ i ] = arr [ i ] ; } arr . sort ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != B [ i ] ) { if ( B [ i ] % mn != 0 ) { document . write ( " " ) ; return ; } } } document . write ( " " ) ; return ; } let N = 6 ; let arr = [ 4 , 3 , 6 , 6 , 2 , 9 ] ; isPossible ( arr , N ) ;
Check if every vertex triplet in graph contains two vertices connected to third vertex | JavaScript program of the above approach ; Function to add edge into the graph ; Finding the maximum and minimum values in each component ; Function for checking whether the given graph is valid or not ; Checking for intersecting intervals ; If intersection is found ; Graph is not valid ; Function for the DFS Traversal ; Traversing for every vertex ; Storing maximum and minimum values of each component ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function addEdge ( adj , u , v ) { ( adj [ u ] ) . push ( v ) ; ( adj [ v ] ) . push ( u ) ; } function DFSUtil ( u , adj , visited , componentMin , componentMax ) { visited [ u ] = true ; componentMax = Math . max ( componentMax , u ) ; componentMin = Math . min ( componentMin , u ) ; for ( var i = 0 ; i < ( adj [ u ] ) . length ; i ++ ) if ( visited [ ( adj [ u ] ) [ i ] ] == false ) DFSUtil ( ( adj [ u ] ) [ i ] , adj , visited , componentMin , componentMax ) ; } function isValid ( v ) { var MAX = - 1 ; var ans = false ; for ( var i of v ) { if ( i . first <= MAX ) { ans = true ; } MAX = Math . max ( MAX , i . second ) ; } return ( ans == false ? true : false ) ; } function DFS ( adj , V ) { var v = [ ] ; var visited = Array ( V + 1 ) . fill ( false ) ; for ( var u = 1 ; u <= V ; u ++ ) { if ( visited [ u ] == false ) { var componentMax = u ; var componentMin = u ; DFSUtil ( u , adj , visited , componentMin , componentMax ) ; v . push ( new pair ( componentMin , componentMax ) ) ; } } var check = isValid ( v ) ; if ( check ) document . write ( " " ) ; else document . write ( " " ) ; return ; } var N = 4 ; var adj = [ ] ; for ( var i = 0 ; i <= N + 1 ; i ++ ) adj . push ( new Array ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; DFS ( adj , N ) ;
Check if all array elements are present in a given stack or not | Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver code
function checkArrInStack ( s , arr , n ) { var freq = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) else freq . set ( arr [ i ] , 1 ) } while ( s . length != 0 ) { var poppedEle = s [ s . length - 1 ] ; s . pop ( ) ; if ( freq . has ( poppedEle ) ) freq . set ( poppedEle , freq . get ( poppedEle ) - 1 ) ; } if ( freq . size == 0 ) return 0 ; return 1 ; } var s = [ ] ; s . push ( 10 ) ; s . push ( 20 ) ; s . push ( 30 ) ; s . push ( 40 ) ; s . push ( 50 ) ; var arr = [ 20 , 30 ] ; var n = arr . length ; if ( checkArrInStack ( s , arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Length of longest substring to be deleted to make a string equal to another string | Function to print the length of longest substring to be deleted ; Stores the length of string ; Store the position of previous matched character of str1 ; Store the position of first occurrence of str2 in str1 ; Find the position of the first occurrence of str2 ; Store the index of str1 ; If both characters not matched ; Store the length of the longest deleted substring ; Store the position of last occurrence of str2 in str1 ; If both characters not matched ; Update res ; Update res . ; Given string ; Function Call
function longDelSub ( str1 , str2 ) { var N = str1 . length ; var M = str2 . length ; var prev_pos = 0 ; var pos = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { var index = prev_pos ; while ( index < N && str1 [ index ] != str2 [ i ] ) { index ++ ; } pos [ i ] = index ; prev_pos = index + 1 ; } var res = N - prev_pos ; prev_pos = N - 1 ; for ( let i = M - 1 ; i >= 0 ; i -- ) { var index = prev_pos ; while ( index >= 0 && str1 [ index ] != str2 [ i ] ) { index -- ; } if ( i != 0 ) { res = Math . max ( res , index - pos [ i - 1 ] - 1 ) ; } prev_pos = index - 1 ; } res = Math . max ( res , prev_pos + 1 ) ; return res ; } var str1 = " " ; var str2 = " " ; console . log ( longDelSub ( str1 , str2 ) ) ;
Maximized partitions of a string such that each character of the string appears in one substring | Function to print all the substrings ; Stores the substrings ; Stores last index of characters of string s ; Find the last position of each character in the string ; Update the last index ; Iterate the given string ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; If the current pos of character equals the min pos then the end of partition ; Add the respective character first ; Store the partition 's  len and reset variables ; Update the minp and str ; Input string ; Function call
function print_substring ( s ) { let n = s . length ; let str = " " ; let ans = [ ] ; if ( n == 0 ) { document . write ( " " ) ; return ; } let last_pos = Array ( 26 ) . fill ( - 1 ) ; for ( let i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] == - 1 ) { last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = i ; } } let minp = - 1 ; for ( let i = 0 ; i < n ; ++ i ) { let lp = last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ; minp = Math . max ( minp , lp ) ; if ( i == minp ) { str += s [ i ] ; document . write ( str + ' ' ) ; minp = - 1 ; str = " " ; } else { str += s [ i ] ; } } } let S = " " ; print_substring ( S ) ;
Lengths of maximized partitions of a string such that each character of the string appears in one substring | Function to find the length of all partitions oof a String such that each characters occurs in a single subString ; Stores last index of String s ; Find the last position of each letter in the String ; Update the last index ; Iterate the given String ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; Increase len of partition ; if the current pos of character equals the min pos then the end of partition ; Store the length ; Print all the partition lengths ; Given String str ; Function Call
function partitionString ( s ) { let n = s . length ; let ans = [ ] ; if ( n == 0 ) { document . write ( " " ) ; return ; } let last_pos = Array ( 26 ) . fill ( - 1 ) ; for ( let i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] == - 1 ) { last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = i ; } } let minp = - 1 , plen = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let lp = last_pos [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ; minp = Math . max ( minp , lp ) ; ++ plen ; if ( i == minp ) { ans . push ( plen ) ; minp = - 1 ; plen = 0 ; } } for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let str = " " ; partitionString ( str ) ;
Arithmetic Progression containing X and Y with least possible first term | Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Prlet the resultant AP ; Given length of AP and the two terms ; Function call
function minFirstTerm ( X , diff , N ) { let first_term = Number . MAX_VALUE ; let low = 0 , high = N ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } function prletAP ( N , X , Y ) { if ( X > Y ) { X = X + Y ; Y = X - Y ; X = X - Y ; } let maxDiff = Y - X ; let first_term = Number . MAX_VALUE , diff = 0 ; for ( let i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { let diff1 = i ; let diff2 = Math . floor ( maxDiff / diff1 ) ; let terms1 = diff2 + 1 ; let terms2 = diff1 + 1 ; let first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; let first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( first_term + " " ) ; first_term += diff ; } } let N = 5 , X = 10 , Y = 15 ; prletAP ( N , X , Y ) ;
Check if a number can be represented as sum of two consecutive perfect cubes | Function to check if a number can be expressed as the sum of cubes of two consecutive numbers ; Driver Code
function isCubeSum ( n ) { for ( var i = 1 ; i * i * i <= n ; i ++ ) { if ( i * i * i + ( i + 1 ) * ( i + 1 ) * ( i + 1 ) == n ) return true ; } return false ; } var n = 35 ; if ( isCubeSum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number can be represented as sum of two consecutive perfect cubes | Function to check that a number is the sum of cubes of 2 consecutive numbers or not ; Condition to check if a number is the sum of cubes of 2 consecutive numbers or not ; Driver Code ; Function call
function isSumCube ( N ) { var a = parseInt ( Math . cbrt ( N ) ) ; var b = a - 1 ; return ( ( a * a * a + b * b * b ) == N ) ; } var i = 35 ; if ( isSumCube ( i ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Maximize sum of remaining elements after every removal of the array half with greater sum | Function to find the maximum sum ; Base case len of array is 1 ; Stores the final result ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right ; If both are equal apply the optimal method ; Update with minimum ; Return the final ans ; Function to print maximum sum ; Dicitionary to store prefix sums ; Traversing the array ; Add prefix sum of the array ; Driver Code ; Function call
function maxweight ( s , e , pre ) { if ( s == e ) return 0 ; let ans = 0 ; for ( let i = s ; i < e ; i ++ ) { if ( ! pre [ i ] ) pre [ i ] = 0 ; if ( ! pre [ e ] ) pre [ e ] = 0 ; if ( ! pre [ s - 1 ] ) pre [ s - 1 ] = 0 ; let left = pre [ i ] - pre [ s - 1 ] ; let right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = Math . max ( ans , left + maxweight ( s , i , pre ) ) ; if ( left == right ) { ans = Math . max ( ans , Math . max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; } if ( left > right ) ans = Math . max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; } return ans ; } function maxSum ( arr ) { let pre = new Map ; pre [ - 1 ] = 0 ; pre [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr [ i ] ; } document . write ( maxweight ( 0 , arr . length - 1 , pre ) ) ; } arr = [ 6 , 2 , 3 , 4 , 5 , 5 ] ; maxSum ( arr ) ;
Minimum size substring to be removed to make a given string palindromic | Function to find palindromic prefix of maximum length ; Finding palindromic prefix of maximum length ; Checking if curr subString is palindrome or not . ; Condition to check if the prefix is a palindrome ; If no palindrome exist ; Function to find the maximum size palindrome such that after removing minimum size subString ; Finding prefix and suffix of same length ; Matching the ranges ; Case 1 : Length is even and whole String is palindrome ; Case 2 : Length is odd and whole String is palindrome ; Adding that mid character ; Add prefix or suffix of the remaining String or suffix , whichever is longer ; Reverse the remaining String to find the palindromic suffix ; Driver Code
function palindromePrefix ( S ) { let n = S . length ; for ( let i = n - 1 ; i >= 0 ; i -- ) { let curr = S . substr ( 0 , i + 1 ) ; let l = 0 , r = curr . length - 1 ; let is_palindrome = true ; while ( l < r ) { if ( curr [ l ] != curr [ r ] ) { is_palindrome = false ; break ; } l ++ ; r -- ; } if ( is_palindrome ) return curr ; } return " " ; } function maxPalindrome ( S ) { let n = S . length ; if ( n <= 1 ) { return S ; } let pre = " " , suff = " " ; let i = 0 , j = n - 1 ; while ( S [ i ] == S [ j ] && i < j ) { i ++ ; j -- ; } i -- ; j ++ ; pre = S . substr ( 0 , i + 1 ) ; suff = S . substr ( j ) ; if ( j - i == 1 ) { return pre + suff ; } if ( j - i == 2 ) { let mid_char = S . substr ( i + 1 , i + 2 ) ; return pre + mid_char + suff ; } let rem_str = S . substr ( i + 1 , j ) ; let pre_of_rem_str = palindromePrefix ( rem_str ) ; rem_str = reverse ( rem_str ) ; let suff_of_rem_str = palindromePrefix ( rem_str ) ; if ( pre_of_rem_str . length >= suff_of_rem_str . length ) { return pre + pre_of_rem_str + suff ; } else { return pre + suff_of_rem_str + suff ; } } function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return parseInt ( a ) ; } let S = " " ; document . write ( maxPalindrome ( S ) ) ;
Count numbers from a given range that contains a given number as the suffix | Function to count the number ends with given number in range ; Find number of digits in A ; Find the power of 10 ; Incrementing the A ; Driver code ; Function Call
function countNumEnds ( A , L , R ) { let temp = 0 , count = 0 , digits = 0 ; let cycle = 0 ; digits = Math . round ( Math . log10 ( A ) ) + 1 ; temp = Math . round ( Math . pow ( 10 , digits ) ) ; cycle = temp ; while ( temp <= R ) { if ( temp >= L ) count ++ ; temp += cycle ; } document . write ( count ) ; } let A = 2 , L = 2 , R = 20 ; countNumEnds ( A , L , R ) ;
Minimize replacement of characters to its nearest alphabet to make a string palindromic | Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Given string ; Function Call
function minOperations ( s ) { var len = s . length ; var result = 0 ; for ( var i = 0 ; i < len / 2 ; i ++ ) { var D1 = Math . max ( s [ i ] . charCodeAt ( 0 ) , s [ len - 1 - i ] . charCodeAt ( 0 ) ) - Math . min ( s [ i ] . charCodeAt ( 0 ) , s [ len - 1 - i ] . charCodeAt ( 0 ) ) ; var D2 = 26 - D1 ; result += Math . min ( D1 , D2 ) ; } return result ; } var s = " " ; document . write ( minOperations ( s ) ) ;
Count subsequences which contains both the maximum and minimum array element | Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call
function countSubSequence ( arr , n ) { let maximum = Math . max ( ... arr ) ; let minimum = Math . min ( ... arr ) ; if ( maximum == minimum ) return Math . pow ( 2 , n ) - 1 ; let i = count ( arr , maximum ) ; let j = count ( arr , minimum ) ; let res = ( Math . pow ( 2 , i ) - 1 ) * ( Math . pow ( 2 , j ) - 1 ) * Math . pow ( 2 , n - i - j ) ; return res ; } function count ( arr , value ) { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( countSubSequence ( arr , n ) ) ;
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Function that finds the minimum number of the element must be added to make A as a subsequence in B ; Base Case ; dp [ i ] [ j ] indicates the length of LCS of A of length i & B of length j ; If there are no elements either in A or B then the length of lcs is 0 ; If the element present at ith and jth index of A and B are equal then include in LCS ; If they are not equal then take the max ; Return difference of length of A and lcs of A and B ; Driver Code ; Given sequence A and B ; Function call
function transformSubsequence ( n , m , A , B ) { if ( B . length == 0 ) return n ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( m + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n + 1 ; i ++ ) { for ( var j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( A [ i - 1 ] == B [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return n - dp [ n ] [ m ] ; } var N = 5 ; var M = 6 ; var A = [ 1 , 2 , 3 , 4 , 5 ] ; var B = [ 2 , 5 , 6 , 4 , 9 , 12 ] ; document . write ( transformSubsequence ( N , M , A , B ) ) ;