codeparrot/xlcost-text-to-code huggingface dataset:
Minimum jumps required to reach all array elements using largest element | Function to find next greater element to left and right of current element ; Starting l and r from previous and the next element of the current element ; FInd the next greater element to the left ; Find the next greater element to the right ; Return l and r in the form of array of size 2 ; Function to find the minimum jumps required to reach to all elements from the largest element ; Stores the mapping from index to the element in array A [ ] ; Stores largest array element ; Find the two indices l , r such that A [ l ] > A [ i ] < A [ r ] and l < i < r using expand function ; sorting A in descending order ; Stores the resultant minimum jumps required ; Check if the current element is largest or not ; Find the answer to the current element ; Update the resultant minimum jumps for the current element ; Return the result ; Driver Code ; Prlet the result
function expand ( idx , A ) { let l = idx - 1 ; let r = idx + 1 ; while ( l >= 0 ) { if ( A [ idx ] > A [ l ] ) { -- l ; } else { break ; } } if ( l < 0 l == idx ) { l = - 2 ; } while ( r < A . length ) { if ( A [ idx ] > A [ r ] ) { ++ r ; } else { break ; } } if ( r >= A . length r == idx ) { r = - 2 ; } return [ l , r ] ; } function minJumps ( N , A ) { let ans = new Array ( N ) . fill ( 0 ) ; let mp = new Map ( ) ; let iToA = new Map ( ) ; let AToi = new Map ( ) ; let big = A [ 0 ] ; for ( let i = 0 ; i < N ; ++ i ) { big = Math . max ( big , A [ i ] ) ; mp . set ( i , expand ( i , A ) ) ; iToA . set ( i , A [ i ] ) ; AToi . set ( A [ i ] , i ) ; } A . sort ( ( a , b ) => - a + b ) ; for ( let i = 0 ; i < A . length ; ++ i ) { let m ; if ( A [ i ] == big ) { let cur = AToi . get ( A [ i ] ) ; ans [ cur ] = 0 ; continue ; } let cur = AToi . get ( A [ i ] ) ; let l = mp . get ( cur ) [ 0 ] ; let r = mp . get ( cur ) [ 1 ] ; if ( l >= 0 && r < N ) { m = Math . min ( ans [ l ] , ans [ r ] ) + 1 ; } else if ( l < 0 && r < N ) { m = ans [ r ] + 1 ; } else if ( l >= 0 && r >= N ) { m = ans [ l ] + 1 ; } ans [ cur ] = m ; } return ans ; } let arr = [ 5 , 1 , 3 , 4 , 7 ] ; let N = arr . length ; let out = minJumps ( N , arr ) ; for ( it of out ) document . write ( it + " " ) ;
Largest square sub | Define the prefix sum arrays globally ; Diagonal sum ; Check each row ; Check each column ; Check anti - diagonal ; Store the size of the given grid ; Compute the prefix sum for the rows ; Compute the prefix sum for the columns ; Check for all possible square submatrix ; Driver Code
let prefix_sum_row = new Array ( 50 ) ; for ( let i = 0 ; i < prefix_sum_row . length ; i ++ ) { prefix_sum_row [ i ] = new Array ( 51 ) . fill ( 0 ) ; } let prefix_sum_col = new Array ( 50 ) ; for ( let i = 0 ; i < prefix_sum_col . length ; i ++ ) { prefix_sum_col [ i ] = new Array ( 51 ) . fill ( 0 ) ; } function is_valid ( r , c , size , grid ) { let r_end = r + size , c_end = c + size ; let sum = 0 ; for ( let i = r , j = c ; i < r_end ; i ++ , j ++ ) { sum += grid [ i ] [ j ] ; } for ( let i = r ; i < r_end ; i ++ ) { if ( prefix_sum_row [ i ] [ c_end ] - prefix_sum_row [ i ] != sum ) { return false ; } } for ( let i = c ; i < c_end ; i ++ ) { if ( prefix_sum_col [ r_end ] [ i ] - prefix_sum_col [ r ] [ i ] != sum ) { return false ; } } let ad_sum = 0 ; for ( let i = r , j = c_end - 1 ; i < r_end ; i ++ , j -- ) { ad_sum += grid [ i ] [ j ] ; } return ad_sum == sum ; } function largestSquareValidMatrix ( grid ) { let m = grid . length , n = grid [ 0 ] . length ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { prefix_sum_row [ i ] [ j ] = prefix_sum_row [ i ] [ j - 1 ] + grid [ i ] [ j - 1 ] ; } } for ( let i = 1 ; i <= m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { prefix_sum_col [ i ] [ j ] = prefix_sum_col [ i - 1 ] [ j ] + grid [ i - 1 ] [ j ] ; } } for ( let size = Math . min ( m , n ) ; size > 1 ; size -- ) { for ( let i = 0 ; i <= m - size ; i ++ ) { for ( let j = 0 ; j <= n - size ; j ++ ) { if ( is_valid ( i , j , size , grid ) ) { return size ; } } } } return 1 ; } let grid = [ [ 7 , 1 , 4 , 5 , 6 ] , [ 2 , 5 , 1 , 6 , 4 ] , [ 1 , 5 , 4 , 3 , 2 ] , [ 1 , 2 , 7 , 3 , 4 ] ] ; document . write ( largestSquareValidMatrix ( grid ) ) ;
Minimize product of first 2 ^ Kâ €“ 1 Natural Numbers by swapping bits for any pair any number of times | Function to find the minimum positive product after swapping bits at the similar position for any two numbers ; Stores the resultant number ; range / 2 numbers will be 1 and range / 2 numbers will be range - 1 ; Multiply with the final number ; Return the answer ; Driver Code
function minimumPossibleProduct ( K ) { let res = 1 ; let range = ( 1 << K ) - 1 ; for ( let i = 0 ; i < K ; i ++ ) { res *= ( range - 1 ) ; } res *= range ; return res ; } let K = 3 ; document . write ( minimumPossibleProduct ( K ) ) ;
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle | Function to find the maximum size of the subset of the given array such that a triangle can be formed from any three integers of the subset as sides ; Sort arr [ ] in increasing order ; Stores the maximum size of a valid subset of the given array ; Stores the starting index of the current window ; Stores the last index of the current window ; Iterate over the array arr [ ] ; Increment j till the value of arr [ i ] + arr [ i + 1 ] > arr [ j ] holds true ; Update the value of maxSize ; Return Answer ; Driver Code
const maximizeSubset = ( arr , N ) => { arr . sort ( ( a , b ) => a - b ) let maxSize = 0 ; let i = 0 ; let j = i + 2 ; while ( i < N - 2 ) { while ( arr [ i ] + arr [ i + 1 ] > arr [ j ] ) { j ++ ; } maxSize = Math . max ( maxSize , j - i ) ; i ++ ; j = Math . max ( j , i + 2 ) ; } return maxSize ; } let arr = [ 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 ] ; let N = arr . length ; document . write ( maximizeSubset ( arr , N ) ) ;
Maximum possible value of array elements that can be made based on given capacity conditions | Function to find the maximum element after shifting operations in arr [ ] ; Stores the sum of array element ; Stores the maximum element in cap [ ] ; Iterate to find maximum element ; Return the resultant maximum value ; Driver Code
function maxShiftArrayValue ( arr , cap , N ) { let sumVals = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sumVals += arr [ i ] ; } let maxCapacity = 0 ; for ( let i = 0 ; i < N ; i ++ ) { maxCapacity = Math . max ( cap [ i ] , maxCapacity ) ; } return Math . min ( maxCapacity , sumVals ) ; } let arr = [ 2 , 3 ] ; let cap = [ 5 , 6 ] ; let N = arr . length document . write ( maxShiftArrayValue ( arr , cap , N ) ) ;
Minimum size of Array possible with given sum and product values | Function to find the minimum size of array with sum S and product P ; Base Case ; Iterate through all values of S and check the mentioned condition ; Otherwise , print " - 1" ; Driver Code
function minimumSizeArray ( S , P ) { if ( S == P ) { return 1 ; } for ( let i = 2 ; i <= S ; i ++ ) { let d = i ; if ( ( S / d ) >= Math . pow ( P , 1.0 / d ) ) { return i ; } } return - 1 ; } let S = 5 , P = 6 ; document . write ( minimumSizeArray ( S , P ) ) ;
Smallest possible integer to be added to N | Function to find the smallest positive integer X such that X is added to every element of A except one element to give array B ; Stores the unique elements of array A ; Insert A [ i ] into the set s ; Sort array A [ ] ; Sort array B [ ] ; Assume X value as B [ 0 ] - A [ 1 ] ; Check if X value assumed is negative or 0 ; Update the X value to B [ 0 ] - A [ 0 ] ; If assumed value is wrong ; Update X value ; Driver Code
function findVal ( A , B , N ) { let s = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { s . add ( A [ i ] ) ; } A . sort ( function ( a , b ) { return a - b } ) ; B . sort ( function ( a , b ) { return a - b } ) let X = B [ 0 ] - A [ 1 ] ; if ( X <= 0 ) X = B [ 0 ] - A [ 0 ] ; else { for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( s . has ( B [ i ] - X ) == false ) { X = B [ 0 ] - A [ 0 ] ; break ; } } } document . write ( X ) ; } let A = [ 1 , 4 , 3 , 8 ] ; let B = [ 15 , 8 , 11 ] ; let N = A . length ; findVal ( A , B , N ) ;
Count of integral points that lie at a distance D from origin | Function to find the total valid integer coordinates at a distance D from origin ; Stores the count of valid points ; Iterate over possibel x coordinates ; Find the respective y coordinate with the pythagoras theorem ; Adding 4 to compensate the coordinates present on x and y axes . ; Return the answer ; Driver Code
function countPoints ( D ) { let count = 0 ; for ( let x = 1 ; x * x < D * D ; x ++ ) { let y = Math . floor ( Math . sqrt ( D * D - x * x ) ) ; if ( x * x + y * y == D * D ) { count += 4 ; } } count += 4 ; return count ; } let D = 5 ; document . write ( countPoints ( D ) ) ;
Sum of the shortest distance between all 0 s to 1 in given binary string | JavaScript program for the above approach ; Function to find the total sum of the shortest distance between every 0 to 1 in a given binary string ; Stores the prefix distance and suffix distance from 0 to 1 ; Stores the current distance from 1 to 0 ; Marks the 1 ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign prefixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update prefixDistance [ i ] ; Assign prefixDistance [ i ] as INT_MAX ; Assign haveOne as false ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign the suffixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update suffixDistance [ i ] as cnt ; Assign suffixDistance [ i ] as INT_MAX ; Stores the total sum of distances between 0 to nearest 1 ; If current character is 0 ; Update the value of sum ; Print the value of the sum ; Driver Code
const INT_MAX = 2147483647 const findTotalDistance = ( S , N ) => { let prefixDistance = new Array ( N ) . fill ( 0 ) ; let suffixDistance = new Array ( N ) . fill ( 0 ) ; let cnt = 0 ; let haveOne = false ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ' ' ) { haveOne = true ; cnt = 0 ; prefixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; prefixDistance [ i ] = cnt ; } else prefixDistance [ i ] = INT_MAX ; } haveOne = false ; for ( let i = N - 1 ; i >= 0 ; -- i ) { if ( S [ i ] == ' ' ) { haveOne = true ; cnt = 0 ; suffixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; suffixDistance [ i ] = cnt ; } else suffixDistance [ i ] = INT_MAX ; } let sum = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ' ' ) { sum += Math . min ( prefixDistance [ i ] , suffixDistance [ i ] ) ; } } document . write ( ` ${ sum } ` ) ; } let S = " " ; let N = S . length ; findTotalDistance ( S , N ) ;
Minimize value of | A | Function to find the minimum possible value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z for given A , B and C ; Stores the minimum value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z ; Iterate over all values of i in the range [ 1 , 2 * C ] ; Iterate over all values of j such that i * j <= 2 * c ; Update the value of ans ; Return answer ; Driver Code
function minimizeCost ( A , B , C ) { let ans = A + B + C ; for ( let i = 1 ; i <= 2 * C ; i ++ ) { let j = 0 ; while ( i * j <= 2 * C ) { ans = Math . min ( ans , Math . abs ( A - i ) + Math . abs ( B - j ) + Math . abs ( i * j - C ) ) ; j ++ ; } } return ans ; } let A = 19 , B = 28 , C = 522 ; document . write ( minimizeCost ( A , B , C ) ) ;
Average value of set bit count in given Binary string after performing all possible choices of K operations | Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array arr [ ] and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code
function averageSetBits ( N , K , arr ) { let p = N ; let q = 0 ; for ( let i = 0 ; i < K ; i ++ ) { let _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } let N = 5 ; let arr = [ 1 , 2 , 3 ] ; let K = arr . length ; document . write ( averageSetBits ( N , K , arr ) . toPrecision ( 3 ) ) ;
Find maximum sum of subsequence after flipping signs of at most K elements in given Array | Function to calculate the max sum of subsequence ; Variable to store the max sum ; Sort the array ; Iterate over the array ; Flip sign ; Decrement k ; Traverse over the array ; Add only positive elements ; Return the max sum ; Given array ; Variable to store number of flips are allowed ; Function call to find the maximum sum of subsequence
const maxSubseq = ( arr , N , K ) => { let sum = 0 ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( K == 0 ) break ; if ( arr [ i ] < 0 ) { arr [ i ] = - arr [ i ] ; K -- ; } } for ( let i = 0 ; i < N ; i ++ ) if ( arr [ i ] > 0 ) sum += arr [ i ] ; return sum ; } let arr = [ 6 , - 10 , - 1 , 0 , - 4 , 2 ] ; let K = 2 ; let N = arr . length document . write ( maxSubseq ( arr , N , K ) ) ;
Minimum number of intervals to cover the target interval | Function to find the minimum number of intervals in the array A [ ] to cover the entire target interval ; Sort the array A [ ] in increasing order of starting point ; Insert a pair of INT_MAX to prevent going out of bounds ; Stores start of current interval ; Stores end of current leterval ; Stores the count of intervals ; Iterate over all the intervals ; If starting point of current index <= start ; Update the value of start ; Increment the value of count ; If the target interval is already covered or it is not possible to move then break the loop ; If the entire target interval is not covered ; Return Answer ; Driver Code
function minimizeSegment ( A , X ) { A . sort ( function ( a , b ) { return a . first - b . first ; } ) A . push ( { first : Number . MAX_VALUE , second : Number . MAX_VALUE } ) ; let start = X . first ; let end = X . first - 1 ; let cnt = 0 ; for ( let i = 0 ; i < A . length ; ) { if ( A [ i ] . first <= start ) { end = Math . max ( A [ i ++ ] . second , end ) ; } else { start = end ; ++ cnt ; if ( A [ i ] . first > end end >= X . second ) { break ; } } } if ( end < X . second ) { return - 1 ; } return cnt ; } let A = [ { first : 1 , second : 3 } , { first : 2 , second : 4 } , { first : 2 , second : 10 } , { first : 2 , second : 3 } , { first : 1 , second : 1 } ] ; let X = { first : 1 , second : 10 } ; document . write ( minimizeSegment ( A , X ) ) ;
K | Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
function KthSolution ( X , K ) { let ans = 0 ; for ( let i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1 << i ) ) ) { if ( K & 1 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } let X = 5 , K = 5 ; document . write ( KthSolution ( X , K ) ) ;
Rearrange sorted array such that all odd indices elements comes before all even indices element | Function to print the array ; Function to rearrange the array such that odd indexed elements come before even indexed elements ; Reduces the size of array by one because last element does not need to be changed in case N = odd ; Initialize the variables ; Iterate over the range ; Add the modified element at the odd position ; Iterate over the range ; Add the modified element at the even position ; Iterate over the range ; Divide by thr maximum element ; Driver Code
function printTheArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) document . write ( Math . floor ( arr [ i ] ) + " " ) ; } function rearrange ( arr , N ) { if ( N & 1 ) N -- ; let odd_idx = 1 , even_idx = 0 ; let i , max_elem = arr [ N - 1 ] + 1 ; for ( i = 0 ; i < N / 2 ; i ++ ) { arr [ i ] += ( arr [ odd_idx ] % max_elem ) * max_elem ; odd_idx += 2 ; } for ( ; i < N ; i ++ ) { arr [ i ] += ( arr [ even_idx ] % max_elem ) * max_elem ; even_idx += 2 ; } for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] / max_elem ; } } let arr = [ 1 , 2 , 3 , 4 , 5 , 16 , 18 , 19 ] ; let N = arr . length ; rearrange ( arr , N ) ; printTheArray ( arr , N ) ;
Find range of values for S in given Array with values satisfying [ arr [ i ] = floor ( ( i * S ) / K ) ] | Function to find the range of values for S in a given array that satisfies the given condition ; Stores the left range value ; Stores the right range value ; Find the current left range value for S ; Find the current right range value for S ; Updating L value ; Updating R value ; Driver code
function findRange ( arr , N , K ) { let L = Number . MIN_VALUE ; let R = Number . MAX_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let l = Math . ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; let r = Math . ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = Math . max ( L , l ) ; R = Math . min ( R , r ) ; } document . write ( L + " " + R ) ; } let arr = [ 2 , 4 , 6 , 9 , 11 ] ; let K = 10 ; let N = arr . length ; findRange ( arr , N , K ) ;
Find an anagram of given String having different characters at corresponding indices | Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code
function swap ( str , i , j ) { if ( i == j ) { return str ; } if ( j < i ) { var temp = j ; j = i ; i = temp ; } if ( i >= str . length ) { return str ; } return str . substring ( 0 , i ) + str [ j ] + str . substring ( i + 1 , j ) + str [ i ] + str . substring ( j + 1 ) ; } function findAnagram ( s ) { let check = s . slice ( ) ; let i = 0 , j = s . length - 1 ; while ( i < s . length && j >= 0 ) { if ( s [ i ] != s [ j ] && check [ i ] != s [ j ] && check [ j ] != s [ i ] ) { s = swap ( s , i , j ) i ++ ; j = s . length - 1 ; } else { j -- ; } } if ( s . length % 2 != 0 ) { let mid = s . length / 2 ; if ( check [ mid ] == s [ mid ] ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( check [ i ] != s [ mid ] && s [ i ] != s [ mid ] ) { s = swap ( s , i , mid ) break ; } } } } let ok = true ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( check [ i ] == s [ i ] ) { ok = false ; break ; } } if ( ok ) document . write ( s ) ; else document . write ( - 1 ) ; } let S = " " ; findAnagram ( S ) ;
Lexicographically smallest permutation of [ 1 , N ] based on given Binary string | Function to generate the lexicographically smallest permutation according to the given criteria ; Stores the resultant permutation ; Initialize the first elements to 1 ; Traverse the given string S ; Number greater than last number ; Number equal to the last number ; Correct all numbers to the left of the current index ; Printing the permutation ; Driver Code
function constructPermutation ( S , N ) { let ans = new Array ( N ) ; ans [ 0 ] = 1 ; for ( let i = 1 ; i < N ; ++ i ) { if ( S [ i - 1 ] == ' ' ) { ans [ i ] = i + 1 ; } else { ans [ i ] = ans [ i - 1 ] ; } for ( let j = 0 ; j < i ; ++ j ) { if ( ans [ j ] >= ans [ i ] ) { ans [ j ] ++ ; } } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( ans [ i ] ) ; if ( i != N - 1 ) { document . write ( " " ) ; } } } let S = " " ; constructPermutation ( S , S . length + 1 ) ;
Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X | Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
function KthSolution ( X , K ) { let ans = 0 ; for ( let i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1 << i ) ) ) { if ( K & 1 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } let X = 10 , K = 5 ; document . write ( KthSolution ( X , K ) ) ;
Count of distinct N | Function to find the count of distinct arrays of size n having elements in range [ 1 , k ] and all adjacent elements ( P , Q ) follows ( P <= Q ) or ( P % Q > 0 ) ; Stores the divisors of all integers in the range [ 1 , k ] ; Calculate the divisors of all integers using the Sieve ; Stores the dp states such that dp [ i ] [ j ] with i elements having j as the last element of array ; Initialize the dp array ; Calculate the dp states using the derived relation ; Calculate the sum for len - 1 ; Subtract dp [ len - 1 ] [ j ] for each factor of j from [ 1 , K ] ; Calculate the final result ; Return the resultant sum ; Driver Code
function countArrays ( n , k ) { let divisors = new Array ( k + 1 ) . fill ( new Array ( ) ) ; for ( let i = 1 ; i <= k ; i ++ ) { for ( let j = 2 * i ; j <= k ; j += i ) { divisors [ j ] . push ( i ) ; } } let dp = new Array ( n + 1 ) . fill ( new Array ( k + 1 ) ) ; for ( let j = 1 ; j <= k ; j ++ ) { dp [ 1 ] [ j ] = 1 ; } for ( let x = 2 ; x <= n ; x ++ ) { let sum = 0 ; for ( let j = 1 ; j <= k ; j ++ ) { sum += dp [ x - 1 ] [ j ] ; } for ( let y = 1 ; y <= k ; y ++ ) { dp [ x ] [ y ] = sum ; for ( let d of divisors [ y ] ) { dp [ x ] [ y ] = ( dp [ x ] [ y ] - dp [ x - 1 ] [ d ] ) ; } } } let sum = 0 ; for ( let j = 1 ; j <= k ; j ++ ) { sum += dp [ n ] [ j ] ; } sum ++ ; return sum ; } let N = 2 , K = 3 ; document . write ( countArrays ( N , K ) ) ;
Minimize increment | Function to calculate the minimum number of operations to convert array A to array B by incrementing and decrementing adjacent elements ; Stores the final count ; Stores the sum of array A and B respectivelly ; Check of the sums are unequall ; Pointer to iterate through array ; Case 1 where A [ i ] > B [ i ] ; Stores the extra values for the current index ; Iterate the array from [ i - 1 , 0 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Iterate the array in right direction id A [ i ] - B [ i ] > 0 ; Iterate the array from [ i + 1 , n - 1 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Return Answer ; Driver Code ; Function Call
function minimumMoves ( A , B , N ) { var ans = 0 ; var i ; var sum_A = 0 , sum_B = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum_A += A [ i ] ; } for ( i = 0 ; i < N ; i ++ ) { sum_B += B [ i ] ; } if ( sum_A != sum_B ) { return - 1 ; } var i = 0 ; while ( i < N ) { if ( A [ i ] > B [ i ] ) { var temp = A [ i ] - B [ i ] ; var j = i - 1 ; while ( j >= 0 && temp > 0 ) { if ( B [ j ] > A [ j ] ) { var cnt = Math . min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . abs ( j - i ) ) ; } j -- ; } if ( temp > 0 ) { var j = i + 1 ; while ( j < N && temp > 0 ) { if ( B [ j ] > A [ j ] ) { var cnt = Math . min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . abs ( j - i ) ) ; } j ++ ; } } } i ++ ; } return ans ; } var A = [ 1 , 5 , 7 ] ; var B = [ 13 , 0 , 0 ] ; var N = A . length ; document . write ( minimumMoves ( A , B , N ) ) ;
Check if X can be reduced to 0 in exactly T moves by substracting D or 1 from it | Function to check the above problem condition ; Check for base cases ; Check if D - 1 is a divisor of X - T ; Driver Code
function possibleReachingSequence ( X , D , T ) { if ( X < T ) { document . write ( " " ) ; return 0 ; } if ( T * D < X ) { document . write ( " " ) ; return 0 ; } if ( ( X - T ) % ( D - 1 ) == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } return 0 ; } let X = 10 , D = 3 , T = 6 ; possibleReachingSequence ( X , D , T ) ;