codeparrot/xlcost-text-to-code huggingface dataset:
Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | Function to maximize the count of elements required to be removed from arr [ ] such that the sum of arr [ ] is greater than or equal to sum of the array brr [ ] ; Sort the array arr [ ] ; Stores index of smallest element of arr [ ] ; Stores sum of elements of the array arr [ ] ; Traverse the array arr [ ] ; Update sumArr ; Stores sum of elements of the array brr [ ] ; Traverse the array brr [ ] ; Update sumArr ; Stores count of removed elements ; Repeatedly remove the smallest element of arr [ ] ; Update sumArr ; Remove the smallest element ; If the sum of remaining elements in arr [ ] >= sum of brr [ ] ; Update cntRemElem ; Driver Code
function maxCntRemovedfromArray ( arr , N , brr , M ) { arr . sort ( ) ; let i = 0 ; let sumArr = 0 ; for ( i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } let sumBrr = 0 ; for ( i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } let cntRemElem = 0 ; while ( i < N && sumArr >= sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } let arr = [ 1 , 2 , 4 , 6 ] ; let brr = [ 7 ] ; let N = arr . length ; let M = brr . length ; document . write ( maxCntRemovedfromArray ( arr , N , brr , M ) ) ;
Find any two pairs ( a , b ) and ( c , d ) such that a d | Javascript implementation to sort the array of points by its distance from the given point ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function findPair ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let a = arr [ i ] . first , b = arr [ i ] . second ; for ( let j = i + 1 ; j < N ; j ++ ) { let c = arr [ j ] . first , d = arr [ j ] . second ; if ( a < c && b > d ) { document . write ( " " + a + " " + b + " " + c + " " + d + " " ) ; return ; } } } document . write ( " " ) ; } let arr = [ new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) ] ; findPair ( arr , 5 ) ;
Find any two pairs ( a , b ) and ( c , d ) such that a d | Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code
function findPair ( arr , N ) { arr . sort ( function ( a , b ) { return ( a [ 0 ] - b [ 0 ] ) || ( a [ 1 ] - b [ 1 ] ) ; } ) ; for ( let i = 1 ; i < N ; i ++ ) { let b = arr [ i - 1 ] [ 1 ] ; let d = arr [ i ] [ 1 ] ; if ( b > d ) { console . log ( " " + arr [ i - 1 ] [ 0 ] + " " + b + " " + arr [ i ] [ 0 ] + " " + d + " " ) ; return ; } } console . log ( " " ) ; } var arr = [ [ 3 , 7 ] , [ 21 , 23 ] , [ 4 , 13 ] , [ 1 , 2 ] , [ 7 , - 1 ] ] findPair ( arr , 5 ) ;
Print indices in non | Function to print the order of array elements generating non - decreasing quotient after division by X ; Stores the quotient and the order ; Traverse the array ; Sort the vector ; Print the order ; Driver Code
function printOrder ( order , N , X ) { let vect = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( order [ i ] % X == 0 ) { vect . push ( [ order [ i ] / X , i + 1 ] ) ; } else { vect . push ( [ order [ i ] / X + 1 , i + 1 ] ) ; } } vect . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; for ( let i = 0 ; i < N ; i ++ ) { document . write ( vect [ i ] [ 1 ] + " " ) ; } document . write ( ) ; } let N = 3 , X = 3 ; let order = [ 2 , 7 , 4 ] ; printOrder ( order , N , X ) ;
Shuffle 2 n integers as a1 | Function to reverse the array from the position ' start ' to position ' end ' ; Stores mid of start and end ; Traverse the array in the range [ start , end ] ; Stores arr [ start + i ] ; Update arr [ start + i ] ; Update arr [ end - i ] ; Utility function to shuffle the given array in the of form { a1 , b1 , a2 , b2 , ... . an , bn } ; Stores the length of the array ; If length of the array is 2 ; Stores mid of the { start , end } ; Divide array into two halves of even length ; Update mid ; Calculate the mid - points of both halves of the array ; Reverse the subarray made from mid1 to mid2 ; Reverse the subarray made from mid1 to mid ; Reverse the subarray made from mid to mid2 ; Recursively calls for both the halves of the array ; Function to shuffle the given array in the form of { a1 , b1 , a2 , b2 , ... . an , bn } ; Function Call ; Print the modified array ; Given array ; Size of the array ; Shuffles the given array to the required permutation
function reverse ( arr , start , end ) { let mid = ( end - start + 1 ) / 2 ; for ( let i = 0 ; i < mid ; i ++ ) { let temp = arr [ start + i ] ; arr [ start + i ] = arr [ end - i ] ; arr [ end - i ] = temp ; } return ; } function shuffleArrayUtil ( arr , start , end ) { let i ; let l = end - start + 1 ; if ( l == 2 ) return ; let mid = start + l / 2 ; if ( l % 4 > 0 ) { mid -= 1 ; } let mid1 = start + ( mid - start ) / 2 ; let mid2 = mid + ( end + 1 - mid ) / 2 ; reverse ( arr , mid1 , mid2 - 1 ) ; reverse ( arr , mid1 , mid - 1 ) ; reverse ( arr , mid , mid2 - 1 ) ; shuffleArrayUtil ( arr , start , mid - 1 ) ; shuffleArrayUtil ( arr , mid , end ) ; } function shuffleArray ( arr , N , start , end ) { shuffleArrayUtil ( arr , start , end ) ; for ( let i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 3 , 5 , 2 , 4 , 6 ] ; let N = arr . length ; shuffleArray ( arr , N , 0 , N - 1 ) ;
Reduce sum of same | Function to check if elements of B [ ] can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A [ ] in ascending order ; Sort B [ ] in descending order ; Traverse the arrays A [ ] and B [ ] ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call
function rearrange ( A , B , N , X ) { let flag = true ; A . sort ( ) ; B . sort ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ N - 1 - i ] > X ) { flag = false ; break ; } } if ( flag == true ) document . write ( " " ) ; else document . write ( " " ) ; } let A = [ 1 , 2 , 3 ] ; let B = [ 1 , 1 , 2 ] ; let X = 4 ; let N = A . length ; rearrange ( A , B , N , X ) ;
Minimum increments or decrements by D required to make all array elements equal | Function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Sort the array ; Traverse the array ; If difference between two consecutive elements are not divisible by D ; Store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Stores middle element of the array ; Traverse the array ; Update count ; Given N & D ; Given array arr [ ] ; Function Call
function numOperation ( arr , N , D ) { arr . sort ( ) ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( ( arr [ i + 1 ] - arr [ i ] ) % D != 0 ) { document . write ( " " ) ; return ; } } let count = 0 ; let mid = arr [ N / 2 ] ; for ( let i = 0 ; i < N ; i ++ ) { count += Math . abs ( mid - arr [ i ] ) / D ; } document . write ( count ) ; } let N = 4 , D = 2 ; let arr = [ 2 , 4 , 6 , 8 ] ; numOperation ( arr , N , D ) ;
Maximize sum of second minimums of each K length partitions of the array | Function to find the maximum sum of second smallest of each partition of size K ; Sort the array A [ ] in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Given size of partitions ; Given array A [ ] ; Size of the given array ; Function Call
function findSum ( A , N , K ) { A . sort ( ) ; let sum = 0 ; for ( let i = N / K ; i < N ; i += K - 1 ) { sum += A [ i ] ; } document . write ( sum ) ; } let K = 4 ; let A = [ 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 ] ; let N = A . length ; findSum ( A , N , K ) ;
Minimize remaining array element by removing pairs and replacing them with their average | Function to find the smallest element left in the array by removing pairs and inserting their average ; Stores array elements such that the largest element present at top of PQ ; Traverse the array ; Insert arr [ i ] into PQ ; Iterate over elements of PQ while count of elements in PQ greater than 1 ; Stores largest element of PQ ; Pop the largest element of PQ ; Stores largest element of PQ ; Pop the largest element of PQ ; Insert the ceil value of average of top1 and top2 ; Driver Code
function findSmallestNumLeft ( arr , N ) { let PQ = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { PQ . push ( arr [ i ] ) ; } PQ . sort ( function ( a , b ) { return b - a ; } ) ; while ( PQ . length > 1 ) { let top1 = PQ [ 0 ] ; PQ . shift ( ) ; let top2 = PQ [ 0 ] ; PQ . shift ( ) ; PQ . push ( Math . floor ( ( top1 + top2 + 1 ) / 2 ) ) ; PQ . sort ( function ( a , b ) { return b - a ; } ) ; } return PQ [ 0 ] ; } let arr = [ 30 , 16 , 40 ] ; let N = arr . length ; document . write ( findSmallestNumLeft ( arr , N ) ) ;
Find the array element from indices not divisible by K having largest composite product of digits | Javascript program to implement the above approach ; Function to check if a number is a composite number or not ; Corner cases ; Check if number is divisible by 2 or 3 ; Check if number is a multiple of any other prime number ; Function to calculate the product of digits of a number ; Stores the product of digits ; Extract digits of a number ; Calculate product of digits ; Function to check if the product of digits of a number is a composite number or not ; Stores product of digits ; If product of digits is equal to 1 ; If product of digits is not prime ; Function to find the number with largest composite product of digits from the indices not divisible by k from the given array ; Traverse the array ; If index is divisible by k ; Check if product of digits is a composite number or not ; Sort the products ; Driver Code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function isComposite ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function digitProduct ( number ) { let product = 1 ; while ( number > 0 ) { product *= ( number % 10 ) ; number = Math . floor ( number / 10 ) ; } return product ; } function compositedigitProduct ( num ) { let res = digitProduct ( num ) ; if ( res == 1 ) { return false ; } if ( isComposite ( res ) ) { return true ; } return false ; } function largestCompositeDigitProduct ( a , n , k ) { let pq = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( i % k ) == 0 ) { continue ; } if ( compositedigitProduct ( a [ i ] ) ) { let b = digitProduct ( a [ i ] ) ; pq . push ( new pair ( b , a [ i ] ) ) ; } } pq . sort ( function ( x , y ) { return x . first - y . first } ) ; return pq [ pq . length - 1 ] . second ; } let arr = [ 233 , 144 , 89 , 71 , 13 , 21 , 11 , 34 , 55 , 23 ] ; let n = arr . length ; let k = 3 ; let ans = largestCompositeDigitProduct ( arr , n , k ) ; document . write ( ans + " " ) ;
Split array into K | Function to find the minimum sum of 2 nd smallest elements of each subset ; Sort the array ; Stores minimum sum of second elements of each subset ; Traverse first K 2 nd smallest elements ; Update their sum ; Prvar the sum ; Given Array ; Given length of array ; Given subset lengths
function findMinimum ( arr , N , K ) { arr . sort ( ( a , b ) => a - b ) ; var ans = 0 ; for ( i = 1 ; i < 2 * ( parseInt ( N / K ) ) ; i += 2 ) { ans += arr [ i ] ; } document . write ( ans ) ; } var arr = [ 11 , 20 , 5 , 7 , 8 , 14 , 2 , 17 , 16 , 10 ] ; var N = arr . length ; var K = 5 ; findMinimum ( arr , N , K ) ;
Find two non | Function to check if two non - intersecting subarrays with equal sum exists or not ; Sort the given array ; Traverse the array ; Check for duplicate elements ; If no duplicate element is present in the array ; Given array ; Size of array
function findSubarrays ( arr , N ) { arr . sort ( ) ; var i = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var arr = [ 4 , 3 , 0 , 1 , 2 , 0 ] ; var N = arr . length ; findSubarrays ( arr , N ) ;
Selection Sort VS Bubble Sort | Function for bubble sort ; Iterate from 1 to n - 1 ; Iterate from 0 to n - i - 1 ; Driver Code
function Bubble_Sort ( arr , n ) { Boolean ( flag = true ) ; for ( var i = 1 ; i < n ; ++ i ) { flag = false ; for ( var j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { var temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; flag = true ; } } if ( flag == false ) break ; } } var n = 5 ; var arr = [ 2 , 0 , 1 , 4 , 3 ] ; Bubble_Sort ( arr , n ) ; document . write ( " " + " " ) ; for ( var i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;
Print all array elements appearing more than N / K times | Function to print all array elements whose frequency is greater than N / K ; Sort the array , arr [ ] ; Traverse the array ; Stores frequency of arr [ i ] ; Traverse array elements which is equal to arr [ i ] ; Update cnt ; Update i ; If frequency of arr [ i ] is greater than ( N / K ) ; Driver Code
function NDivKWithFreq ( arr , N , K ) { arr . sort ( ) ; for ( let i = 0 ; i < N ; ) { let cnt = 1 ; while ( ( i + 1 ) < N && arr [ i ] == arr [ i + 1 ] ) { cnt ++ ; i ++ ; } if ( cnt > ( N / K ) ) { document . write ( arr [ i ] + " " ) ; } i ++ ; } } let arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] ; let N = arr . length ; let K = 4 ; NDivKWithFreq ( arr , N , K ) ;
Sort M elements of given circular array starting from index K | Function to print the circular array ; Print the array ; Function to sort m elements of diven circular array starting from index k ; Traverse M elements ; Iterate from index k to k + m - 1 ; Check if the next element in the circular array is less than the current element ; Swap current element with the next element ; Print the circular array ; Driver Code ; Function Call
function printCircularArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function sortCircularArray ( arr , n , k , m ) { for ( let i = 0 ; i < m ; i ++ ) { for ( let j = k ; j < k + m - 1 ; j ++ ) { if ( arr [ j % n ] > arr [ ( j + 1 ) % n ] ) { let t = arr [ j % n ] ; arr [ j % n ] = arr [ ( j + 1 ) % n ] ; arr [ ( j + 1 ) % n ] = t ; } } } printCircularArray ( arr , n ) ; } let arr = [ 4 , 1 , 6 , 5 , 3 ] ; let K = 2 , M = 3 ; let N = arr . length ; sortCircularArray ( arr , N , K , M ) ;
Minimize cost required to complete all processes | Function to find minimum cost required to complete all the process ; Sort the array on descending order of Y ; Stores length of array ; Stores minimum cost required to complete all the process ; Stores minimum cost to initiate any process ; Traverse the array ; If minCostInit is less than the cost to initiate the process ; Update minCost ; Update minCostInit ; Update minCostInit ; Return minCost ; Driver Code ; Function Call
function minimumCostReqToCompthePrcess ( arr ) { arr = arr . map ( row => row ) . reverse ( ) var n = arr . length ; var minCost = 0 ; var minCostInit = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] [ 1 ] > minCostInit ) { minCost += ( arr [ i ] [ 1 ] - minCostInit ) ; minCostInit = arr [ i ] [ 1 ] ; } minCostInit -= arr [ i ] [ 0 ] ; } return minCost ; } var arr = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 4 , 8 ] ] ; document . write ( minimumCostReqToCompthePrcess ( arr ) ) ;
Split array into equal length subsets with maximum sum of Kth largest element of each subset | Function to find the maximum sum of Kth largest element of M equal length partition ; Stores sum of K_th largest element of equal length partitions ; If N is not divisible by M ; Stores length of each partition ; If K is greater than length of partition ; Sort array in descending porder ; Traverse the array ; Update maxSum ; Driver code
function maximumKthLargestsumPart ( arr , N , M , K ) { let maxSum = 0 ; if ( N % M != 0 ) return - 1 ; let sz = ( N / M ) ; if ( K > sz ) return - 1 ; arr . sort ( ) ; let i , k , t ; for ( i = 0 ; i < N / 2 ; i ++ ) { t = arr [ i ] ; arr [ i ] = arr [ N - i - 1 ] ; arr [ N - i - 1 ] = t ; } for ( i = 1 ; i <= M ; i ++ ) { maxSum += arr [ i * K - 1 ] ; } return maxSum ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let M = 2 ; let K = 1 ; let N = arr . length ; document . write ( maximumKthLargestsumPart ( arr , N , M , K ) ) ;
Minimize difference between maximum and minimum array elements by exactly K removals | Function to minimize the difference of the maximum and minimum array elements by removing K elements ; Base Condition ; Sort the array ; Initialize left and right pointers ; Iterate for K times ; Removing right element to reduce the difference ; Removing the left element to reduce the difference ; Print the minimum difference ; Driver Code ; Function Call
function minimumRange ( arr , N , K ) { if ( K >= N ) { document . write ( 0 ) ; return ; } arr . sort ( ( a , b ) => a - b ) ; var left = 0 , right = N - 1 , i ; for ( i = 0 ; i < K ; i ++ ) { if ( arr [ right - 1 ] - arr [ left ] < arr [ right ] - arr [ left + 1 ] ) right -- ; else left ++ ; } document . write ( arr [ right ] - arr [ left ] ) ; } var arr = [ 5 , 10 , 12 , 14 , 21 , 54 , 61 ] ; var N = arr . length ; var K = 4 ; minimumRange ( arr , N , K ) ;
Split array into K subsets to maximize sum of their second largest elements | Function to split array into K subsets having maximum sum of their second maximum elements ; Sort the array ; Stores the maximum possible sum of second maximums ; Add second maximum of current subset ; Proceed to the second maximum of next subset ; Print the maximum sum obtained ; Given array arr [ ] ; Size of array ; Function Call
function splitArray ( arr , n , K ) { arr . sort ( ) ; let i = n - 1 ; let result = 0 ; while ( K -- != 0 ) { result += arr [ i - 1 ] ; i -= 2 ; } document . write ( result ) ; } let arr = [ 1 , 3 , 1 , 5 , 1 , 3 ] ; let N = arr . length ; let K = 2 ; splitArray ( arr , N , K ) ;
Count subsequences for every array element in which they are the maximum | Function to merge the subarrays arr [ l . . m ] and arr [ m + 1 , . . r ] based on indices [ ] ; If a [ indices [ l ] ] is less than a [ indices [ j ] ] , add indice [ l ] to temp ; Else add indices [ j ] ; Add remaining elements ; Add remainging elements ; Recursive function to divide the array leto to parts ; Recursive call for elements before mid ; Recursive call for elements after mid ; Merge the two sorted arrays ; Function to find the number of subsequences for each element ; Sorting the indices according to array arr [ ] ; Array to store output numbers ; Initialize subseq ; B [ i ] is 2 ^ i ; Doubling the subsequences ; Print the final output , array B [ ] ; Given array ; Given length ; Function call
function merge ( indices , a , l , mid , r ) { let temp_ind = [ ] ; let j = mid + 1 ; let i = 0 , temp_l = l , k ; while ( l <= mid && j <= r ) { if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) temp_ind [ i ++ ] = indices [ l ++ ] ; else temp_ind [ i ++ ] = indices [ j ++ ] ; } while ( l <= mid ) temp_ind [ i ++ ] = indices [ l ++ ] ; while ( j <= r ) temp_ind [ i ++ ] = indices [ j ++ ] ; for ( k = 0 ; k < i ; k ++ ) indices [ temp_l ++ ] = temp_ind [ k ] ; } function divide ( indices , a , l , r ) { if ( l >= r ) return ; let mid = l / 2 + r / 2 ; divide ( indices , a , l , mid ) ; divide ( indices , a , mid + 1 , r ) ; merge ( indices , a , l , mid , r ) ; } function noOfSubsequences ( arr , N ) { let indices = [ ] ; let i ; for ( i = 0 ; i < N ; i ++ ) indices [ i ] = i ; divide ( indices , arr , 0 , N - 1 ) ; let B = [ ] ; let subseq = 1 ; for ( i = 0 ; i < N ; i ++ ) { B [ indices [ i ] ] = subseq ; subseq *= 2 ; } for ( i = 0 ; i < N ; i ++ ) document . write ( B [ i ] + " " ) ; } let arr = [ 2 , 3 , 1 ] ; let N = arr . length ; noOfSubsequences ( arr , N ) ;
Find the order of execution of given N processes in Round Robin Scheduling | Function to sort the array order [ ] on the basis of the array freq [ ] ; If order [ i ] th is less than order [ temp ] th process ; Otherwise ; Add the left half to tempOrder [ ] ; Add right half to tempOrder [ ] ; Copy the tempOrder [ ] array to order [ ] array ; Utility function to sort the array order [ ] on the basis of freq [ ] ; Base Case ; Divide array into 2 parts ; Sort the left array ; Sort the right array ; Merge the sorted arrays ; Function to find the order of processes in which execution occurs ; Store the frequency ; Find elements in array freq [ ] ; Store the order of completion of processes ; Initialize order [ i ] as i ; Function Call to find the order of execution ; Print order of completion of processes ; Burst Time of the processes ; Quantum Time ; Function Call
function merge ( order , freq , i , mid , j ) { var tempOrder = Array ( j - i + 1 ) ; var temp = mid + 1 , index = - 1 ; while ( i <= mid && temp <= j ) { if ( freq [ order [ i ] ] <= freq [ order [ temp ] ] ) { tempOrder [ ++ index ] = order [ i ++ ] ; } else { tempOrder [ ++ index ] = order [ temp ++ ] ; } } while ( i <= mid ) { tempOrder [ ++ index ] = order [ i ++ ] ; } while ( temp <= j ) { tempOrder [ ++ index ] = order [ temp ++ ] ; } for ( index ; index >= 0 ; index -- ) { order [ j -- ] = tempOrder [ index ] ; } } function divide ( order , freq , i , j ) { if ( i >= j ) return ; var mid = parseInt ( i / 2 ) + parseInt ( j / 2 ) ; divide ( order , freq , i , mid ) ; divide ( order , freq , mid + 1 , j ) ; merge ( order , freq , i , mid , j ) ; } function orderProcesses ( A , N , q ) { var i = 0 ; var freq = Array ( N ) ; for ( i = 0 ; i < N ; i ++ ) { freq [ i ] = ( A [ i ] / q ) + ( A [ i ] % q > 0 ) ; } var order = Array ( 4 ) ; for ( i = 0 ; i < N ; i ++ ) { order [ i ] = i ; } divide ( order , freq , 0 , N - 1 ) ; for ( i = 0 ; i < N ; i ++ ) { document . write ( order [ i ] + " " ) ; } } var arr = [ 3 , 7 , 4 ] ; var Q = 3 ; var N = arr . length ; orderProcesses ( arr , N , Q ) ;
Maximize cost to empty given array by repetitively removing K array elements | Function to find the maximum cost to remove all array elements ; Stores maximum cost to remove array elements ; Sort array in ascending order ; Traverse the array ; Update maxCost ; Driver code
function maxCostToRemove ( arr , N , K ) { let maxCost = 0 ; arr . sort ( ) ; for ( let i = 0 ; i < N ; i += K ) { maxCost += arr [ i + 1 ] ; } return maxCost ; } let arr = [ 1 , 3 , 4 , 1 , 5 , 1 , 5 , 3 ] ; let N = arr . length ; let K = 4 ; document . write ( maxCostToRemove ( arr , N , K ) ) ;