codeparrot/xlcost-text-to-code huggingface dataset:
Maximum profit by buying and selling a stock at most twice | Set 2 | Function to find the maximum profit with two transactions on a given list of stock prices ; buy1 - Money lent to buy 1 stock profit1 - Profit after selling the 1 st stock buyed . buy2 - Money lent to buy 2 stocks including profit of selling 1 st stock profit2 - Profit after selling 2 stocks ; Set initial buying values to Number . MAX_VALUE as we want to minimize it ; Set initial selling values to zero as we want to maximize it ; Money lent to buy the stock should be minimum as possible . buy1 tracks the minimum possible stock to buy from 0 to i - 1. ; Profit after selling a stock should be maximum as possible . profit1 tracks maximum possible profit we can make from 0 to i - 1. ; Now for buying the 2 nd stock , we will integrate profit made from selling the 1 st stock ; Profit after selling a 2 stocks should be maximum as possible . profit2 tracks maximum possible profit we can make from 0 to i - 1. ; Driver Code
function maxProfit ( price , n ) { var buy1 , profit1 , buy2 , profit2 ; buy1 = buy2 = Number . MAX_VALUE ; profit1 = profit2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { buy1 = Math . min ( buy1 , price [ i ] ) ; profit1 = Math . max ( profit1 , price [ i ] - buy1 ) ; buy2 = Math . min ( buy2 , price [ i ] - profit1 ) ; profit2 = Math . max ( profit2 , price [ i ] - buy2 ) ; } return profit2 ; } var price = [ 2 , 30 , 15 , 10 , 8 , 25 , 80 ] ; var n = price . length ; document . write ( " " + maxProfit ( price , n ) ) ;
Maximum score of deleting an element from an Array based on given condition | Function to find the maximum score of the deleting an element from an array ; Creating a map to keep the frequency of numbers ; Loop to iterate over the elements of the array ; Creating a DP array to keep count of max score at ith element and it will be filled in the bottom Up manner ; Loop to choose the elements of the array to delete from the array ; Driver Code ; Function call
function findMaximumScore ( a , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( a [ i ] ) ) { freq . set ( a [ i ] , freq . get ( a [ i ] ) + 1 ) ; } else { freq . set ( a [ i ] , 1 ) ; } } let dp = new Array ( Math . max ( ... a ) + 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = freq . get ( 1 ) ; for ( let i = 2 ; i < dp . length ; i ++ ) dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + freq . get ( i ) * i ) ; return dp [ dp . length - 1 ] ; } let n = 3 ; let a = [ 1 , 2 , 3 ] ; document . write ( findMaximumScore ( a , n ) ) ;
Reverse a subarray to maximize sum of even | Function to return maximized sum at even indices ; Stores difference with element on the left ; Stores difference with element on the right ; Compute and store left difference ; For first index ; If previous difference is positive ; Otherwise ; For the last index ; Otherwise ; For first index ; If the previous difference is positive ; Driver Code
function maximizeSum ( arr ) { let n = arr . length ; let sum = 0 ; for ( let i = 0 ; i < n ; i = i + 2 ) sum += arr [ i ] ; let leftDP = new Array ( Math . floor ( n / 2 ) ) ; let rightDP = new Array ( Math . floor ( n / 2 ) ) ; for ( let i = 0 ; i < n / 2 ; i ++ ) { leftDP [ i ] = 0 ; rightDP [ i ] = 0 ; } let c = 0 ; for ( let i = 1 ; i < n ; i = i + 2 ) { let leftDiff = arr [ i ] - arr [ i - 1 ] ; if ( c - 1 < 0 ) leftDP [ i ] = leftDiff ; else { if ( leftDP [ i ] > 0 ) leftDP [ i ] = leftDiff + leftDP [ i - 1 ] ; else leftDP [ i ] = leftDiff ; } let rightDiff ; if ( i + 1 >= arr . length ) rightDiff = 0 ; else rightDiff = arr [ i ] - arr [ i + 1 ] ; if ( c - 1 < 0 ) rightDP [ i ] = rightDiff ; else { if ( rightDP [ i ] > 0 ) rightDP [ i ] = rightDiff + rightDP [ i - 1 ] ; else rightDP [ i ] = rightDiff ; } c ++ ; } let max = 0 ; for ( let i = 0 ; i < n / 2 ; i ++ ) { max = Math . max ( max , Math . max ( leftDP [ i ] , rightDP [ i ] ) ) ; } return max + sum ; } let arr = [ 7 , 8 , 4 , 5 , 7 , 6 , 8 , 9 , 7 , 3 ] ; let ans = maximizeSum ( arr ) ; document . write ( ans ) ;
Count of all subsequences having adjacent elements with different parity | Function to find required subsequences ; dp [ i ] [ 0 ] : Stores the number of subsequences till i - th index ending with even element dp [ i ] [ 1 ] : Stores the number of subsequences till i - th index ending with odd element ; Initialise the dp [ ] [ ] with 0. ; If odd element is encountered ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with even element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with odd element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Count of all valid subsequences ; Driver Code
function validsubsequences ( arr , n ) { let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] [ 0 ] = 0 ; dp [ i ] [ 1 ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 != 0 ) { dp [ i ] [ 1 ] += 1 ; dp [ i ] [ 1 ] += dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ] ; } else { dp [ i ] [ 0 ] += 1 ; dp [ i ] [ 0 ] += dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ] ; } } return ( dp [ n ] [ 0 ] + dp [ n ] [ 1 ] ) ; } let arr = [ 5 , 6 , 9 , 7 ] ; let n = arr . length ; document . write ( validsubsequences ( arr , n ) ) ;
Count of N | Function to return count of N - digit numbers with absolute difference of adjacent digits not exceeding K ; For 1 - digit numbers , the count is 10 irrespective of K ; dp [ i ] [ j ] stores the number of such i - digit numbers ending in j ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find actual values of i - digit numbers ending in j ; Stores the final answer ; Driver Code
function getCount ( n , k ) { if ( n == 1 ) return 10 ; var dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) dp [ i ] = Array ( 11 ) . fill ( 0 ) ; for ( i = 1 ; i <= 9 ; i ++ ) dp [ 1 ] [ i ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 0 ; j <= 9 ; j ++ ) { var l = Math . max ( 0 , j - k ) ; var r = Math . min ( 9 , j + k ) ; dp [ i ] [ l ] += dp [ i - 1 ] [ j ] ; dp [ i ] [ r + 1 ] -= dp [ i - 1 ] [ j ] ; } for ( j = 1 ; j <= 9 ; j ++ ) dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } var count = 0 ; for ( i = 0 ; i <= 9 ; i ++ ) count += dp [ n ] [ i ] ; return count ; } var n = 2 , k = 1 ; document . write ( getCount ( n , k ) ) ;
Find if there is a path between two vertices in a directed graph | Set 2 | Javascript program to find if there is a path between two vertices in a directed graph using Dynamic Programming ; Function to find if there is a path between two vertices in a directed graph ; mat matrix ; set mat [ i , j ] = true if there is edge between i to j ; Check for all intermediate vertex ; If vertex is invalid ; If there is a path ; Driver code
var X = 6 ; var Z = 2 ; function existPath ( V , edges , u , v ) { var mat = Array . from ( Array ( V ) , ( ) => Array ( V ) ) ; for ( var i = 0 ; i < X ; i ++ ) mat [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = true ; for ( var k = 0 ; k < V ; k ++ ) { for ( var i = 0 ; i < V ; i ++ ) { for ( var j = 0 ; j < V ; j ++ ) { mat [ i ] [ j ] = mat [ i ] [ j ] || mat [ i ] [ k ] && mat [ k ] [ j ] ; } } } if ( u >= V v >= V ) { return false ; } if ( mat [ u ] [ v ] ) return true ; return false ; } var V = 4 ; var edges = [ [ 0 , 2 ] , [ 0 , 1 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 2 , 0 ] , [ 3 , 3 ] ] ; var u = 1 , v = 3 ; if ( existPath ( V , edges , u , v ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize the numbers of splits in an Array having sum divisible by 3 | Javascript program for the above approach ; Prefix array storing right most index with prefix sums 0 , 1 , 2 ; dp array ; Current prefix sum ; Calculating the prefix sum modulo 3 ; We dont have a left pointer with prefix sum C ; We cannot consider i as a right pointer of any segment ; We have a left pointer pre [ C ] with prefix sum C ; i is the rightmost index of prefix sum C ; Driver code
function calculate_maximum_splits ( arr , N ) { var pre = [ 0 , - 1 , - 1 ] ; var i ; var dp = new Array ( N ) ; for ( i = 0 ; i < N ; i ++ ) dp [ i ] = 0 ; var C = 0 ; for ( i = 1 ; i < N ; i ++ ) { C = C + arr [ i ] ; C = C % 3 ; if ( pre [ C ] == - 1 ) { dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ pre [ C ] ] + 1 ) ; } pre [ C ] = i ; } document . write ( dp [ N - 1 ] ) ; } var arr = [ 2 , 36 , 1 , 9 , 2 , 0 , 1 , 8 , 1 ] ; var N = arr . length ; calculate_maximum_splits ( arr , N ) ;
Count of elements having Euler 's Totient value one less than itself | JavaScript program for the above approach ; Seiev of Erotosthenes method to compute all primes ; If current number is marked prime then mark its multiple as non - prime ; Function to count the number of element satisfying the condition ; Compute the number of primes in count prime array ; Print the number of elements satisfying the condition ; Given array ; Size of the array ; Function Call
let prime = new Uint8Array ( 1000001 ) ; function seiveOfEratosthenes ( ) { for ( let i = 2 ; i < 1000001 ; i ++ ) { prime [ i ] = 1 ; } for ( let i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 1 ) { for ( let j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 0 ; } } } } function CountElements ( arr , n , L , R ) { seiveOfEratosthenes ( ) ; countPrime = new Uint8Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ; } document . write ( ( countPrime [ R ] - countPrime [ L - 1 ] ) + " " ) ; return ; } let arr = [ 2 , 4 , 5 , 8 ] ; let N = arr . length ; let L = 1 , R = 3 ; CountElements ( arr , N , L , R ) ;
Count of elements having odd number of divisors in index range [ L , R ] for Q queries | Function count the number of elements having odd number of divisors ; Initialise dp [ ] array ; Precomputation ; Find the Prefix Sum ; Iterate for each query ; Find the answer for each query ; Driver Code ; Given array arr [ ] ; Given Query ; Function Call
function OddDivisorsCount ( n , q , a , Query ) { var DP = new Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { var x = Math . sqrt ( a [ i ] ) ; if ( x * x == a [ i ] ) DP [ i ] = 1 ; } for ( var i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } var l , r ; for ( var i = 0 ; i < q ; i ++ ) { l = Query [ i ] [ 0 ] ; r = Query [ i ] [ 1 ] ; if ( l == 0 ) { document . write ( DP [ r ] + " " ) ; } else { document . write ( DP [ r ] - DP [ l - 1 ] + " " ) ; } } } var N = 5 ; var Q = 3 ; var arr = [ 2 , 4 , 5 , 6 , 9 ] ; var Query = [ [ 0 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] ] ; OddDivisorsCount ( N , Q , arr , Query ) ;
Find all possible ways to Split the given string into Primes | Javascript program to Find all the ways to split the given string into Primes . ; Sieve of Eratosthenes ; Function Convert integer to binary string ; Function print all the all the ways to split the given string into Primes . ; To store all possible strings ; Exponetnital complexity n * ( 2 ^ ( n - 1 ) ) for bit ; Pruning step ; Driver code
let primes = new Array ( 1000000 ) ; const maxn = 1000000 ; function sieve ( ) { primes . fill ( true ) primes [ 0 ] = primes [ 1 ] = 0 ; for ( let i = 2 ; i * i <= maxn ; i ++ ) { if ( primes [ i ] ) { for ( let j = i * i ; j <= maxn ; j += i ) primes [ j ] = false ; } } } function toBinary ( n ) { let r = " " ; while ( n != 0 ) { r = ( n % 2 == 0 ? " " : " " ) + r ; n = Math . floor ( n / 2 ) ; } return ( r == " " ) ? " " : r ; } function PrimeSplit ( str ) { let temp ; let cnt = 0 ; let ans = new Array ( ) ; let bt = 1 << ( str . length - 1 ) ; let n = str . length ; for ( let i = 0 ; i < bt ; i ++ ) { temp = toBinary ( i ) + " " ; let j = 0 , x = n - temp . length , y ; while ( j < x ) { temp = " " + temp ; j ++ ; } j = 0 ; x = 0 ; y = - 1 ; let sp = " " , tp = " " ; let flag = 0 ; while ( j < n ) { sp += str [ j ] ; if ( temp [ j ] == ' ' ) { tp += sp + ' ' ; y = parseInt ( sp ) ; if ( ! primes [ y ] ) { flag = 1 ; break ; } sp = " " ; } j ++ ; } tp += sp ; if ( sp != " " ) { y = parseInt ( sp ) ; if ( ! primes [ y ] ) flag = 1 ; } if ( ! flag ) ans . push ( tp ) ; } if ( ans . length == 0 ) { document . write ( - 1 + " " ) ; } for ( let i of ans ) { document . write ( i + " " ) ; } } let str = " " ; sieve ( ) ; PrimeSplit ( str ) ;
Count of subsequences of length 4 in form ( x , x , x + 1 , x + 1 ) | Set 2 | Function to count the numbers ; Array that stores the digits from left to right ; Array that stores the digits from right to left ; Initially both array store zero ; Fill the table for count1 array ; Update the count of current character ; Fill the table for count2 array ; Update the count of cuuent character ; Variable that stores the count of the numbers ; Traverse Input string and get the count of digits from count1 and count2 array such that difference b / w digit is 1 & store it var c1 & c2 . And store it in variable c1 and c2 ; Update the ans ; Return the final count ; Given String ; Function Call
function countStableNum ( str , N ) { var count1 = Array . from ( Array ( N ) , ( ) => Array ( 10 ) ) ; var count2 = Array . from ( Array ( N ) , ( ) => Array ( 10 ) ) ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < 10 ; j ++ ) count1 [ i ] [ j ] = count2 [ i ] [ j ] = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( i != 0 ) { for ( var j = 0 ; j < 10 ; j ++ ) { count1 [ i ] [ j ] += count1 [ i - 1 ] [ j ] ; } } count1 [ i ] [ str [ i ] - ' ' ] ++ ; } for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( i != N - 1 ) { for ( var j = 0 ; j < 10 ; j ++ ) { count2 [ i ] [ j ] += count2 [ i + 1 ] [ j ] ; } } count2 [ i ] [ str [ i ] - ' ' ] ++ ; } var ans = 0 ; for ( var i = 1 ; i < N - 1 ; i ++ ) { if ( str [ i ] == ' ' ) continue ; var c1 = count1 [ i - 1 ] [ str [ i ] - ' ' ] ; var c2 = count2 [ i + 1 ] [ str [ i ] - ' ' + 1 ] ; if ( c2 == 0 ) continue ; ans = ( ans + ( c1 * ( ( c2 * ( c2 - 1 ) / 2 ) ) ) ) ; } return ans ; } var str = " " ; var N = str . length ; document . write ( countStableNum ( str , N ) ) ;
Count of subsequences in an array with sum less than or equal to X | Utility function to return the count of subsequence in an array with sum less than or equal to X ; Base condition ; Return if the sub - problem is already calculated ; Check if the current element is less than or equal to sum ; Count subsequences excluding the current element ; Count subsequences including the current element ; Exclude current element ; Return the result ; Function to return the count of subsequence in an array with sum less than or equal to X ; Initialize a DP array ; Return the result ; Driver Code
function countSubsequenceUtil ( ind , sum , A , N , dp ) { if ( ind == N ) return 1 ; if ( dp [ ind ] [ sum ] != - 1 ) return dp [ ind ] [ sum ] ; if ( A [ ind ] <= sum ) { dp [ ind ] [ sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) + countSubsequenceUtil ( ind + 1 , sum - A [ ind ] , A , N , dp ) ; } else { dp [ ind ] [ sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) ; } return dp [ ind ] [ sum ] ; } function countSubsequence ( A , N , X ) { let dp = new Array ( N ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < X + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return countSubsequenceUtil ( 0 , X , A , N , dp ) - 1 ; } let arr = [ 25 , 13 , 40 ] , X = 50 ; let N = arr . length ; document . write ( countSubsequence ( arr , N , X ) ) ;
Count possible decodings of a given Digit Sequence | Set 2 | Function to count the number of ways to decode the given digit sequence ; Array to store the dp states ; Case of empty String ; Condition to check if the first character of String is 0 ; Base case for single length String ; Bottom - up dp for the String ; Previous character ; Current character ; Case to include the Current digit as a single digit for decoding the String ; Case to include the current character as two - digit for decoding the String ; Condition to check if the current character is " * " ; Condition to check if the current character is less than or equal to 26 ; Condition to check if the previous digit is equal to " * " ; Driver Code ; Function Call
function waysToDecode2 ( s ) { let n = s . length ; let dp = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; dp [ 0 ] = 1 ; if ( s [ 0 ] == ' ' ) return 0 ; dp [ 1 ] = ( ( s [ 0 ] == ' ' ) ? 9 : 1 ) ; for ( let i = 2 ; i <= n ; i ++ ) { let first = s [ i - 2 ] ; let second = s [ i - 1 ] ; if ( second == ' ' ) { dp [ i ] += 9 * dp [ i - 1 ] ; } else if ( second > ' ' ) dp [ i ] += dp [ i - 1 ] ; if ( first == ' ' first == ' ' ) { if ( second == ' ' ) { if ( first == ' ' ) dp [ i ] += 9 * dp [ i - 2 ] ; else if ( first == ' ' ) dp [ i ] += 6 * dp [ i - 2 ] ; } else if ( ( ( first - ' ' ) * 10 + ( second - ' ' ) ) <= 26 ) { dp [ i ] += dp [ i - 2 ] ; } } else if ( first == ' ' ) { if ( second == ' ' ) { dp [ i ] += 15 * dp [ i - 2 ] ; } else if ( second <= ' ' ) { dp [ i ] += 2 * dp [ i - 2 ] ; } else { dp [ i ] += dp [ i - 2 ] ; } } } return dp [ n ] ; } let str = " " ; document . write ( waysToDecode2 ( str . split ( ' ' ) ) + " " ) ;
Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H | Function that prlets the number of times X gets a value between L and R ; Base condition ; Condition if X can be made equal to j after i additions ; Compute value of X after adding arr [ i ] ; Compute value of X after adding arr [ i ] - 1 ; Update dp as the maximum value ; Compute maximum answer from all possible cases ; Printing maximum good occurrence of X ; Driver Code
function goodLeteger ( arr , n , h , l , r ) { let dp = new Array ( n + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n + 1 ; i ++ ) for ( let j = 0 ; j < h ; j ++ ) dp [ i ] [ j ] = - 1 ; dp [ 0 ] [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < h ; j ++ ) { if ( dp [ i ] [ j ] != - 1 ) { let h1 = ( j + arr [ i ] ) % h ; let h2 = ( j + arr [ i ] - 1 ) % h ; dp [ i + 1 ] [ h1 ] = Math . max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( ( h1 >= l && h1 <= r ) ? 1 : 0 ) ) ; dp [ i + 1 ] [ h2 ] = Math . max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( ( h2 >= l && h2 <= r ) ? 1 : 0 ) ) ; } } } let ans = 0 ; for ( let i = 0 ; i < h ; i ++ ) { if ( dp [ n ] [ i ] != - 1 ) ans = Math . max ( ans , dp [ n ] [ i ] ) ; } document . write ( ans + " " ) ; } let A = [ 16 , 17 , 14 , 20 , 20 , 11 , 22 ] ; let H = 24 ; let L = 21 ; let R = 23 ; let size = A . length ; goodLeteger ( A , size , H , L , R ) ;
Range queries for alternatively addition and subtraction on given Array | Structure to represent a query ; This function fills the Prefix Array ; Initialise the prefix array ; Iterate all the element of arr [ ] and update the prefix array ; If n is even then , add the previous value of prefix array with the current value of arr ; if n is odd , then subtract the previous value of prefix Array from current value ; Function to find the result of alternatively adding and subtracting elements in the range [ L < R ] ; Case 1 : when L is zero ; Case 2 : When L is non zero ; If L is odd means range starts from odd position multiply result by - 1 ; Return the final result ; Function to find the sum of all alternative add and subtract between ranges [ L , R ] ; Declare prefix array ; Function Call to fill prefix arr [ ] ; Iterate for each query ; Given array ; Given Queries ; Function Call
class Query { constructor ( l , r ) { this . L = l ; this . R = r ; } } function fillPrefixArray ( arr , n , prefixArray ) { prefixArray [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { prefixArray [ i ] = prefixArray [ i - 1 ] + arr [ i ] ; } else { prefixArray [ i ] = prefixArray [ i - 1 ] - arr [ i ] ; } } } function findResultUtil ( prefixArray , L , R ) { let result ; if ( L == 0 ) { result = prefixArray [ R ] ; } else { result = prefixArray [ R ] - prefixArray [ L - 1 ] ; } if ( L % 2 == 1 ) { result = result * ( - 1 ) ; } return result ; } function findResult ( arr , n , q , m ) { let prefixArray = new Array ( n ) ; fillPrefixArray ( arr , n , prefixArray ) ; for ( let i = 0 ; i < m ; i ++ ) { document . write ( findResultUtil ( prefixArray , q [ i ] . L , q [ i ] . R ) + " " ) ; } } let arr = [ 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 ] ; let n = arr . length ; let q = [ new Query ( 2 , 5 ) , new Query ( 6 , 8 ) , new Query ( 1 , 7 ) , new Query ( 4 , 8 ) , new Query ( 0 , 5 ) ] ; let m = q . length ; findResult ( arr , n , q , m ) ;
Longest subarray whose elements can be made equal by maximum K increments | Function to find maximum possible length of subarray ; Stores the sum of elements that needs to be added to the sub array ; Stores the index of the current position of subarray ; Stores the maximum length of subarray . ; Maximum element from each subarray length ; Previous index of the current subarray of maximum length ; Deque to store the indices of maximum element of each sub array ; For each array element , find the maximum length of required subarray ; Traverse the deque and update the index of maximum element . ; If it is first element then update maximum and dp [ ] ; Else check if current element exceeds max ; Update max and dp [ ] ; Update the index of the current maximum length subarray ; While current element being added to dp [ ] array exceeds K ; Update index of current position and the previous position ; Remove elements from deque and update the maximum element ; Update the maximum length of the required subarray . ; Driver Program
function validSubArrLength ( arr , N , K ) { var dp = Array ( N + 1 ) ; var pos = 0 ; var ans = 0 ; var mx = 0 ; var pre = 0 ; var q = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { while ( q . length != 0 && arr [ q [ q . length - 1 ] ] < arr [ i ] ) q . pop ( ) ; q . push ( i ) ; if ( i == 0 ) { mx = arr [ i ] ; dp [ i ] = arr [ i ] ; } else if ( mx <= arr [ i ] ) { dp [ i ] = dp [ i - 1 ] + arr [ i ] ; mx = arr [ i ] ; } else { dp [ i ] = dp [ i - 1 ] + arr [ i ] ; } if ( pre == 0 ) pos = 0 ; else pos = pre - 1 ; while ( ( i - pre + 1 ) * mx - ( dp [ i ] - dp [ pos ] ) > K && pre < i ) { pos = pre ; pre ++ ; while ( q . length != 0 && q [ 0 ] < pre && pre < i ) { q . shift ( ) ; mx = arr [ q [ 0 ] ] ; } } ans = Math . max ( ans , i - pre + 1 ) ; } return ans ; } var N = 6 ; var K = 8 ; var arr = [ 2 , 7 , 1 , 3 , 4 , 5 ] ; document . write ( validSubArrLength ( arr , N , K ) ) ;