codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum length sub | Javascript implementation of the approach ; Function to return the minimum length of a sub - array which contains { 0 , 1 , 2 , 3 , 4 } as a sub - sequence ; To store the indices where 0 , 1 , 2 , 3 and 4 are present ; To store if there exist a valid prefix of sequence in array ; Base Case ; If current element is 0 ; Update the count of 0 s till now ; Push the index of the new 0 ; To check if previous element of the given sequence is found till now ; If it is the end of sequence ; Iterate for other elements of the sequence ; Binary Search to find closest occurrence less than equal to starting point ; Update the starting point ; Driver Code
let MAX_INT = 1000000 ; function solve ( array , N ) { let pos = new Array ( 5 ) ; for ( let i = 0 ; i < 5 ; i ++ ) { pos [ i ] = new Array ( 10000 ) ; for ( let j = 0 ; j < 10000 ; j ++ ) { pos [ i ] [ j ] = 0 ; } } let pref = new Array ( 5 ) ; for ( let i = 0 ; i < 5 ; i ++ ) { pref [ i ] = 0 ; } if ( array [ 0 ] == 0 ) { pref [ 0 ] = 1 ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = 0 ; } let ans = MAX_INT ; for ( let i = 1 ; i < N ; i ++ ) { if ( array [ i ] == 0 ) { pref [ 0 ] ++ ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = i ; } else { if ( pref [ array [ i ] - 1 ] > 0 ) { pref [ array [ i ] ] ++ ; pos [ array [ i ] ] [ pos [ array [ i ] ] . length - 1 ] = i ; if ( array [ i ] == 4 ) { let end = i ; let start = i ; for ( let j = 3 ; j >= 0 ; j -- ) { let s = 0 ; let e = pos [ j ] . length - 1 ; let temp = - 1 ; while ( s <= e ) { let m = Math . floor ( ( s + e ) / 2 ) ; if ( pos [ j ] [ m ] <= start ) { temp = pos [ j ] [ m ] ; s = m + 1 ; } else e = m - 1 ; } start = temp ; } ans = Math . min ( ans , end - start + 1 ) ; } } } } return ans ; } let array = [ 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 ] ; let N = array . length ; document . write ( solve ( array , N ) ) ;
Find optimal weights which can be used to weigh all the weights in the range [ 1 , X ] | Function to find the optimal weights ; Number of weights required ; Finding the value of required powers of 3 ; Optimal Weights are powers of 3 ; Driver code
function findWeights ( X ) { let sum = 0 ; let power = 0 ; let number = 3 ; while ( sum < X ) { sum = number - 1 ; sum = Math . floor ( sum / 2 ) ; power ++ ; number *= 3 ; } let ans = 1 ; for ( let i = 1 ; i <= power ; i ++ ) { document . write ( ans + " " ) ; ans = ans * 3 ; } } let X = 2 ; findWeights ( X ) ;
Order of indices which is lexicographically smallest and sum of elements is <= X | Function to return the chosen indices ; Maximum indices chosen ; Try to replace the first element in ans by i , making the order lexicographically smaller ; If no further replacement possible return ; If found an index smaller than ind and sum not exceeding X then remove index of smallest value from ans and then add index i to ans ; Driver code ; Print the chosen indices
function solve ( X , A ) { let min = Number . MAX_VALUE ; let ind = - 1 ; for ( let i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] < min ) { min = A [ i ] ; ind = i ; } } let maxIndChosen = Math . floor ( X / min ) ; let ans = [ ] ; if ( maxIndChosen == 0 ) { return ans ; } for ( let i = 0 ; i < maxIndChosen ; i ++ ) { ans . push ( ind ) ; } let temp = maxIndChosen ; let sum = maxIndChosen * A [ ind ] ; for ( let i = 0 ; i < ind ; i ++ ) { if ( sum - X == 0 temp == 0 ) break ; while ( ( sum - A [ ind ] + A [ i ] ) <= X && temp != 0 ) { ans . shift ( ) ; ans . push ( i ) ; temp -- ; sum += ( A [ i ] - A [ ind ] ) ; } } ans . sort ( function ( a , b ) { return a - b ; } ) ; return ans ; } let A = [ 5 , 6 , 4 , 8 ] ; let X = 18 ; let ans = solve ( X , A ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ;
Find if a binary matrix exists with given row and column sums | Function to check if matrix exists ; Store sum of rowsums , max of row sum number of non zero row sums ; Store sum of column sums , max of column sum number of non zero column sums ; Check condition 1 , 2 , 3 ; Driver Code
function matrix_exist ( row , column , r , c ) { var row_sum = 0 ; var column_sum = 0 ; var row_max = - 1 ; var column_max = - 1 ; var row_non_zero = 0 ; var column_non_zero = 0 ; for ( var i = 0 ; i < r ; i ++ ) { row_sum += row [ i ] ; row_max = Math . max ( row_max , row [ i ] ) ; if ( row [ i ] ) row_non_zero ++ ; } for ( var i = 0 ; i < c ; i ++ ) { column_sum += column [ i ] ; column_max = Math . max ( column_max , column [ i ] ) ; if ( column [ i ] ) column_non_zero ++ ; } if ( ( row_sum != column_sum ) || ( row_max > column_non_zero ) || ( column_max > row_non_zero ) ) return false ; return true ; } var row = [ 2 , 2 , 2 , 2 , 2 ] ; var column = [ 5 , 5 , 0 , 0 ] ; var r = row . length ; var c = column . length ; if ( matrix_exist ( row , column , r , c ) ) document . write ( " " ) ; else document . write ( " " ) ;
Length of longest sub | Function to find maximum distance between unequal elements ; Calculate maxMean ; Iterate over array and calculate largest subarray with all elements greater or equal to maxMean ; Driver code
function longestSubarray ( arr , n ) { var maxMean = 0 ; for ( var i = 1 ; i < n ; i ++ ) maxMean = Math . max ( maxMean , parseInt ( ( arr [ i ] + arr [ i - 1 ] ) / 2 ) ) ; var ans = 0 ; var subarrayLength = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] >= maxMean ) ans = Math . max ( ans , ++ subarrayLength ) ; else subarrayLength = 0 ; return ans ; } var arr = [ 4 , 3 , 3 , 2 , 1 , 4 ] ; var n = arr . length ; document . write ( longestSubarray ( arr , n ) ) ;
Maximum distance between two unequal elements | Function to return the maximum distance between two unequal elements ; If first and last elements are unequal they are maximum distance apart ; Fix first element as one of the elements and start traversing from the right ; Break for the first unequal element ; To store the distance from the first element ; Fix last element as one of the elements and start traversing from the left ; Break for the first unequal element ; To store the distance from the last element ; Maximum possible distance ; Driver code
function maxDistance ( arr , n ) { if ( arr [ 0 ] != arr [ n - 1 ] ) return ( n - 1 ) ; var i = n - 1 ; while ( i > 0 ) { if ( arr [ i ] != arr [ 0 ] ) break ; i -- ; } var distFirst = ( i == 0 ) ? - 1 : i ; i = 0 ; while ( i < n - 1 ) { if ( arr [ i ] != arr [ n - 1 ] ) break ; i ++ ; } var distLast = ( i == n - 1 ) ? - 1 : ( n - 1 - i ) ; var maxDist = Math . max ( distFirst , distLast ) ; return maxDist ; } var arr = [ 4 , 4 , 1 , 2 , 1 , 4 ] ; var n = arr . length ; document . write ( maxDistance ( arr , n ) ) ;
Generate an array of K elements such that sum of elements is N and the condition a [ i ] < a [ i + 1 ] <= 2 * a [ i ] is met | Set 2 | Function that print the desired array which satisfies the given conditions ; If the lowest filling condition is void , then it is not possible to generate the required array ; Increase all the elements by cnt ; Start filling from the back till the number is a [ i + 1 ] <= 2 * a [ i ] ; Get the number to be filled ; If it is less than the remaining numbers to be filled ; less than remaining numbers to be filled ; Get the sum of the array ; If this condition is void at any stage during filling up , then print - 1 ; Else add it to the sum ; If the sum condition is not satisified , then print - 1 ; Print the generated array ; Driver code
function solve ( n , k ) { let mini = 0 ; let x1 = 1 ; let a = new Array ( k ) ; for ( let i = 1 ; i <= k ; i ++ ) { mini += x1 ; a [ i - 1 ] = x1 ; x1 += 1 ; } if ( n < mini ) { document . write ( " " ) ; return ; } let rem = n - mini ; let cnt = parseInt ( rem / k ) ; rem = rem % k ; for ( let i = 0 ; i < k ; i ++ ) a [ i ] += cnt ; for ( let i = k - 1 ; i > 0 && rem > 0 ; i -- ) { let xx = a [ i - 1 ] * 2 ; let left = xx - a [ i ] ; if ( rem >= left ) { a [ i ] = xx ; rem -= left ; } else { a [ i ] += rem ; rem = 0 ; } } let sum = a [ 0 ] ; for ( let i = 1 ; i < k ; i ++ ) { if ( a [ i ] > 2 * a [ i - 1 ] ) { document . write ( " " ) ; return ; } sum += a [ i ] ; } if ( sum != n ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < k ; i ++ ) document . write ( a [ i ] + " " ) ; } let n = 26 , k = 6 ; solve ( n , k ) ;
Minimizing array sum by applying XOR operation on all elements of the array | Javascript implementation of the approach ; Function to return the minimized sum ; To store the frequency of bit in every element ; Finding element X ; Taking XOR of elements and finding sum ; Driver code
const MAX = 25 ; function getMinSum ( arr , n ) { let bits_count = new Array ( MAX ) . fill ( 0 ) , max_bit = 0 , sum = 0 , ans = 0 ; for ( let d = 0 ; d < n ; d ++ ) { let e = arr [ d ] , f = 0 ; while ( e > 0 ) { let rem = e % 2 ; e = parseInt ( e / 2 ) ; if ( rem == 1 ) { bits_count [ f ] += rem ; } f ++ ; } max_bit = Math . max ( max_bit , f ) ; } for ( let d = 0 ; d < max_bit ; d ++ ) { let temp = Math . pow ( 2 , d ) ; if ( bits_count [ d ] > parseInt ( n / 2 ) ) ans = ans + temp ; } for ( let d = 0 ; d < n ; d ++ ) { arr [ d ] = arr [ d ] ^ ans ; sum = sum + arr [ d ] ; } return sum ; } let arr = [ 3 , 5 , 7 , 11 , 15 ] ; let n = arr . length ; document . write ( getMinSum ( arr , n ) ) ;
Maximum money that can be withdrawn in two steps | Function to return the maximum coins we can get ; Update elements such that X > Y ; Take from the maximum ; Refill ; Again , take the maximum ; Driver code
function maxCoins ( X , Y ) { if ( X < Y ) { let temp = X ; X = Y ; Y = temp ; } let coins = X ; X -- ; coins += Math . max ( X , Y ) ; return coins ; } let X = 7 , Y = 5 ; document . write ( maxCoins ( X , Y ) ) ;
Maximum elements which can be crossed using given units of a and b | Function to find the number of elements crossed ; Keep a copy of a ; Iterate in the binary array ; If no a and b left to use ; If there is no a ; use b and increase a by 1 if arr [ i ] is 1 ; simply use b ; Use a if theres no b ; Increase a and use b if arr [ i ] == 1 ; Use a ; Driver code
function findElementsCrossed ( arr , a , b , n ) { var aa = a ; var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a == 0 && b == 0 ) break ; else if ( a == 0 ) { if ( arr [ i ] == 1 ) { b -= 1 ; a = Math . min ( aa , a + 1 ) ; } else b -= 1 ; } else if ( b == 0 ) a -- ; else if ( arr [ i ] == 1 && a < aa ) { b -= 1 ; a = Math . min ( aa , a + 1 ) ; } else a -- ; ans ++ ; } return ans ; } var arr = [ 1 , 0 , 0 , 1 , 0 , 1 ] ; var n = arr . length ; var a = 1 ; var b = 2 ; document . write ( findElementsCrossed ( arr , a , b , n ) ) ;
Move all zeros to start and ones to end in an Array of random integers | Utility function to print the contents of an array ; Function that pushes all the zeros to the start and ones to the end of an array ; To store the count of elements which are not equal to 1 ; Traverse the array and calculate count of elements which are not 1 ; Now all non - ones elements have been shifted to front and ' count1' is set as index of first 1. Make all elements 1 from count to end . ; Initialize lastNonBinary position to zero ; Traverse the array and pull non - zero elements to the required indices ; Ignore the 1 's ; Mark the position Of last NonBinary integer ; Place non - zero element to their required indices ; Put zeros to start of array ; Driver code
function printArr ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function pushBinaryToBorder ( arr , n ) { var count1 = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 1 ) arr [ count1 ++ ] = arr [ i ] ; while ( count1 < n ) arr [ count1 ++ ] = 1 ; var lastNonOne = 0 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == 1 ) continue ; if ( ! lastNonOne ) { lastNonOne = i ; } if ( arr [ i ] != 0 ) arr [ lastNonOne -- ] = arr [ i ] ; } while ( lastNonOne >= 0 ) arr [ lastNonOne -- ] = 0 ; } var arr = [ 1 , 2 , 0 , 0 , 0 , 3 , 6 ] ; var n = arr . length ; pushBinaryToBorder ( arr , n ) ; printArr ( arr , n ) ;
Count the pairs of vowels in the given string | Function that return true if character ch is a vowel ; Function to return the count of adjacent vowel pairs in the given string ; If current character and the character after it are both vowels ; Driver Code
function isVowel ( ch ) { switch ( ch ) { case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : return true ; default : return false ; } } function vowelPairs ( s , n ) { let cnt = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( isVowel ( s [ i ] ) && isVowel ( s [ i + 1 ] ) ) cnt ++ ; } return cnt ; } let s = " " ; let n = s . length ; document . write ( vowelPairs ( s , n ) ) ;
Minimum possible final health of the last monster in a game | Function to return the gcd of two numbers ; Function to return the minimum possible health for the monster ; gcd of first and second element ; gcd for all subsequent elements ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function solve ( health , n ) { let currentgcd = gcd ( health [ 0 ] , health [ 1 ] ) ; for ( let i = 2 ; i < n ; ++ i ) { currentgcd = gcd ( currentgcd , health [ i ] ) ; } return currentgcd ; } let health = [ 4 , 6 , 8 , 12 ] ; let n = health . length ; document . write ( solve ( health , n ) ) ;
Divide array into increasing and decreasing subsequence without changing the order | Function to print strictly increasing and strictly decreasing sequence if possible ; Arrays to store strictly increasing and decreasing sequence ; Initializing last element of both sequence ; Iterating through the array ; If current element can be appended to both the sequences ; If next element is greater than the current element Then append it to the strictly increasing array ; Otherwise append it to the strictly decreasing array ; If current element can be appended to the increasing sequence only ; If current element can be appended to the decreasing sequence only ; Else we can not make such sequences from the given array ; Print the required sequences ; Driver Code
function Find_Sequence ( arr , n ) { let inc_arr = [ ] , dec_arr = [ ] ; let flag = 0 ; let inc = - 1 , dec = 1e7 ; for ( let i = 0 ; i < n ; i ++ ) { if ( inc < arr [ i ] && arr [ i ] < dec ) { if ( arr [ i ] < arr [ i + 1 ] ) { inc = arr [ i ] ; inc_arr . push ( arr [ i ] ) ; } else { dec = arr [ i ] ; dec_arr . push ( arr [ i ] ) ; } } else if ( inc < arr [ i ] ) { inc = arr [ i ] ; inc_arr . push ( arr [ i ] ) ; } else if ( dec > arr [ i ] ) { dec = arr [ i ] ; dec_arr . push ( arr [ i ] ) ; } else { document . write ( - 1 ) ; flag = 1 ; break ; } } if ( flag == 0 ) { document . write ( " " ) ; for ( let i in inc_arr ) document . write ( inc_arr [ i ] + " " ) ; document . write ( " " ) ; document . write ( " " ) ; for ( let i in dec_arr ) document . write ( dec_arr [ i ] + " " ) ; document . write ( " " ) ; } } let arr = [ 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 ] ; let n = arr . length ; Find_Sequence ( arr , n ) ;
Find the sum of digits of a number at even and odd places | Function to return the reverse of a number ; Function to find the sum of the odd and even positioned digits in a number ; If c is even number then it means digit extracted is at even place ; function call
function reverse ( n ) { let rev = 0 ; while ( n != 0 ) { rev = ( rev * 10 ) + ( n % 10 ) ; n = Math . floor ( n / 10 ) ; } return rev ; } function getSum ( n ) { n = reverse ( n ) ; let sumOdd = 0 , sumEven = 0 , c = 1 ; while ( n != 0 ) { if ( c % 2 == 0 ) sumEven += n % 10 ; else sumOdd += n % 10 ; n = Math . floor ( n / 10 ) ; c ++ ; } document . write ( " " + sumOdd ) ; document . write ( " " ) ; document . write ( " " + sumEven ) ; } let n = 457892 ; getSum ( n ) ;
Find the sum of digits of a number at even and odd places | Function to find the sum of the odd and even positioned digits in a number ; If n is odd then the last digit will be odd positioned ; To store the respective sums ; While there are digits left process ; If current digit is odd positioned ; Even positioned digit ; Invert state ; Remove last digit ; Driver code
function getSum ( n ) { let isOdd = ( n % 2 == 1 ) ? true : false ; let sumOdd = 0 , sumEven = 0 ; while ( n != 0 ) { if ( isOdd ) sumOdd += n % 10 ; else sumEven += n % 10 ; isOdd = ! isOdd ; n = Math . floor ( n / 10 ) ; } document . write ( " " + sumOdd + " " ) ; document . write ( " " + sumEven ) ; } let n = 457892 ; getSum ( n ) ;
Count the number of currency notes needed | Function to return the amount of notes with value A required ; If possible ; Driver Code
function bankNotes ( A , B , S , N ) { let numerator = S - ( B * N ) ; let denominator = A - B ; if ( numerator % denominator == 0 ) return ( Math . floor ( numerator / denominator ) ) ; return - 1 ; } let A = 1 , B = 2 , S = 7 , N = 5 ; document . write ( bankNotes ( A , B , S , N ) + " " ) ;
Divide a number into two parts | Function to print the two parts ; Find the position of 4 ; If current character is not '4' but appears after the first occurrence of '4' ; Print both the parts ; Driver code
function twoParts ( str ) { var flag = 0 ; var a = " " ; var gfg = str . split ( ' ' ) ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( gfg [ i ] == ' ' ) { gfg [ i ] = ' ' ; a += ' ' ; flag = 1 ; } else if ( flag != 0 ) a += ' ' ; } document . write ( gfg . join ( ' ' ) + " " + a ) ; } var str = " " ; twoParts ( str ) ;
Length of the longest substring with no consecutive same letters | Function to return the length of the required sub - string ; Get the length of the string ; Iterate in the string ; Check for not consecutive ; If cnt greater than maxi ; Re - initialize ; Check after iteration is complete ; Driver code
function longestSubstring ( s ) { var cnt = 1 , maxi = 1 ; var n = s . length ; for ( i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) cnt ++ ; else { maxi = Math . max ( cnt , maxi ) ; cnt = 1 ; } } maxi = Math . max ( cnt , maxi ) ; return maxi ; } var s = " " ; document . write ( longestSubstring ( s ) ) ;
Minimum number of changes such that elements are first Negative and then Positive | Function to return the count of minimum operations required ; To store the count of negative letegers on the right of the current index ( inclusive ) ; Find the count of negative letegers on the right ; If current element is negative ; To store the count of positive elements ; Find the positive letegers on the left ; If current element is positive ; Update the answer ; Return the required answer ; Driver Code
function Minimum_Operations ( a , n ) { let np = Array ( n + 1 ) . fill ( 0 ) ; np [ n ] = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { np [ i ] = np [ i + 1 ] ; if ( a [ i ] <= 0 ) np [ i ] ++ ; } let pos = 0 ; let ans = n ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] >= 0 ) pos ++ ; ans = Math . min ( ans , pos + np [ i + 1 ] ) ; } return ans ; } let a = [ - 1 , 0 , 1 , 2 ] ; let n = a . length ; document . write ( Minimum_Operations ( a , n ) ) ;
Sum of elements in an array whose difference with the mean of another array is less than k | Function for finding sum of elements whose diff with mean is not more than k ; Find the mean of second array ; Find sum of elements from array1 whose difference with mean in not more than k ; Return result ; Driver code
function findSumofEle ( arr1 , m , arr2 , n , k ) { var arraySum = 0 ; for ( var i = 0 ; i < n ; i ++ ) arraySum += arr2 [ i ] ; var mean = ( arraySum / n ) ; var sumOfElements = 0 ; var difference ; for ( var i = 0 ; i < m ; i ++ ) { difference = arr1 [ i ] - mean ; if ( ( difference < 0 ) && ( k > ( - 1 ) * difference ) ) { sumOfElements += arr1 [ i ] ; } if ( ( difference >= 0 ) && ( k > difference ) ) { sumOfElements += arr1 [ i ] ; } } return sumOfElements ; } var arr1 = [ 1 , 2 , 3 , 4 , 7 , 9 ] ; var arr2 = [ 0 , 1 , 2 , 1 , 1 , 4 ] ; var k = 2 ; var m , n ; m = arr1 . length ; n = arr2 . length ; document . write ( findSumofEle ( arr1 , m , arr2 , n , k ) ) ;
Find n positive integers that satisfy the given equations | Function to find n positive integers that satisfy the given conditions ; To store n positive integers ; Place N - 1 one 's ; If can not place ( y - ( n - 1 ) ) as the Nth integer ; Place Nth integer ; To store the sum of squares of N integers ; If it is less than x ; Print the required integers ; Driver Code
function findIntegers ( n , x , y ) { let ans = [ ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) ans . push ( 1 ) ; if ( y - ( n - 1 ) <= 0 ) { document . write ( " " ) ; return ; } ans . push ( y - ( n - 1 ) ) ; let store = 0 ; for ( let i = 0 ; i < n ; i ++ ) store += ans [ i ] * ans [ i ] ; if ( store < x ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( ans [ ( i ) ] + " " ) ; } let n = 3 , x = 254 , y = 18 ; findIntegers ( n , x , y ) ;
Find the minimum number of steps to reach M from N | Function to find a minimum number of steps to reach M from N ; Continue till m is greater than n ; If m is odd ; add one ; divide m by 2 ; Return the required answer ; Driver code
function Minsteps ( n , m ) { let ans = 0 ; while ( m > n ) { if ( m & 1 ) { m ++ ; ans ++ ; } m = Math . floor ( m / 2 ) ; ans ++ ; } return ans + n - m ; } let n = 4 , m = 6 ; document . write ( Minsteps ( n , m ) ) ;
Find the number of jumps to reach X in the number line from zero | Utility function to calculate sum of numbers from 1 to x ; Function to find the number of jumps to reach X in the number line from zero ; First make number positive Answer will be same either it is Positive or negative ; To store required answer ; Continue till number is lesser or not in same parity ; Return the required answer ; Driver Code
function getsum ( x ) { return ( x * ( x + 1 ) ) / 2 ; } function countJumps ( n ) { n = Math . abs ( n ) ; let ans = 0 ; while ( getsum ( ans ) < n || ( ( getsum ( ans ) - n ) & 1 ) > 0 ) ans ++ ; return ans ; } let n = 9 ; document . write ( countJumps ( n ) ) ;
Maximum number of candies that can be bought | Function to return the maximum candies that can be bought ; Buy all the candies of the last type ; Starting from second last ; Amount of candies of the current type that can be bought ; Add candies of current type that can be bought ; Update the previous bought amount ; Driver Code
function maxCandies ( arr , n ) { let prevBought = arr [ n - 1 ] ; let candies = prevBought ; for ( let i = n - 2 ; i >= 0 ; i -- ) { let x = Math . min ( prevBought - 1 , arr [ i ] ) ; if ( x >= 0 ) { candies += x ; prevBought = x ; } } return candies ; } let arr = [ 1 , 2 , 1 , 3 , 6 ] ; let n = arr . length ; document . write ( maxCandies ( arr , n ) ) ;
Replace all elements by difference of sums of positive and negative numbers after that element | Function to prvar the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; Calculate absolute sums of positive and negative elements in range i + 1 to N ; Calculate difference of both sums ; Replace i - th elements with absolute difference ; Driver code
function printArray ( N , arr ) { for ( i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } function replacedArray ( N , arr ) { var pos_sum , neg_sum , i , j , diff ; for ( i = 0 ; i < N ; i ++ ) { pos_sum = 0 ; neg_sum = 0 ; for ( j = i + 1 ; j < N ; j ++ ) { if ( arr [ j ] > 0 ) pos_sum += arr [ j ] ; else neg_sum += arr [ j ] ; } diff = Math . abs ( pos_sum ) - Math . abs ( neg_sum ) ; arr [ i ] = Math . abs ( diff ) ; } } var N = 5 ; var arr = [ 1 , - 1 , 2 , 3 , - 2 ] ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; var arr1 = [ - 3 , - 4 , - 2 , 5 , 1 , - 2 ] ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ;