codeparrot/xlcost-text-to-code huggingface dataset:
Split an array into minimum number of non | Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , arr [ ] ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; If previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Given array ; Given size of array
function minimumSubarrays ( arr , n ) { let current = ' ' ; let answer = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( current == ' ' ) { if ( arr [ i ] < arr [ i - 1 ] ) { current = ' ' ; } else if ( arr [ i ] == arr [ i - 1 ] ) { current = ' ' ; } else { current = ' ' ; } } else if ( current == ' ' ) { if ( arr [ i ] >= arr [ i - 1 ] ) { current = ' ' ; } else { current = ' ' ; answer += 1 ; } } else { if ( arr [ i ] <= arr [ i - 1 ] ) { current = ' ' ; } else { current = ' ' ; answer += 1 ; } } } document . write ( answer ) ; } let arr = [ 2 , 3 , 9 , 5 , 4 , 6 , 8 ] ; let n = arr . length ; minimumSubarrays ( arr , n ) ;
Convert a Matrix into another Matrix of given dimensions | Function to cona matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Prlet the resultant matrix ; Driver Code
function ConstMatrix ( mat , N , M , A , B ) { if ( N * M != A * B ) return ; let idx = 0 ; let res = new Array ( A ) ; for ( var i = 0 ; i < res . length ; i ++ ) { res [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { res [ Math . floor ( idx / B ) ] [ idx % B ] = mat [ i ] [ j ] ; idx ++ ; } } for ( let i = 0 ; i < A ; i ++ ) { for ( let j = 0 ; j < B ; j ++ ) document . write ( res [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ; let A = 2 ; let B = 3 ; let N = mat . length ; let M = mat [ 0 ] . length ; ConstMatrix ( mat , N , M , A , B ) ;
Modify given array by reducing each element by its next smaller element | Function to print the final array after reducing each array element by its next smaller element ; Initialize stack ; Array size ; To store the corresponding element ; If stack is not empty ; If top element is smaller than the current element ; Keep popping until stack is empty or top element is greater than the current element ; If stack is not empty ; Push current element ; Print the final array ; Given array ; Function call
function printFinalPrices ( arr ) { var minStk = [ ] var n = arr . length ; var i ; var reduce = Array ( n ) . fill ( 0 ) ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( minStk . length > 0 ) { if ( minStk [ minStk . length - 1 ] <= arr [ i ] ) { reduce [ i ] = minStk [ minStk . length - 1 ] ; } else { while ( minStk . length > 0 && ( minStk [ minStk . length - 1 ] > arr [ i ] ) ) { minStk . pop ( ) ; } if ( minStk . length > 0 ) { reduce [ i ] = minStk [ minStk . length - 1 ] ; } } } minStk . push ( arr [ i ] ) ; } for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] - reduce [ i ] + " " ) ; } var arr = [ 8 , 4 , 6 , 2 , 3 ] ; printFinalPrices ( arr ) ;
Generate array having differences between count of occurrences of every array element on its left and right | Function to construct array of differences of counts on the left and right of the given array ; Initialize left and right frequency arrays ; Construct left cumulative frequency table ; Construct right cumulative frequency table ; Print the result ; Driver Code ; Function Call
function constructArray ( A , N ) { let left = [ ] ; let right = [ ] ; let X = [ ] ; let Y = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { X [ i ] = 0 ; left [ i ] = 0 ; right [ i ] = 0 ; Y [ i ] = 0 ; } for ( let i = 0 ; i < N ; i ++ ) { X [ i ] = left [ A [ i ] ] ; left [ A [ i ] ] ++ ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { Y [ i ] = right [ A [ i ] ] ; right [ A [ i ] ] ++ ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( Y [ i ] - X [ i ] + " " ) ; } } let A = [ 3 , 2 , 1 , 2 , 3 ] ; let N = A . length ; constructArray ( A , N ) ;
Mean of distinct odd fibonacci nodes in a Linked List | Structure of a singly Linked List ; Stores data value of a Node ; Stores pointer to next Node ; Function to insert a node at the beginning of the singly Linked List ; Create a new Node ; Insert the data into the Node ; Insert pointer to the next Node ; Update head_ref ; Function to find the largest element from the linked list ; Stores the largest element in the linked list ; Iterate over the linked list ; If max is less than head . data ; Update max ; Update head ; Function to store all Fibonacci numbers up to the largest element of the list ; Store all Fibonacci numbers up to Max ; Stores first element of Fibonacci number ; Stores second element of Fibonacci number ; Insert prev into hashmap ; Insert curr into hashmap ; Insert all elements of Fibonacci numbers up to Max ; Stores current fibonacci number ; Insert temp into hashmap ; Update prev ; Update curr ; Function to find the mean of odd Fibonacci nodes ; Stores the largest element in the linked list ; Stores all fibonacci numbers up to Max ; Stores current node of linked list ; Stores count of odd Fibonacci nodes ; Stores sum of all odd fibonacci nodes ; Traverse the linked list ; if the data value of current node is an odd number ; if data value of the node is present in hashmap ; Update cnt ; Update sum ; Remove current fibonacci number from hashmap so that duplicate elements can 't be counted ; Update curr ; Return the required mean ; Driver Code Stores head node of the linked list ; Insert all data values in the linked list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } function largestElement ( head_ref ) { var Max = - 1000000000 ; var head = head_ref ; while ( head != null ) { if ( Max < head . data ) { Max = head . data ; } head = head . next ; } return Max ; } function createHashMap ( Max ) { var hashmap = new Set ( ) ; var prev = 0 ; var curr = 1 ; hashmap . add ( prev ) ; hashmap . add ( curr ) ; while ( curr <= Max ) { var temp = curr + prev ; hashmap . add ( temp ) ; prev = curr ; curr = temp ; } return hashmap ; } function meanofnodes ( head ) { var Max = largestElement ( head ) ; var hashmap = createHashMap ( Max ) ; var curr = head ; var cnt = 0 ; var sum = 0.0 ; while ( curr != null ) { if ( ( curr . data ) & 1 ) { if ( hashmap . has ( curr . data ) ) { cnt ++ ; sum += curr . data ; hashmap . delete ( curr . data ) ; } } curr = curr . next ; } return ( sum / cnt ) ; } var head = null ; head = push ( head , 5 ) ; head = push ( head , 21 ) ; head = push ( head , 8 ) ; head = push ( head , 12 ) ; head = push ( head , 3 ) ; head = push ( head , 13 ) ; head = push ( head , 144 ) ; head = push ( head , 6 ) ; document . write ( meanofnodes ( head ) ) ;
Generate an alternate odd | Function to print the required sequence ; Print ith odd number ; Driver Code
function findNumbers ( n ) { var i = 0 ; while ( i <= n ) { document . write ( 2 * i * i + 4 * i + 1 + i % 2 + " " ) ; i ++ ; } } var n = 6 ; findNumbers ( n ) ;
Program to calculate gross salary of a person | Function to calculate the salary of the person ; Condition to compute the allowance for the person ; Calculate gross salary ; Driver code ; Function call
function computeSalary ( basic , grade ) { let allowance ; let hra , da , pf ; hra = 0.2 * basic ; da = 0.5 * basic ; pf = 0.11 * basic ; if ( grade == ' ' ) { allowance = 1700.0 ; } else if ( grade == ' ' ) { allowance = 1500.0 ; } else { allowance = 1300.0 ; } let gross ; gross = Math . round ( basic + hra + da + allowance - pf ) ; return gross ; } let basic = 10000 ; let grade = ' ' ; document . write ( computeSalary ( basic , grade ) ) ;
Rearrange and update array elements as specified by the given queries | Function to perform the given operations ; Dequeue to store the array elements ; Insert all element of the array into the dequeue ; Stores the size of the queue ; Traverse each query ; Query for left shift . ; Extract the element at the front of the queue ; Pop the element at the front of the queue ; Push the element at the back of the queue ; Query for right shift ; Extract the element at the back of the queue ; Pop the element at the back of the queue ; Push the element at the front of the queue ; Query for update ; Query to get the value ; Driver Code ; All possible Queries
function Queries ( arr , N , Q ) { let dq = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { dq . push ( arr [ i ] ) ; } let sz = Q . length ; for ( let i = 0 ; i < sz ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { let front = dq [ 0 ] ; dq . shift ( ) ; dq . push ( front ) ; } else if ( Q [ i ] [ 0 ] == 1 ) { let back = dq [ dq . length - 1 ] ; dq . pop ( ) ; dq . unshift ( back ) ; } else if ( Q [ i ] [ 0 ] == 2 ) { dq [ Q [ i ] [ 1 ] ] = Q [ i ] [ 2 ] ; } else { document . write ( dq [ Q [ i ] [ 1 ] ] + " " ) ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; let Q = [ [ 0 ] , [ 1 ] , [ 3 , 1 ] , [ 2 , 2 , 54 ] , [ 3 , 2 ] ] ; Queries ( arr , N , Q ) ;
Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal | Function ot find the length of the smallest subarray ; Stores ( prefix Sum , index ) as ( key , value ) mappings ; Iterate till N ; Update the prefixSum ; Update the length ; Put the latest index to find the minimum length ; Update the length ; Return the answer ; Function to find the length of the largest subarray ; Stores the sum of all array elements after modification ; Change greater than k to 1 ; Change smaller than k to - 1 ; Change equal to k to 0 ; Update total_sum ; No deletion required , return 0 ; Delete smallest subarray that has sum = total_sum ; Driver Code ; Function call
function smallSubarray ( arr , n , total_sum ) { let m = new Map ( ) ; let length = Number . MAX_VALUE ; let prefixSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixSum == total_sum ) { length = Math . min ( length , i + 1 ) ; } m . set ( prefixSum , i ) ; if ( m . has ( prefixSum - total_sum ) ) { length = Math . min ( length , i - m . get ( prefixSum - total_sum ) ) ; } } return length ; } function smallestSubarrayremoved ( arr , n , k ) { let total_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > k ) { arr [ i ] = 1 ; } else if ( arr [ i ] < k ) { arr [ i ] = - 1 ; } else { arr [ i ] = 0 ; } total_sum += arr [ i ] ; } if ( total_sum == 0 ) { return 0 ; } else { return smallSubarray ( arr , n , total_sum ) ; } } let arr = [ 12 , 16 , 12 , 13 , 10 ] ; let K = 13 ; let n = arr . length ; document . write ( smallestSubarrayremoved ( arr , n , K ) ) ;
Balanced Ternary Number System | Javascript program to convert positive decimals into balanced ternary system ; Driver code
function balancedTernary ( n ) { var output = " " ; while ( n > 0 ) { var rem = n % 3 ; n = parseInt ( n / 3 ) ; if ( rem == 2 ) { rem = - 1 ; n ++ ; } output = ( rem == 0 ? ' ' : ( rem == 1 ) ? ' ' : ' ' ) + output ; } return output ; } var n = 238 ; document . write ( " " + n + " " + balancedTernary ( n ) ) ;
Spt function or Smallest Parts Function of a given number | variable to store spt function of a number ; Function to add value of frequency of minimum element among all representations of n ; find the value of frequency of minimum element ; calculate spt ; Recursive function to find different ways in which n can be written as a sum of at one or more positive integers ; if sum becomes n , consider this representation ; start from previous element in the representation till n ; include current element from representation ; call function again with reduced sum ; backtrack - remove current element from representation ; Function to find the spt function ; Using recurrence find different ways in which n can be written as a sum of at 1 or more positive integers ; Driver Code
var spt = 0 ; function printVector ( arr ) { var min_i = 1000000000 ; for ( var i = 0 ; i < arr . length ; i ++ ) min_i = Math . min ( min_i , arr [ i ] ) ; var freq = 0 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == min_i ) freq ++ ; } spt += freq ; } function findWays ( arr , i , n ) { if ( n == 0 ) printVector ( arr ) ; for ( var j = i ; j <= n ; j ++ ) { arr . push ( j ) ; findWays ( arr , j , n - j ) ; arr . pop ( ) ; } } function spt_function ( n ) { var arr = [ ] ; findWays ( arr , 1 , n ) ; document . write ( spt ) ; } var N = 4 ; spt_function ( N ) ;
Find initial integral solution of Linear Diophantine equation if finite solution exists | Javascript program for the above approach ; Function to implement the extended euclid algorithm ; Base Case ; Recursively find the gcd ; Function to prlet the solutions of the given equations ax + by = c ; Condition for infinite solutions ; Condition for no solutions exist ; Condition for no solutions exist ; Print the solution ; Driver Code ; Given coefficients ; Function Call
let x , y ; function gcd_extend ( a , b ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } else { let g = gcd_extend ( b , a % b ) ; let x1 = x , y1 = y ; x = y1 ; y = x1 - Math . floor ( a / b ) * y1 ; return g ; } } function print_solution ( a , b , c ) { if ( a == 0 && b == 0 ) { if ( c == 0 ) { document . write ( " " + " " + " " ) ; } else { document . write ( " " + " " ) ; } } let gcd = gcd_extend ( a , b ) ; if ( c % gcd != 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + x * ( c / gcd ) + " " + y * ( c / gcd ) + " " ) ; } } let a , b , c ; a = 4 ; b = 18 ; c = 10 ; print_solution ( a , b , c ) ;
Count of pairs whose bitwise AND is a power of 2 | Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to return the number of valid pairs ; Iterate for all possible pairs ; Bitwise and value of the pair is passed ; Return the final count ; Given array arr [ ] ; Function call
function check ( x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } function count ( arr , n ) { let cnt = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( check ( arr [ i ] & arr [ j ] ) ) cnt ++ ; } } return cnt ; } let arr = [ 6 , 4 , 2 , 3 ] ; let n = arr . length ; document . write ( count ( arr , n ) ) ;
Count of all possible pairs of array elements with same parity | Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver code
function countPairs ( A , n ) { let count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( A [ i ] % 2 == 0 && A [ j ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ j ] % 2 != 0 ) ) count ++ ; } } return count ; } let A = [ 1 , 2 , 3 , 1 , 3 ] ; let n = A . length ; document . write ( countPairs ( A , n ) ) ;
Smallest number whose sum of digits is square of N | Function to return smallest number whose sum of digits is n ^ 2 ; Driver Code
function smallestNum ( n ) { return parseInt ( n * n % 9 + 1 ) * Math . pow ( 10 , parseInt ( n * n / 9 ) ) - 1 ; } let n = 4 ; document . write ( smallestNum ( n ) ) ;
Second heptagonal numbers | Function to find N - th term in the series ; Driver code ; Function call
function findNthTerm ( n ) { document . write ( parseInt ( ( n * ( 5 * n + 3 ) ) / 2 ) ) ; } let N = 4 ; findNthTerm ( N ) ;
Weakly Prime Numbers | function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to check if n is a Weakly Prime Number ; number should be prime ; converting N to string ; loop to change digit at every character one by one . ; loop to store every digit one by one at index j ; Driver Code Given Number N
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isWeaklyPrimeNum ( N ) { if ( ! isPrime ( N ) ) return false ; var s = N . toString ( ) ; for ( var j = 0 ; j < s . length ; j ++ ) { var str = s ; for ( var i = 0 ; i <= 9 ; i ++ ) { var c = ' ' + i . toString ( ) ; str [ j ] = c ; var Num = parseInt ( str ) ; if ( str [ j ] != s [ j ] && isPrime ( Num ) ) { return false ; } } } return true ; } var N = 294001 ; if ( isWeaklyPrimeNum ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Unprimeable Numbers | function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to check if n is a unprimeable Number ; number should be composite ; converting N to string ; loop to change digit at every character one by one . ; loop to store every digit one by one at index j ; Driver Code ; Function Call
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isUnPrimeableNum ( N ) { if ( isPrime ( N ) ) return false ; var s = N . toString ( ) ; for ( var j = 0 ; j < s . length ; j ++ ) { var str = s ; for ( var i = 0 ; i <= 9 ; i ++ ) { var c = ' ' + parseInt ( i ) ; str [ j ] = c ; var Num = parseInt ( str ) ; if ( str [ j ] != s [ j ] && isPrime ( Num ) ) { return false ; } } } return true ; } var N = 200 ; if ( isUnPrimeableNum ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the Nth row in Pascal 's Triangle | Function to find the elements of rowIndex in Pascal 's Triangle ; 1 st element of every row is 1 ; Check if the row that has to be returned is the first row ; Generate the previous row ; Generate the elements of the current row by the help of the previous row ; Return the row ; Driver code
function getRow ( rowIndex ) { let currow = [ ] ; currow . push ( 1 ) ; if ( rowIndex == 0 ) { return currow ; } let prev = getRow ( rowIndex - 1 ) ; for ( let i = 1 ; i < prev . length ; i ++ ) { let curr = prev [ i - 1 ] + prev [ i ] ; currow . push ( curr ) ; } currow . push ( 1 ) ; return currow ; } let n = 3 ; let arr = getRow ( n ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( i == arr . length - 1 ) document . write ( arr [ i ] ) ; else document . write ( arr [ i ] + " " ) ; }
All possible values of floor ( N / K ) for all values of K | Function to print all possible values of floor ( N / K ) ; loop from 1 to N + 1 ; Driver code
function allQuotients ( N ) { var s = new Set ( ) ; for ( var k = 1 ; k <= N + 1 ; k ++ ) { s . add ( parseInt ( N / k ) ) ; } var ls = Array . from ( s ) . reverse ( ) ; ls . forEach ( v => document . write ( v + " " ) ) } var N = 5 ; allQuotients ( N ) ;
Greatest odd factor of an even number | Function to print greatest odd factor ; Initialize i with 1 ; Iterate till i <= pow_2 ; Find the pow ( 2 , i ) ; If factor is odd , then print the number and break ; Given Number ; Function Call
function greatestOddFactor ( n ) { let pow_2 = ( Math . log ( n ) ) ; let i = 1 ; while ( i <= pow_2 ) { let fac_2 = ( 2 * i ) ; if ( n % fac_2 == 0 ) { if ( ( n / fac_2 ) % 2 == 1 ) { return ( n / fac_2 ) ; } } i += 1 ; } return 0 ; } let N = 8642 ; document . write ( greatestOddFactor ( N ) ) ; ;
Find the element in a linked list with frequency at least N / 3 | Structure of a node for the linked list ; Utility function to create a node ; Function to find and return the element with frequency of at least N / 3 ; Candidates for being the required majority element ; Store the frequencies of the respective candidates ; Iterate all nodes ; Increase frequency of candidate s ; Increase frequency of candidate t ; Set the new sting as candidate for majority ; Set the new sting as second candidate for majority ; Decrease the frequency ; Check the frequency of two final selected candidate linklist ; Increase the frequency of first candidate ; Increase the frequency of second candidate ; Return the string with higher frequency ; Driver Code
class node { constructor ( ) { this . i = " " ; this . next = null ; } } function newnode ( s ) { var temp = new node ( ) ; temp . i = s ; temp . next = null ; return temp ; } function Majority_in_linklist ( head ) { var s = " " ; var t = " " ; var p = 0 , q = 0 ; var ptr = null ; while ( head != null ) { if ( s == head . i ) { p = p + 1 ; } else { if ( t == head . i ) { q = q + 1 ; } else { if ( p == 0 ) { s = head . i ; p = 1 ; } else { if ( q == 0 ) { t = head . i ; q = 1 ; } else { p = p - 1 ; q = q - 1 ; } } } } head = head . next ; } head = ptr ; p = 0 ; q = 0 ; while ( head != null ) { if ( s == head . i ) { p = 1 ; } else { if ( t == head . i ) { q = 1 ; } } head = head . next ; } if ( p > q ) { return s ; } else { return t ; } } var head = newnode ( " " ) ; head . next = newnode ( " " ) ; head . next . next = newnode ( " " ) ; head . next . next . next = newnode ( " " ) ; head . next . next . next . next = newnode ( " " ) ; head . next . next . next . next . next = newnode ( " " ) ; head . next . next . next . next . next . next = newnode ( " " ) ; head . next . next . next . next . next . next . next = newnode ( " " ) ; document . write ( Majority_in_linklist ( head ) ) ;
Form smallest number using indices of numbers chosen from Array with sum less than S | Function to find the minimum number which have maximum length ; Find Maximum length of number ; Find minimum number WHich have maximum length ; Driver Code
function max_number ( arr , sum ) { let frac = new Array ( 9 ) ; let maxi = Number . MIN_VALUE ; let ans = [ ] ; let pos = 0 ; for ( let i = 0 ; i < 9 ; i ++ ) { frac [ i ] = Math . floor ( sum / arr [ i ] ) ; if ( frac [ i ] > maxi ) { pos = i ; maxi = frac [ i ] ; } } for ( let i = 0 ; i < maxi ; i ++ ) { ans . push ( String . fromCharCode ( ( pos + 1 ) + ' ' . charCodeAt ( 0 ) ) ) ; } sum -= maxi * arr [ pos ] ; for ( let i = 0 ; i < maxi ; i ++ ) { for ( let j = 1 ; j <= 9 ; j ++ ) { if ( sum + arr [ pos ] - arr [ j - 1 ] >= 0 ) { ans [ i ] = String . fromCharCode ( ( j + ' ' . charCodeAt ( 0 ) ) ) ; sum += arr [ pos ] - arr [ j - 1 ] ; break ; } } } if ( maxi == 0 ) { return " " ; } else { return ans . join ( " " ) ; } } let arr = [ 3 , 4 , 2 , 4 , 6 , 5 , 4 , 2 , 3 ] ; let s = 13 ; document . write ( max_number ( arr , s ) ) ;