codeparrot/xlcost-text-to-code huggingface dataset:
Count subarrays with Prime sum | Function to count subarrays with Prime sum ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize result ; Traverse through the array ; return answer ; Driver program
function primeSubarrays ( A , n ) { var max_val = parseInt ( Math . pow ( 10 , 7 ) ) ; var prime = new Array ( max_val + 1 ) ; prime . fill ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } var cnt = 0 ; for ( var i = 0 ; i < n - 1 ; ++ i ) { var val = A [ i ] ; for ( var j = i + 1 ; j < n ; ++ j ) { val += A [ j ] ; if ( prime [ val ] ) ++ cnt ; } } return cnt ; } var A = [ 1 , 2 , 3 , 4 , 5 ] ; var n = A . length ; document . write ( primeSubarrays ( A , n ) ) ;
Minimum swaps required to make a binary string alternating | returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code
function countMinSwaps ( st ) { let min_swaps = 0 ; let odd_0 = 0 , even_0 = 0 ; let odd_1 = 0 , even_1 = 0 ; let n = st . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( st [ i ] == ' ' ) { even_1 ++ ; } else { even_0 ++ ; } } else { if ( st [ i ] == ' ' ) { odd_1 ++ ; } else { odd_0 ++ ; } } } let cnt_swaps_1 = Math . min ( even_0 , odd_1 ) ; let cnt_swaps_2 = Math . min ( even_1 , odd_0 ) ; return Math . min ( cnt_swaps_1 , cnt_swaps_2 ) ; } let st = " " ; document . write ( countMinSwaps ( st ) ) ;
Difference Array | Range update query in O ( 1 ) | Creates a diff array D [ ] for A [ ] and returns it after filling initial values . ; We use one extra space because update ( l , r , x ) updates D [ r + 1 ] ; Does range update ; Prints updated Array ; Note that A [ 0 ] or D [ 0 ] decides values of rest of the elements . ; Array to be updated ; Create and fill difference Array ; After below update ( l , r , x ) , the elements should become 20 , 15 , 20 , 40 ; After below updates , the array should become 30 , 35 , 70 , 60
function initializeDiffArray ( A ) { let n = A . length ; let D = [ ] ; D [ 0 ] = A [ 0 ] , D [ n ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) D [ i ] = A [ i ] - A [ i - 1 ] ; return D ; } function update ( D , l , r , x ) { D [ l ] += x ; D [ r + 1 ] -= x ; return D ; } function printArray ( A , D ) { for ( let i = 0 ; i < A . length ; i ++ ) { if ( i == 0 ) A [ i ] = D [ i ] ; else A [ i ] = D [ i ] + A [ i - 1 ] ; document . write ( A [ i ] + " " ) ; } document . write ( " " ) ; } let A = [ 10 , 5 , 20 , 40 ] ; let D = initializeDiffArray ( A ) ; D = update ( D , 0 , 1 , 10 ) ; printArray ( A , D ) ; D = update ( D , 1 , 3 , 20 ) ; D = update ( D , 2 , 2 , 30 ) ; printArray ( A , D ) ;
Find the value of the function Y = ( X ^ 6 + X ^ 2 + 9894845 ) % 971 | computing ( a ^ b ) % c ; driver code
function modpow ( base , exp , modulus ) { base %= modulus ; let result = 1 ; while ( exp > 0 ) { if ( ( exp & 1 ) > 0 ) result = ( result * base ) % modulus ; base = ( base * base ) % modulus ; exp >>= 1 ; } return result ; } let n = 654654 ; let mod = 971 ; document . write ( ( ( modpow ( n , 6 , mod ) + modpow ( n , 2 , mod ) ) % mod + 355 ) % mod ) ;
Largest Sum Contiguous Subarray | JavaScript program to find maximum contiguous subarray Function to find the maximum contiguous subarray ; Driver code
function maxSubArraySum ( a , size ) { var maxint = Math . pow ( 2 , 53 ) var max_so_far = - maxint - 1 var max_ending_here = 0 for ( var i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] if ( max_so_far < max_ending_here ) max_so_far = max_ending_here if ( max_ending_here < 0 ) max_ending_here = 0 } return max_so_far } var a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] document . write ( " " , maxSubArraySum ( a , a . length ) )
Place N ^ 2 numbers in matrix such that every row has an equal sum | Javascript program to distribute n ^ 2 numbers to n people ; 2D array for storing the final result ; using modulo to go to the firs column after the last column ; making a 2D array containing numbers ; driver program
function solve ( arr , n ) { let ans = new Array ( n ) ; for ( var i = 0 ; i < ans . length ; i ++ ) { ans [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] ; } } return ans ; } function show ( arr , n ) { let res = solve ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( res [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function makeArray ( n ) { let arr = new Array ( n ) ; for ( var i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = new Array ( 2 ) ; } let c = 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = c ++ ; } return arr ; } let n = 5 ; let arr = makeArray ( n ) ; show ( arr , n ) ;
Minimum rooms for m events of n batches with given schedule | Returns minimum number of rooms required to perform classes of n groups in m slots with given schedule . ; Store number of class happening in empty slot ; initialize all values to zero ; Number of rooms required is equal to maximum classes happening in a particular slot . ; driver code
function findMinRooms ( slots , n , m ) { let counts = Array ( m ) . fill ( 0 ) ; for ( let i = 0 ; i < m ; i ++ ) counts [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < m ; j ++ ) if ( slots [ i ] [ j ] == ' ' ) counts [ j ] ++ ; let max = - 1 ; for ( let i = 0 ; i < m ; i ++ ) if ( max < counts [ i ] ) max = counts [ i ] ; return max ; } let n = 3 , m = 7 ; let slots = [ " " , " " , " " ] ; document . write ( findMinRooms ( slots , n , m ) ) ;
Largest Sum Contiguous Subarray | C # program to prlet largest contiguous array sum ; Driver code
function maxSubArraySum ( a , size ) { let max_so_far = a [ 0 ] ; let curr_max = a [ 0 ] ; for ( let i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } let a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] ; let n = a . length ; document . write ( " " , maxSubArraySum ( a , n ) ) ;
Find minimum number of currency notes and values that sum to given amount | function to count and print currency notes ; count notes using Greedy approach ; Print notes ; driver code
function countCurrency ( amount ) { let notes = [ 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 ] ; let noteCounter = Array ( 9 ) . fill ( 0 ) ; for ( let i = 0 ; i < 9 ; i ++ ) { if ( amount >= notes [ i ] ) { noteCounter [ i ] = Math . floor ( amount / notes [ i ] ) ; amount = amount - noteCounter [ i ] * notes [ i ] ; } } document . write ( " " + " " ) ; for ( let i = 0 ; i < 9 ; i ++ ) { if ( noteCounter [ i ] != 0 ) { document . write ( notes [ i ] + " " + noteCounter [ i ] + " " ) ; } } } let amount = 868 ; countCurrency ( amount ) ;
Minimum sum by choosing minimum of pairs from array | Returns minimum possible sum in array B [ ] ; driver function
function minSum ( A , n ) { let min_val = Math . min ( ... A ) ; return ( min_val * ( n - 1 ) ) ; } let A = [ 3 , 6 , 2 , 8 , 7 , 5 ] ; let n = A . length ; document . write ( minSum ( A , n ) ) ;
Program for Next Fit algorithm in Memory Management | Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a process ; pick each process and find suitable blocks according to its size ad assign to it ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver Code
function NextFit ( blockSize , m , processSize , n ) { let allocation = Array . from ( { length : n } , ( _ , i ) => - 1 ) , j = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } j = ( j + 1 ) % m ; } } document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( i + 1 + Array ( 20 ) . fill ( ' \xa0 ' ) . join ( ' ' ) + processSize [ i ] + Array ( 20 ) . fill ( ' \xa0 ' ) . join ( ' ' ) ) ; if ( allocation [ i ] != - 1 ) { document . write ( allocation [ i ] + 1 ) ; } else { document . write ( " " ) ; } document . write ( " " ) ; } } let blockSize = [ 5 , 10 , 20 ] ; let processSize = [ 10 , 20 , 5 ] ; let m = blockSize . length ; let n = processSize . length ; NextFit ( blockSize , m , processSize , n ) ;
Maximum profit by buying and selling a share at most twice |  ; adding array ; 80 / 30 / / \ 25 / 15 / / \ / 2 10 / \ / 8 ; traversing through array from ( i + 1 ) th position
var price = [ 2 , 30 , 15 , 10 , 8 , 25 , 80 ] ; var profit = 0 ; for ( i = 1 ; i < price . length ; i ++ ) { var sub = price [ i ] - price [ i - 1 ] ; if ( sub > 0 ) profit += sub ; } document . write ( " " + profit ) ;
Find the subarray with least average | Prints beginning and ending indexes of subarray of size k with minimum average ; k must be smaller than or equal to n ; Initialize beginning index of result ; Compute sum of first subarray of size k ; Initialize minimum sum as current sum ; Traverse from ( k + 1 ) ' th ▁ element ▁ ▁ to ▁ n ' th element ; Add current item and remove first item of previous subarray ; Update result if needed ; Driver code ; Subarray size
function findMinAvgSubarray ( arr , n , k ) { if ( n < k ) return ; let res_index = 0 ; let curr_sum = 0 ; for ( let i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; let min_sum = curr_sum ; for ( let i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } document . write ( " " + res_index + " " + ( res_index + k - 1 ) + " " ) ; } let arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] ; let k = 3 ; let n = arr . length ; findMinAvgSubarray ( arr , n , k ) ;
Find element using minimum segments in Seven Segment Display | Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driver code
let seg = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ; function computeSegment ( x ) { if ( x == 0 ) return seg [ 0 ] ; let count = 0 ; while ( x > 0 ) { count += seg [ x % 10 ] ; x = parseInt ( x / 10 , 10 ) ; } return count ; } function elementMinSegment ( arr , n ) { let minseg = computeSegment ( arr [ 0 ] ) ; let minindex = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let temp = computeSegment ( arr [ i ] ) ; if ( temp < minseg ) { minseg = temp ; minindex = i ; } } return arr [ minindex ] ; } let arr = [ 489 , 206 , 745 , 123 , 756 ] ; let n = arr . length ; document . write ( elementMinSegment ( arr , n ) ) ;
Find the Largest number with given number of digits and sum of digits | Prints the smalles possible number with digit sum ' s ' and ' m ' number of digits . ; If sum of digits is 0 , then a number is possible only if number of digits is 1. ; Sum greater than the maximum possible sum . ; Create an array to store digits of result ; Fill from most significant digit to least significant digit . ; Fill 9 first to make the number largest ; If remaining sum becomes less than 9 , then fill the remaining sum ; Driver code
function findLargest ( m , s ) { if ( s == 0 ) { ( m == 1 ) ? document . write ( " " + 0 ) : document . write ( " " ) ; return ; } if ( s > 9 * m ) { document . write ( " " ) ; return ; } let res = new Array ( m ) ; for ( let i = 0 ; i < m ; i ++ ) { if ( s >= 9 ) { res [ i ] = 9 ; s -= 9 ; } else { res [ i ] = s ; s = 0 ; } } document . write ( " " ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( res [ i ] ) ; } let s = 9 , m = 2 ; findLargest ( m , s ) ;
Minimum number of jumps to reach end | Function to return the minimum number of jumps to reach arr [ h ] from arr [ l ] ; Base case : when source and destination are same ; Traverse through all the points reachable from arr [ l ] Recursively , get the minimum number of jumps needed to reach arr [ h ] from these reachable points ; Driver Code
function minJumps ( arr , n ) { if ( n == 1 ) return 0 ; let res = Number . MAX_VALUE ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( i + arr [ i ] >= n - 1 ) { let sub_res = minJumps ( arr , i + 1 ) ; if ( sub_res != Number . MAX_VALUE ) res = Math . min ( res , sub_res + 1 ) ; } } return res ; } let arr = [ 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 ] ; let n = arr . length ; document . write ( " " ) ; document . write ( " " + minJumps ( arr , n ) ) ;
Camel and Banana Puzzle | DP | Stores the overlapping state Initialize dp array with - 1 ; Recursive function to find the maximum number of bananas that can be transferred to A distance using memoization ; Base Case where count of bananas is less that the given distance ; Base Case where count of bananas is less that camel 's capacity ; Base Case where distance = 0 ; If the current state is already calculated ; Stores the maximum count of bananas ; Stores the number of trips to transfer B bananas using a camel of capacity C ; Loop to iterate over all the breakpoints in range [ 1 , A ] ; Recursive call over the remaining path ; Update the maxCount ; Memoize the current value ; Return answer ; Function to find the maximum number of bananas that can be transferred ; Function Call ; Driver Code
let dp = new Array ( 1001 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = ( new Array ( 3001 ) . fill ( - 1 ) ) } function recBananaCnt ( A , B , C ) { if ( B <= A ) { return 0 ; } if ( B <= C ) { return B - A ; } if ( A == 0 ) { return B ; } if ( dp [ A ] [ B ] != - 1 ) { return dp [ A ] [ B ] ; } let maxCount = Number . MIN_VALUE ; let tripCount = B % C == 0 ? Math . floor ( ( 2 * B ) / C ) - 1 : Math . floor ( ( 2 * B ) / C ) + 1 ; for ( let i = 1 ; i <= A ; i ++ ) { let curCount = recBananaCnt ( A - i , B - tripCount * i , C ) ; if ( curCount > maxCount ) { maxCount = curCount ; dp [ A ] [ B ] = maxCount ; } } return maxCount ; } function maxBananaCnt ( A , B , C ) { return recBananaCnt ( A , B , C ) ; } let A = 1000 ; let B = 3000 ; let C = 1000 ; document . write ( maxBananaCnt ( A , B , C ) ) ;
Count of valid arrays of size P with elements in range [ 1 , N ] having duplicates at least M distance apart | Function to calculate the total number of arrays ; If the size of the array is P ; Check if all elements are used atlease once ; Check if this state is already calculated ; Initialize the result ; Use a number from the list of unused numbers ; There are ' unused ' number of favourable choices ; Use a number from already present number atlease M distance back ; There are ' used ▁ - ▁ M ' number of favourable choices ; Store the result ; Function to solve the problem ; Initialize DP table : dp [ i ] [ j ] [ j ] i : current position / index j : number of used elements k : number of unused elements ; Driver Code
function calculate ( position , used , unused , P , M , dp ) { if ( position == P ) { return unused == 0 ? 1 : 0 ; } if ( dp [ position ] [ used ] [ unused ] != - 1 ) return dp [ position ] [ used ] [ unused ] ; let result = 0 ; if ( unused > 0 ) { result += calculate ( position + 1 , used + 1 , unused - 1 , P , M , dp ) * unused ; } if ( used > M ) { result += calculate ( position + 1 , used , unused , P , M , dp ) * ( used - M ) ; } return dp [ position ] [ used ] [ unused ] = result ; } function solve ( N , P , M ) { var dp = new Array ( 101 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 101 ) . fill ( - 1 ) ; } for ( let i = 0 ; i < dp . length ; i ++ ) { for ( let j = 0 ; j < dp [ 0 ] . length ; j ++ ) { dp [ i ] [ j ] = new Array ( 101 ) . fill ( - 1 ) ; } } return calculate ( 0 , 0 , N , P , M , dp ) ; } let N = 2 , M = 0 , P = 3 ; document . write ( solve ( N , P , M ) ) ;
Minimum number of jumps to reach end | Returns Minimum number of jumps to reach end ; jumps [ 0 ] will hold the result ; Minimum number of jumps needed to reach last element from last elements itself is always 0 ; Start from the second element , move from right to left and construct the jumps array where jumps [ i ] represents minimum number of jumps needed to reach arr [ m - 1 ] from arr [ i ] ; If arr [ i ] is 0 then arr [ n - 1 ] can 't be reached from here ; If we can direcly reach to the end povar from here then jumps [ i ] is 1 ; Otherwise , to find out the minimum number of jumps needed to reach arr [ n - 1 ] , check all the points reachable from here and jumps value for those points ; initialize min value ; following loop checks with all reachable points and takes the minimum ; Handle overflow ; or Number . MAX_VALUE ; Driver Code
function minJumps ( arr , n ) { var jumps = Array . from ( { length : n } , ( _ , i ) => 0 ) ; var min ; jumps [ n - 1 ] = 0 ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) jumps [ i ] = Number . MAX_VALUE ; else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ; else { min = Number . MAX_VALUE ; for ( j = i + 1 ; j < n && j <= arr [ i ] + i ; j ++ ) { if ( min > jumps [ j ] ) min = jumps [ j ] ; } if ( min != Number . MAX_VALUE ) jumps [ i ] = min + 1 ; else jumps [ i ] = min ; } } return jumps [ 0 ] ; } var arr = [ 1 , 3 , 6 , 1 , 0 , 9 ] ; var size = arr . length ; document . write ( " " + " " + minJumps ( arr , size ) ) ;
Count N | Stores the dp states ; Check if a number is a prime or not ; Function to generate all prime numbers that are less than or equal to n ; Base cases . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of as non - prime ; Function to find the count of N - digit numbers such that the sum of digits is a prime number ; If end of array is reached ; If the sum is equal to a prime number ; Otherwise ; If the dp - states are already computed ; If index = 1 , any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 also can be placed . ; Otherwise , any digit from [ 0 - 9 ] can be placed . ; Return the answer . ; Find all primes less than or equal to 1000 , which is sufficient for N upto 100 ; Given Input ; Function call
let dp = new Array ( 100 ) . fill ( 0 ) . map ( ( ) => new Array ( 1000 ) . fill ( - 1 ) ) ; let prime = new Array ( 1005 ) . fill ( true ) ; function SieveOfEratosthenes ( n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } function countOfNumbers ( index , sum , N ) { if ( index == N + 1 ) { if ( prime [ sum ] == true ) { return 1 ; } return 0 ; } let val = dp [ index ] [ sum ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( let digit = N == 1 ? 0 : 1 ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } else { for ( let digit = 0 ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } return val ; } SieveOfEratosthenes ( 1000 ) ; let N = 6 ; document . write ( countOfNumbers ( 1 , 0 , N ) ) ;
Maximum number of groups that can receive fresh donuts distributed in batches of size K | Stores the result of the same recursive calls ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Store the key and check if it is present in the hashmap ; If already calculated ; If left is 0 ; Traverse the array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Otherwise , traverse the given array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Memoize the result and return it ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array [ ] arr ; Hashmap to memoize the results ; Store the maximum number of groups ; Return the answer ; Driver Code
let memo ; function dfs ( V , left , K ) { let q = 0 ; let key = V . join ( " " ) ; key += left . toString ( ) ; if ( memo . has ( key ) ) return memo [ key ] ; else if ( left == 0 ) { for ( let i = 1 ; i < K ; ++ i ) if ( V [ i ] > 0 ) { V [ i ] -- ; q = Math . max ( q , 1 + dfs ( V , K - i , K ) ) ; V [ i ] ++ ; } } else { for ( let i = 1 ; i < K ; ++ i ) { if ( V [ i ] > 0 ) { V [ i ] -- ; let nleft = i <= left ? left - i : K + left - i ; q = Math . max ( q , dfs ( V , nleft , K ) ) ; V [ i ] ++ ; } } } if ( memo . has ( key ) ) memo [ key ] = q ; else memo [ key ] = q ; return q ; } function maxGroups ( K , arr ) { let V = new Array ( K ) ; V . fill ( 0 ) ; for ( let x = 0 ; x < arr . length ; x ++ ) V [ arr [ x ] % K ] ++ ; memo = new Map ( ) ; let ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let K = 3 ; document . write ( maxGroups ( K , arr ) ) ;
Maximum Sum Alternating Subarray | Function to find the maximum alternating sum of a subarray for the given array ; Traverse the array ; Store sum of subarrays starting at even indices ; Update sum ; Traverse the array ; Store sum of subarrays starting at odd indices ; Update sum ; Given Input ; Function call
function alternatingSum ( arr ) { var sum = 0 ; var sumSoFar = 0 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 1 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = Math . max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = Math . max ( sum , sumSoFar ) ; } sumSoFar = 0 ; for ( var i = 1 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = Math . max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = Math . max ( sum , sumSoFar ) ; } return sum ; } var arr = new Array ( - 4 , - 10 , 3 , 5 ) ; var ans = alternatingSum ( arr ) ; document . write ( ans ) ;
Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver program to test above function
function smallestSubWithSum ( arr , n , x ) { let min_len = n + 1 ; for ( let start = 0 ; start < n ; start ++ ) { let curr_sum = arr [ start ] ; if ( curr_sum > x ) return 1 ; for ( let end = start + 1 ; end < n ; end ++ ) { curr_sum += arr [ end ] ; if ( curr_sum > x && ( end - start + 1 ) < min_len ) min_len = ( end - start + 1 ) ; } } return min_len ; } let arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ] ; let x = 51 ; let n1 = arr1 . length ; let res1 = smallestSubWithSum ( arr1 , n1 , x ) ; ( res1 == n1 + 1 ) ? document . write ( " " ) : document . write ( res1 + " " ) ; let arr2 = [ 1 , 10 , 5 , 2 , 7 ] ; let n2 = arr2 . length ; x = 9 ; let res2 = smallestSubWithSum ( arr2 , n2 , x ) ; ( res2 == n2 + 1 ) ? document . write ( " " ) : document . write ( res2 + " " ) ; let arr3 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ] ; let n3 = arr3 . length ; x = 280 ; let res3 = smallestSubWithSum ( arr3 , n3 , x ) ; ( res3 == n3 + 1 ) ? document . write ( " " ) : document . write ( res3 + " " ) ;