codeparrot/xlcost-text-to-code huggingface dataset:
Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Given array and K ; Function Call
function make_cuts ( arr , n , K ) { var ans = 0 ; var poss = [ ] ; var ce = 0 ; var co = 0 ; var x ; for ( x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . push ( Math . abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } poss . sort ( ) ; var i ; for ( i = 0 ; i < poss . length ; i ++ ) { if ( K >= poss [ i ] ) { ans ++ ; K -= poss [ i ] ; } else break ; } return ans ; } var N = 6 ; var K = 4 ; var arr = [ 1 , 2 , 5 , 10 , 15 , 20 ] ; document . write ( make_cuts ( arr , N , K ) ) ;
Cost of rearranging the array such that no element exceeds the sum of its adjacent elements | Function to check if given elements can be arranged such that sum of its neighbours is strictly greater ; Initialize the total cost ; Storing the original index of elements in a hashmap ; Sort the given array ; Check if a given condition is satisfies or not ; First number ; Add the cost to overall cost ; Last number ; Add the cost to overall cost ; Add the cost to overall cost ; Printing the cost ; Given array ; Function call
function Arrange ( arr , n ) { let cost = 0 ; let index = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { index . set ( arr [ i ] , i ) } arr . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] > arr [ i + 1 ] + arr [ n - 1 ] ) { document . write ( " " ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } else if ( i == n - 1 ) { if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) { document . write ( " " ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } else { if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) { document . write ( " " ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } } document . write ( cost ) ; return ; } let arr = [ 2 , 4 , 5 , 1 , 3 ] ; let N = arr . length ; Arrange ( arr , N ) ;
XOR of all possible pairwise sum from two given Arrays | Function to calculate the sum of XOR of the sum of every pair ; Stores the XOR of sums of every pair ; Iterate to generate all possible pairs ; Update XOR ; Return the answer ; Driver Code
function XorSum ( A , B , N ) { var ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { ans = ans ^ ( A [ i ] + B [ j ] ) ; } } return ans ; } var A = [ 4 , 6 , 0 , 0 , 3 , 3 ] ; var B = [ 0 , 5 , 6 , 5 , 0 , 3 ] ; var N = A . length ; document . write ( XorSum ( A , B , N ) ) ;
Maximum non | Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the final answer ; Count this character if freq 1 ; Return count of distinct characters ; Given string ; Given k ; Function Call
function maxDistinctChar ( s , n , k ) { var freq = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freq . has ( s [ i ] ) ) freq . set ( s [ i ] , freq . get ( s [ i ] ) + 1 ) else freq . set ( s [ i ] , 1 ) } var v = [ ] ; freq . forEach ( ( value , key ) => { v . push ( value ) ; } ) ; v . sort ( ) for ( var i = 0 ; i < v . length ; i ++ ) { var mn = Math . min ( v [ i ] - 1 , k ) ; v [ i ] -= mn ; k -= mn ; } if ( k > 0 ) { for ( var i = 0 ; i < v . length ; i ++ ) { var mn = Math . min ( v [ i ] , k ) ; v [ i ] -= mn ; k -= mn ; } } var res = 0 ; for ( var i = 0 ; i < v . length ; i ++ ) if ( v [ i ] == 1 ) res ++ ; return res ; } var S = " " ; var N = S . length ; var K = 1 ; document . write ( maxDistinctChar ( S , N , K ) ) ;
Split N as the sum of K numbers satisfying the given conditions | Array to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code
let primes = new Array ( ) ; function SieveOfEratosthenes ( ) { let prime = new Array ( 10005 ) ; prime . fill ( true ) ; for ( let p = 2 ; p * p <= 1000 ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= 1000 ; i += p ) prime [ i ] = false ; } } for ( let p = 2 ; p <= 1000 ; p ++ ) if ( prime [ p ] ) primes . push ( p ) ; } function generate ( n , k ) { let prod = new Array ( ) ; SieveOfEratosthenes ( ) ; let l = primes . length ; for ( let i = 0 ; i < l ; i ++ ) { for ( let j = i + 1 ; j < l ; j ++ ) { if ( primes [ i ] * primes [ j ] > 0 ) prod . push ( primes [ i ] * primes [ j ] ) ; } } prod . sort ( ( a , b ) => a - b ) let sum = 0 ; for ( let i = 0 ; i < k - 1 ; i ++ ) sum += prod [ i ] ; if ( sum > n ) document . write ( " " ) ; else { for ( let i = 0 ; i < k - 1 ; i ++ ) { document . write ( prod [ i ] + " " ) ; } document . write ( n - sum + " " ) ; } } let n = 52 , k = 5 ; generate ( n , k ) ;
Maximum modified Array sum possible by choosing elements as per the given conditions | Function that finds the maximum sum of the array elements according to the given condition ; Sort the array ; Take the max value from the array ; Iterate from the end of the array ; Check for the number had come before or not ; Take the value which is not occured already ; Change max to new value ; Return the maximum sum ; Given array arr [ ] ; Function Call
function findMaxValue ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; let ans = arr [ n - 1 ] ; let maxPossible = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; -- i ) { if ( maxPossible > 0 ) { if ( arr [ i ] >= maxPossible ) { ans += ( maxPossible - 1 ) ; maxPossible = maxPossible - 1 ; } else { maxPossible = arr [ i ] ; ans += maxPossible ; } } } return ans ; } let arr = [ 4 , 4 , 1 , 5 ] ; let n = arr . length ; document . write ( findMaxValue ( arr , n ) ) ;
Maximize the last Array element as per the given conditions | Function to find the maximum possible value that can be placed at the last index ; Sort array in ascending order ; If the first element is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code
function maximizeFinalElement ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; if ( arr [ 0 ] != 1 ) arr [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 1 ) { arr [ i ] = arr [ i - 1 ] + 1 ; } } return arr [ n - 1 ] ; } let n = 4 ; let arr = [ 3 , 1 , 3 , 4 ] ; let max = maximizeFinalElement ( arr , n ) ; document . write ( max ) ;
Find the point on X | Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
function findLeastDist ( A , N ) { A . sort ( ( a , b ) => a - b ) ; console . log ( A ) ; if ( ( N % 2 ) == 0 ) { return A [ parseInt ( ( N - 1 ) / 2 ) ] ; } else { return A [ parseInt ( N / 2 ) ] ; } } var A = [ 4 , 1 , 5 , 10 , 2 ] ; var N = A . length ; document . write ( " " + findLeastDist ( A , N ) + " " + 0 + " " ) ;
Maximum sum of any submatrix of a Matrix which is sorted row | Function that finds the maximum Sub - Matrix Sum ; Number of rows in the matrix ; Number of columns in the matrix ; dp matrix to store the results of each iteration ; Base Case - The largest element in the matrix ; To stores the final result ; Find the max sub matrix sum for the last row ; Check whether the current sub - array yields maximum sum ; Calculate the max sub matrix sum for the last column ; Check whether the current sub - array yields maximum sum ; Build the dp matrix from bottom to the top row ; Update sum at each cell in dp ; Update the maximum sum ; Return the maximum sum ; Given matrix mat ; Function Call
function maxSubMatSum ( mat ) { var n = mat . length ; var m = mat [ 0 ] . length ; var i , j ; var dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( m ) . fill ( 0 ) ) ; dp [ n - 1 ] [ m - 1 ] = mat [ n - 1 ] [ m - 1 ] ; var res = dp [ n - 1 ] [ m - 1 ] ; for ( i = m - 2 ; i >= 0 ; i -- ) { dp [ n - 1 ] [ i ] = mat [ n - 1 ] [ i ] + dp [ n - 1 ] [ i + 1 ] ; res = Math . max ( res , dp [ n - 1 ] [ i ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { dp [ i ] [ m - 1 ] = mat [ i ] [ m - 1 ] + dp [ i + 1 ] [ m - 1 ] ; res = Math . max ( res , dp [ i ] [ m - 1 ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = m - 2 ; j >= 0 ; j -- ) { dp [ i ] [ j ] = mat [ i ] [ j ] + dp [ i ] [ j + 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j + 1 ] ; res = Math . max ( res , dp [ i ] [ j ] ) ; } } return res ; } var mat = [ [ - 6 , - 4 , - 1 ] , [ - 3 , 2 , 4 ] , [ 2 , 5 , 8 ] ] ; document . write ( maxSubMatSum ( mat ) ) ;
Arrange the numbers in the Array as per given inequalities | Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the final integer ; Return the answer ; Given List of Integers ; Given list of inequalities ; Function Call ; Print the output
function formAnInequality ( integers , inequalities ) { ( integers ) . sort ( function ( a , b ) { return a - b ; } ) ; let lowerIndex = 0 ; let higherIndex = integers . length - 1 ; let sb = " " ; for ( let ch = 0 ; ch < inequalities . length ; ch ++ ) { if ( inequalities [ ch ] == ' ' ) { sb += " " + ( integers [ lowerIndex ++ ] ) + " " + inequalities [ ch ] ; } else { sb += " " + ( integers [ higherIndex -- ] ) + " " + inequalities [ ch ] ; } } sb += " " + ( integers [ lowerIndex ] ) ; return sb ; } let integers = [ 2 , 5 , 1 , 0 ] ; let inequalities = [ ' ' , ' ' , ' ' ] ; let output = formAnInequality ( integers , inequalities ) ; document . write ( output ) ;
Check if two arrays can be made equal by reversing subarrays multiple times | Function to check if array B can be made equal to array A ; sort both the arrays ; Check if both the arrays are equal or not ; Driver Code
function canMadeEqual ( A , B , n ) { A . sort ( ) ; B . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) if ( A [ i ] != B [ i ] ) return false ; return true ; } var A = [ 1 , 2 , 3 ] ; var B = [ 1 , 3 , 2 ] ; var n = A . length ; if ( canMadeEqual ( A , B , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimize difference between maximum and minimum of Array by at most K replacements | Function to find minimum difference between the maximum and the minimum elements arr [ ] by at most K replacements ; Check if turns are more than or equal to n - 1 then simply return zero ; Sort the array ; Set difference as the maximum possible difference ; Iterate over the array to track the minimum difference in k turns ; Return the answer ; Given array arr [ ] ; Given K replacements ; Function Call
function maxMinDifference ( arr , n , k ) { if ( k >= n - 1 ) return 0 ; arr . sort ( ( a , b ) => a - b ) ; let ans = arr [ n - 1 ] - arr [ 0 ] ; for ( let i = k , j = n - 1 ; i >= 0 ; -- i , -- j ) { ans = Math . min ( arr [ j ] - arr [ i ] , ans ) ; } return ans ; } let arr = [ 1 , 4 , 6 , 11 , 15 ] ; let N = arr . length ; let K = 3 ; document . write ( maxMinDifference ( arr , N , K ) ) ;
Check if a decreasing Array can be sorted using Triple cyclic shift | Javascript program for the above approach ; If array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; Check if its possible to sort ; Number of swap is N / 2 always for this approach ; Printing index of the cyclic right shift ; Driver code
function sortarray ( arr , N ) { if ( N == 3 ) document . write ( " " ) ; else if ( N % 4 == 0 N % 4 == 1 ) { document . write ( " " ) ; document . write ( Math . floor ( N / 2 ) + " " ) ; let k = 1 , l ; for ( l = 0 ; l < Math . floor ( N / 4 ) ; l ++ ) { document . write ( k + " " + ( k + 1 ) + " " + N + " " ) ; document . write ( k + 1 + " " + N + " " + ( N - 1 ) + " " ) ; k = k + 2 ; N = N - 2 ; } } else document . write ( " " ) ; } let N = 5 ; let arr = [ 5 , 4 , 3 , 2 , 1 ] ; sortarray ( arr , N ) ;
Find least non | Javascript program to find the least non - overlapping number from a given set intervals ; function to find the smallest non - overlapping number ; create a visited array ; find the first missing value ; Driver function
var MAX = 100005 ; function find_missing ( interval ) { var vis = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < interval . length ; ++ i ) { var start = interval [ i ] [ 0 ] ; var end = interval [ i ] [ 1 ] ; vis [ start ] ++ ; vis [ end + 1 ] -- ; } for ( var i = 1 ; i < MAX ; i ++ ) { vis [ i ] += vis [ i - 1 ] ; if ( ! vis [ i ] ) { document . write ( i + " " ) ; return ; } } } var interval = [ [ 0 , 14 ] , [ 86 , 108 ] , [ 22 , 30 ] , [ 5 , 17 ] ] ; find_missing ( interval ) ;
Find least non | Function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; Check if any missing value exist ; Finally print the missing value ; Driver code
function findMissing ( interval ) { interval . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] ; } ) ; let mx = 0 ; for ( let i = 0 ; i < interval . length ; ++ i ) { if ( interval [ i ] [ 0 ] > mx ) { document . write ( mx + " " ) ; return ; } else mx = Math . max ( mx , interval [ i ] [ 1 ] + 1 ) ; } document . write ( mx ) ; } let interval = [ [ 0 , 14 ] , [ 86 , 108 ] , [ 22 , 30 ] , [ 5 , 17 ] ] ; findMissing ( interval ) ;
Maximum bitwise OR value of subsequence of length K | Function to convert bit array to decimal number ; Return the final result ; Function to find the maximum Bitwise OR value of subsequence of length K ; Initialize bit array of size 32 with all value as 0 ; Iterate for each index of bit [ ] array from 31 to 0 , and check if the ith value of bit array is 0 ; Check for maximum contributing element ; Update the bit array if max_contributing element is found ; Decrement the value of K ; Return the result ; Given array arr [ ] ; Length of subsequence ; Function Call
function build_num ( bit ) { let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } function maximumOR ( arr , n , k ) { let bit = new Array ( 32 ) ; bit . fill ( 0 ) ; for ( let i = 31 ; i >= 0 ; i -- ) { if ( bit [ i ] == 0 && k > 0 ) { let temp = build_num ( bit ) ; let temp1 = temp ; let val = - 1 ; for ( let j = 0 ; j < n ; j ++ ) { if ( temp1 < ( temp arr [ j ] ) ) { temp1 = temp | arr [ j ] ; val = arr [ j ] ; } } if ( val != - 1 ) { k -- ; for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( val & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } } } return build_num ( bit ) ; } let arr = [ 5 , 9 , 7 , 19 ] ; let k = 3 ; let n = arr . length ; document . write ( maximumOR ( arr , n , k ) ) ;
Minimize number of boxes by putting small box inside bigger one | Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; Check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code
function minBox ( arr , n ) { var box = n ; arr . sort ( ) ; var curr_box = 0 , next_box = 1 ; while ( curr_box < n && next_box < n ) { if ( arr [ curr_box ] < arr [ next_box ] ) { box -- ; curr_box ++ ; next_box ++ ; } else if ( arr [ curr_box ] == arr [ next_box ] ) next_box ++ ; } document . write ( box ) ; } var size = [ 1 , 2 , 3 ] ; var n = size . length ; minBox ( size , n ) ;
Merge two unsorted linked lists to get a sorted list | javascript program for the above approach ; Create structure for a node ; Function to print the linked list ; Store the head of the linked list into a temporary node and iterate ; Function takes the head of the LinkedList and the data as argument and if no LinkedList exists , it creates one with the head pointing to first node . If it exists already , it appends given node at end of the last node ; Create a new node ; Insert data into the temporary node and povar it 's next to null ; Check if head is null , create a linked list with temp as head and tail of the list ; Else insert the temporary node after the tail of the existing node and make the temporary node as the tail of the linked list ; Return the list ; Function to concatenate the two lists ; Iterate through the head1 to find the last node join the next of last node of head1 to the 1 st node of head2 ; return the concatenated lists as a single list - head1 ; Sort the linked list using bubble sort ; Compares two adjacent elements and swaps if the first element is greater than the other one . ; Given Linked List 1 ; Given Linked List 2 ; Merge the two lists in a single list ; Sort the unsorted merged list ; Print the final sorted merged list
var head1 = null ; var head2 = null ; class node { constructor ( ) { this . data = 0 ; this . next = null ; } } function setData ( head ) { var tmp ; tmp = head ; while ( tmp != null ) { document . write ( tmp . data + " " ) ; tmp = tmp . next ; } } function getData ( head , num ) { temp = new node ( ) ; var tail = head ; temp . data = num ; temp . next = null ; if ( head == null ) { head = temp ; tail = temp ; } else { while ( tail != null ) { if ( tail . next == null ) { tail . next = temp ; tail = tail . next ; } tail = tail . next ; } } return head ; } function mergelists ( ) { tail = head1 ; while ( tail != null ) { if ( tail . next == null && head2 != null ) { tail . next = head2 ; break ; } tail = tail . next ; } return head1 ; } function sortlist ( ) { curr = head1 ; temp = head1 ; while ( curr . next != null ) { temp = curr . next ; while ( temp != null ) { if ( temp . data < curr . data ) { var t = temp . data ; temp . data = curr . data ; curr . data = t ; } temp = temp . next ; } curr = curr . next ; } } head1 = getData ( head1 , 4 ) ; head1 = getData ( head1 , 7 ) ; head1 = getData ( head1 , 5 ) ; head2 = getData ( head2 , 2 ) ; head2 = getData ( head2 , 1 ) ; head2 = getData ( head2 , 8 ) ; head2 = getData ( head2 , 1 ) ; head1 = mergelists ( ) ; sortlist ( ) ; setData ( head1 ) ;
Find K elements whose absolute difference with median of array is maximum | Function for calculating median ; Check for even case ; Function to find the K maximum absolute difference with the median of the array ; Sort the array . ; Store median ; Find and store difference ; If diff [ i ] is greater print it Else print diff [ j ] ; Driver Code
function findMedian ( a , n ) { if ( n % 2 != 0 ) return a [ Math . floor ( n / 2 ) ] ; return ( a [ Math . floor ( ( n - 1 ) / 2 ) ] + a [ Math . floor ( n / 2 ) ] ) / 2.0 ; } function kStrongest ( arr , n , k ) { arr . sort ( ) ; let median = findMedian ( arr , n ) ; let diff = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { diff [ i ] = Math . abs ( median - arr [ i ] ) ; } let i = 0 , j = n - 1 ; while ( k > 0 ) { if ( diff [ i ] > diff [ j ] ) { document . write ( arr [ i ] + " " ) ; i ++ ; } else { document . write ( arr [ j ] + " " ) ; j -- ; } k -- ; } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let k = 3 ; let n = arr . length ; kStrongest ( arr , n , k ) ;
Sort an array by swapping elements of different type specified by another array | Function to check if it is possible to sort the array in non - decreasing order by swapping elements of different types ; Consider the array to be already sorted ; Checking if array is already sorted ; Check for a pair which is in decreasing order ; Count the frequency of each type of element ; type0 stores count of elements of type 0 ; type1 stores count of elements of type 1 ; Return true if array is already sorted ; Return false if all elements are of same type and array is unsorted ; Possible for all other cases ; Driver code
function sorting_possible ( a , b , n ) { let sorted = true ; let type1 = 0 , type0 = 0 , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { sorted = false ; break ; } } for ( i = 0 ; i < n ; i ++ ) { if ( b [ i ] == 0 ) type0 ++ ; else type1 ++ ; } if ( sorted ) return true ; else if ( type1 == n type0 == n ) return false ; else return true ; } let a = [ 15 , 1 , 2 , 17 , 6 ] ; let b = [ 1 , 1 , 0 , 1 , 1 ] ; let n = a . length ; let res = sorting_possible ( a , b , n ) ; if ( res ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize array sum by X increments when each element is divided by 10 | Javascript program for the above approach ; Initialize variables ; Add the current contribution of the element to the answer ; If the value is already maximum then we can 't change it ; Moves required to move to the next multiple of 10 ; No of times we can add 10 to this value so that its value does not exceed 1000. ; Sort the array ; Adding the values to increase the numbers to the next multiple of 10 ; If the total moves are less than X then increase the answer ; If the moves exceed X then we cannot increase numbers ; If there still remain some moves ; Remaining moves ; Add minimim of increments and remaining / 10 to the answer ; Output the final answer ; Driver code
function maximizeval10 ( a , n , k ) { let increments = 0 ; let ans = 0 ; let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { ans += Math . floor ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . push ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( Math . floor ( a [ i ] ) / 10 ) - 1 ) ; } } v . sort ( function ( a , b ) { return a - b ; } ) ; let sum = 0 ; for ( let i = 0 ; i < v . length ; i ++ ) { sum += v [ i ] ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { let remaining = k - sum ; ans += Math . min ( increments , Math . floor ( remaining / 10 ) ) ; } document . write ( ans ) ; } let N = 4 ; let X = 6 ; let A = [ 4 , 8 , 8 , 8 ] ; maximizeval10 ( A , N , X ) ;
Split array into two subarrays such that difference of their maximum is minimum | javascript Program to split a given array such that the difference between their maximums is minimized . ; Sort the array ; Return the difference between two highest elements ; Driver code
function findMinDif ( arr , N ) { arr . sort ( ( a , b ) => a - b ) ; return ( arr [ N - 1 ] - arr [ N - 2 ] ) ; } var arr = [ 7 , 9 , 5 , 10 ] ; var N = arr . length ; document . write ( findMinDif ( arr , N ) ) ;