codeparrot/xlcost-text-to-code huggingface dataset:
Prim 's Algorithm (Simple Implementation for Adjacency Matrix Representation) | JavaScript implementation to find minimum spanning tree for adjacency representation . ; Returns true if edge u - v is a valid edge to be include in MST . An edge is valid if one end is already included in MST and other is not in MST . ; Include first vertex in MST ; Keep adding edges while number of included edges does not become V - 1. ; Find minimum weight valid edge . ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution
let V = 5 ; let let_MAX = Number . MAX_VALUE ; function isValidEdge ( u , v , inMST ) { if ( u == v ) return false ; if ( inMST [ u ] == false && inMST [ v ] == false ) return false ; else if ( inMST [ u ] == true && inMST [ v ] == true ) return false ; return true ; } function primMST ( cost ) { let inMST = Array ( V ) . fill ( false ) ; inMST [ 0 ] = true ; let edge_count = 0 , mincost = 0 ; while ( edge_count < V - 1 ) { let min = let_MAX , a = - 1 , b = - 1 ; for ( let i = 0 ; i < V ; i ++ ) { for ( let j = 0 ; j < V ; j ++ ) { if ( cost [ i ] [ j ] < min ) { if ( isValidEdge ( i , j , inMST ) ) { min = cost [ i ] [ j ] ; a = i ; b = j ; } } } } if ( a != - 1 && b != - 1 ) { document . write ( " " + edge_count ++ + " " + a + " " + b + " " + " " + min + " " ) ; mincost = mincost + min ; inMST [ b ] = inMST [ a ] = true ; } } document . write ( " " ) ; document . write ( " " ) ; document . write ( " " + mincost ) ; } let cost = [ [ let_MAX , 2 , let_MAX , 6 , let_MAX ] , [ 2 , let_MAX , 3 , 8 , 5 ] , [ let_MAX , 3 , let_MAX , let_MAX , 7 ] , [ 6 , 8 , let_MAX , let_MAX , 9 ] , [ let_MAX , 5 , 7 , 9 , let_MAX ] ] ; primMST ( cost ) ;
Length and Breadth of rectangle such that ratio of Area to diagonal ^ 2 is maximum | function to print length and breadth ; sort the input array ; create array vector of integers occurring in pairs ; push the same pairs ; calculate length and breadth as per requirement ; check for given condition ; print the required answer ; Driver Code
function findLandB ( arr , n ) { arr . sort ( ) ; var arr_pairs = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { arr_pairs . push ( arr [ i ] ) ; i ++ ; } } var length = arr_pairs [ 0 ] ; var breadth = arr_pairs [ 1 ] ; var size = arr_pairs . length ; for ( var i = 2 ; i < size ; i ++ ) { if ( ( length / breadth + breadth / length ) > ( arr_pairs [ i ] / arr_pairs [ i - 1 ] + arr_pairs [ i - 1 ] / arr_pairs [ i ] ) ) { length = arr_pairs [ i ] ; breadth = arr_pairs [ i - 1 ] ; } } document . write ( length + " " + breadth ) ; } var arr = [ 4 , 2 , 2 , 2 , 5 , 6 , 5 , 6 , 7 , 2 ] ; var n = arr . length ; findLandB ( arr , n ) ;
Smallest sum contiguous subarray | Set | function to find the smallest sum contiguous subarray ; First invert the sign of the elements ; Apply the normal Kadane algorithm But on the elements Of the array having inverted sign ; Invert the answer to get minimum val ; driver code
function smallestSumSubarr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = - arr [ i ] ; let sum_here = arr [ 0 ] , max_sum = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { sum_here = Math . max ( sum_here + arr [ i ] , arr [ i ] ) ; max_sum = Math . max ( max_sum , sum_here ) ; } return ( - 1 ) * max_sum ; } let arr = [ 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 ] ; let n = arr . length ; document . write ( " " + smallestSumSubarr ( arr , n ) ) ;
Find k pairs with smallest sums in two arrays | Set 2 | Function to print the K smallest pairs ; if k is greater than total pairs ; _pair _one keeps track of ' first ' in a1 and ' second ' in a2 in _two , _two [ 0 ] keeps track of element in the a2 [ ] and _two [ 1 ] in a1 [ ] ; Repeat the above process till all K pairs are printed ; when both the pointers are pointing to the same elements ( point 3 ) ; updates according to step 1 ; see point 2 ; updates opposite to step 1 ; updates according to rule 1 ; see point 2 ; updates opposite to rule 1 ; see point 2 ; else update as necessary ( point 1 ) ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; updating according to rule 1 ; see point 2 ; Driver Code
function printKPairs ( a1 , a2 , size1 , size2 , k ) { if ( k > ( size2 * size1 ) ) { document . write ( " " ) ; return ; } let _one = [ 0 , 0 ] ; let _two = [ 0 , 0 ] ; let cnt = 0 ; while ( cnt < k ) { if ( _one [ 0 ] == _two [ 1 ] && _two [ 0 ] == _one [ 1 ] ) { if ( a1 [ _one [ 0 ] ] < a2 [ _one [ 1 ] ] ) { document . write ( " " + a1 [ _one [ 0 ] ] + " " + a2 [ _one [ 1 ] ] + " " ) ; _one [ 1 ] = ( _one [ 1 ] + 1 ) % size2 ; if ( _one [ 1 ] == 0 ) _one [ 0 ] = ( _one [ 0 ] + 1 ) % size1 ; _two [ 1 ] = ( _two [ 1 ] + 1 ) % size2 ; if ( _two [ 1 ] == 0 ) _two [ 0 ] = ( _two [ 0 ] + 1 ) % size2 ; } else { document . write ( " " + a2 [ _one [ 1 ] ] + " " + a1 [ _one [ 0 ] ] + " " ) ; _one [ 0 ] = ( _one [ 0 ] + 1 ) % size1 ; if ( _one [ 0 ] == 0 ) _one [ 1 ] = ( _one [ 1 ] + 1 ) % size2 ; _two [ 0 ] = ( _two [ 0 ] + 1 ) % size2 ; if ( _two [ 0 ] == 0 ) _two [ 1 ] = ( _two [ 1 ] + 1 ) % size1 ; } } else if ( a1 [ _one [ 0 ] ] + a2 [ _one [ 1 ] ] <= a2 [ _two [ 0 ] ] + a1 [ _two [ 1 ] ] ) { if ( a1 [ _one [ 0 ] ] < a2 [ _one [ 1 ] ] ) { document . write ( " " + a1 [ _one [ 0 ] ] + " " + a2 [ _one [ 1 ] ] + " " ) ; _one [ 1 ] = ( ( _one [ 1 ] + 1 ) % size2 ) ; if ( _one [ 1 ] == 0 ) _one [ 0 ] = ( _one [ 0 ] + 1 ) % size1 ; } else { document . write ( " " + a2 [ _one [ 1 ] ] + " " + a1 [ _one [ 0 ] ] + " " ) ; _one [ 0 ] = ( ( _one [ 0 ] + 1 ) % size1 ) ; if ( _one [ 0 ] == 0 ) _one [ 1 ] = ( _one [ 1 ] + 1 ) % size2 ; } } else if ( a1 [ _one [ 0 ] ] + a2 [ _one [ 1 ] ] > a2 [ _two [ 0 ] ] + a1 [ _two [ 1 ] ] ) { if ( a2 [ _two [ 0 ] ] < a1 [ _two [ 1 ] ] ) { document . write ( " " + a2 [ _two [ 0 ] ] + " " + a1 [ _two [ 1 ] ] + " " ) ; _two [ 0 ] = ( ( _two [ 0 ] + 1 ) % size2 ) ; if ( _two [ 0 ] == 0 ) _two [ 1 ] = ( _two [ 1 ] + 1 ) % size1 ; } else { document . write ( " " + a1 [ _two [ 1 ] ] + " " + a2 [ _two [ 0 ] ] + " " ) ; _two [ 1 ] = ( ( _two [ 1 ] + 1 ) % size1 ) ; if ( _two [ 1 ] == 0 ) _two [ 0 ] = ( _two [ 0 ] + 1 ) % size1 ; } } cnt ++ ; } } let a1 = [ 2 , 3 , 4 ] ; let a2 = [ 1 , 6 , 5 , 8 ] ; let size1 = a1 . length ; let size2 = a2 . length ; let k = 4 ; printKPairs ( a1 , a2 , size1 , size2 , k ) ;
Maximum number by concatenating every element in a rotation of an array | Function to prlet the largest number ; store the index of largest left most digit of elements ; Iterate for all numbers ; check for the last digit ; check for the largest left most digit ; print the rotation of array ; print the rotation of array ; driver code
function printLargest ( a , n ) { let max = - 1 ; let ind = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let num = a [ i ] ; while ( num > 0 ) { let r = num % 10 ; num = Math . floor ( num / 10 ) ; if ( num == 0 ) { if ( max < r ) { max = r ; ind = i ; } } } } for ( let i = ind ; i < n ; i ++ ) document . write ( a [ i ] ) ; for ( let i = 0 ; i < ind ; i ++ ) document . write ( a [ i ] ) ; } let a = [ 54 , 546 , 548 , 60 ] ; let n = a . length ; printLargest ( a , n ) ;
Minimum number of operations to convert a given sequence into a Geometric Progression | Javascript program to find minimum number of operations to convert a given sequence to an Geometric Progression ; Function to print the GP series ; Check for possibility ; Function for getting the Arithmetic Progression ; The array c describes all the given set of possible operations . ; Size of c ; candidate answer ; Loop through all the permutations of the first two elements . ; a1 and a2 are the candidate first two elements of the possible GP . ; temp stores the current answer , including the modification of the first two elements . ; Common ratio of the possible GP ; To check if the chosen set is valid , and id yes find the number of operations it takes . ; ai is value of a [ i ] according to the assumed first two elements a1 , a2 ith element of an GP = a1 * ( ( a2 - a1 ) ^ ( i - 1 ) ) ; Check for the " proposed " element to be only differing by one ; Set the temporary ans ; To infinity and break ; Update answer ; Calling function to print the sequence ; Array is 1 - indexed , with a [ 0 ] = 0 for the sake of simplicity ; Function to print the minimum operations and the sequence of elements
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function construct ( n , ans_pair ) { if ( ans_pair . first == - 1 ) { document . write ( " " ) ; return ; } var a1 = ans_pair . first ; var a2 = ans_pair . second ; var r = a2 / a1 ; document . write ( " " ) ; for ( var i = 1 ; i <= n ; i ++ ) { var ai = parseInt ( a1 * Math . pow ( r , i - 1 ) ) ; document . write ( ai + " " ) ; } } function findMinimumOperations ( a , n ) { var ans = 1000000000 ; var c = [ - 1 , 0 , 1 ] ; var possibilities = 3 ; var pos1 = - 1 , pos2 = - 1 ; for ( var i = 0 ; i < possibilities ; i ++ ) { for ( var j = 0 ; j < possibilities ; j ++ ) { var a1 = a [ 1 ] + c [ i ] ; var a2 = a [ 2 ] + c [ j ] ; var temp = ( Math . abs ( a1 - a [ 1 ] ) + Math . abs ( a2 - a [ 2 ] ) ) ; if ( a1 == 0 a2 == 0 ) continue ; var r = a2 / a1 ; for ( var pos = 3 ; pos <= n ; pos ++ ) { var ai = a1 * Math . pow ( r , pos - 1 ) ; if ( a [ pos ] == ai ) { continue ; } else if ( a [ pos ] + 1 == ai a [ pos ] - 1 == ai ) { temp ++ ; } else { temp = 1000000000 ; break ; } } if ( temp < ans ) { ans = temp ; pos1 = a1 ; pos2 = a2 ; } } } if ( ans == - 1 ) { document . write ( " " ) ; return ; } document . write ( " " + " " + ans + " " ) ; var ans_pair = new pair ( pos1 , pos2 ) ; construct ( n , ans_pair ) ; } var a = [ 0 , 7 , 20 , 49 , 125 ] ; var n = a . length ; findMinimumOperations ( a , n - 1 ) ;
Optimal Storage on Tapes | This functions outputs the required order and Minimum Retrieval Time ; Here length of i 'th program is L[i] ; Lengths of programs sorted according to increasing lengths . This is the order in which the programs have to be stored on tape for minimum MRT ; MRT - Minimum Retrieval Time ; driver code
function findOrderMRT ( L , n ) { L . sort ( ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( L [ i ] + " " ) ; document . write ( " " ) ; let MRT = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let sum = 0 ; for ( let j = 0 ; j <= i ; j ++ ) sum += L [ j ] ; MRT += sum ; } MRT /= n ; document . write ( " " + " " + MRT ) ; } let L = [ 2 , 5 , 4 ] ; let n = L . length ; findOrderMRT ( L , n ) ;
Minimum operations to make GCD of array a multiple of k | Javascript program to make GCD of array a multiple of k . ; If array value is not 1 and it is greater than k then we can increase the or decrease the remainder obtained by dividing k from the ith value of array so that we get the number which is either closer to k or its multiple ; Else we only have one choice which is to increment the value to make equal to k ; Driver code
function MinOperation ( a , n , k ) { let result = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( a [ i ] != 1 && a [ i ] > k ) { result = result + Math . min ( a [ i ] % k , k - a [ i ] % k ) ; } else { result = result + k - a [ i ] ; } } return result ; } let arr = [ 4 , 5 , 6 ] ; let n = arr . length ; let k = 5 ; document . write ( MinOperation ( arr , n , k ) ) ;
Maximum product subset of an array | JavaScript program to find maximum product of a subset . ; Find count of negative numbers , count of zeros , negative number with least absolute value and product of non - zero numbers ; If number is 0 , we don 't  multiply it with product. ; Count negatives and keep track of negative number with least absolute value . ; If there are all zeros ; If there are odd number of negative numbers ; Exceptional case : There is only negative and all other are zeros ; Otherwise result is product of all non - zeros divided by negative number with least absolute value . ; Driver Code
function maxProductSubset ( a , n ) { if ( n == 1 ) return a [ 0 ] ; let max_neg = Number . MIN_SAFE_INTEGER ; let count_neg = 0 ; count_zero = 0 ; let prod = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ++ ; max_neg = Math . max ( max_neg , a [ i ] ) ; } prod = prod * a [ i ] ; } if ( count_zero == n ) return 0 ; if ( count_neg & 1 ) { if ( count_neg == 1 && count_zero > 0 && count_zero + count_neg == n ) return 0 ; prod = prod / max_neg ; } return prod ; } let a = [ - 1 , - 1 , - 2 , 4 , 3 ] ; let n = a . length ; document . write ( maxProductSubset ( a , n ) ) ;
Minimum Fibonacci terms with sum equal to K | Function to calculate Fibonacci Terms ; Calculate all Fibonacci terms which are less than or equal to K . ; If next term is greater than K do not push it in vector and return . ; Function to find the minimum number of Fibonacci terms having sum equal to K . ; Vector to store Fibonacci terms . ; Subtract Fibonacci terms from sum K until sum > 0. ; Divide sum K by j - th Fibonacci term to find how many terms it contribute in sum . ; Driver code
function calcFiboTerms ( fiboTerms , K ) { var i = 3 , nextTerm ; fiboTerms . push ( 0 ) ; fiboTerms . push ( 1 ) ; fiboTerms . push ( 1 ) ; while ( 1 ) { nextTerm = fiboTerms [ i - 1 ] + fiboTerms [ i - 2 ] ; if ( nextTerm > K ) return ; fiboTerms . push ( nextTerm ) ; i ++ ; } } function findMinTerms ( K ) { var fiboTerms = [ ] ; calcFiboTerms ( fiboTerms , K ) ; var count = 0 ; var j = fiboTerms . length - 1 ; while ( K > 0 ) { count += parseInt ( K / fiboTerms [ j ] ) ; K %= ( fiboTerms [ j ] ) ; j -- ; } return count ; } var K = 17 ; document . write ( findMinTerms ( K ) ) ;
Smallest number with sum of digits as N and divisible by 10 ^ N | JavaScript program to find smallest number to find smallest number with N as sum of digits and divisible by 10 ^ N . ; If N = 0 the string will be 0 ; If n is not perfectly divisible by 9 output the remainder ; Print 9 N / 9 times ; Append N zero 's to the number so  as to make it divisible by 10^N ; Driver Code
function digitsNum ( N ) { if ( N == 0 ) document . write ( " " ) ; if ( N % 9 != 0 ) document . write ( N % 9 ) ; for ( var i = 1 ; i <= N / 9 ; ++ i ) document . write ( " " ) ; for ( var i = 1 ; i <= N ; ++ i ) document . write ( " " ) ; document . write ( " " ) ; } var N = 5 ; document . write ( " " ) ; digitsNum ( N ) ;
Divide 1 to n into two groups with minimum sum difference | To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group 1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver program to test above functions
function printVector ( v ) { document . write ( v . length + " " ) ; for ( var i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + " " ) ; document . write ( " " ) ; } function findTwoGroup ( n ) { var sum = n * ( n + 1 ) / 2 ; var group1Sum = parseInt ( sum / 2 ) ; var group1 = [ ] , group2 = [ ] ; for ( var i = n ; i > 0 ; i -- ) { if ( group1Sum - i >= 0 ) { group1 . push ( i ) ; group1Sum -= i ; } else { group2 . push ( i ) ; } } printVector ( group1 ) ; printVector ( group2 ) ; } var n = 5 ; findTwoGroup ( n ) ;
Partition into two subarrays of lengths k and ( N | Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver code
function maxDifference ( arr , N , k ) { let M , S = 0 , S1 = 0 , max_difference = 0 ; for ( let i = 0 ; i < N ; i ++ ) S += arr [ i ] ; let temp ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } M = Math . max ( k , N - k ) ; for ( let i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1 ) ; return max_difference ; } let arr = [ 8 , 4 , 5 , 2 , 10 ] ; let N = arr . length ; let k = 2 ; document . write ( maxDifference ( arr , N , k ) ) ;
Minimum sum of product of two arrays | Function to find the minimum product ; Find product of current elements and update result . ; If both product and b [ i ] are negative , we must increase value of a [ i ] to minimize result . ; If both product and a [ i ] are negative , we must decrease value of a [ i ] to minimize result . ; Similar to above two cases for positive product . ; Check if current difference becomes higher than the maximum difference so far . ; Driver code
function minproduct ( a , b , n , k ) { let diff = 0 , res = 0 ; let temp = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let pro = a [ i ] * b [ i ] ; res = res + pro ; if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro < 0 && a [ i ] < 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] > 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; let d = Math . abs ( pro - temp ) ; if ( d > diff ) diff = d ; } return res - diff ; } let a = [ 2 , 3 , 4 , 5 , 4 ] ; let b = [ 3 , 4 , 2 , 3 , 2 ] ; let n = 5 , k = 3 ; document . write ( minproduct ( a , b , n , k ) ) ;
Split n into maximum composite numbers | function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; driver program to test the above function
function c_ount ( n ) { if ( n < 4 ) return - 1 ; let rem = n % 4 ; if ( rem == 0 ) return n / 4 ; if ( rem == 1 ) { if ( n < 9 ) return - 1 ; return ( n - 9 ) / 4 + 1 ; } if ( rem == 2 ) return ( n - 6 ) / 4 + 1 ; if ( rem == 3 ) { if ( n < 15 ) return - 1 ; return ( n - 15 ) / 4 + 2 ; } } let n = 90 ; document . write ( c_ount ( n ) + " " ) ; n = 143 ; document . write ( c_ount ( n ) ) ;
Minimum rotations to unlock a circular lock | function for min rotation ; iterate till input and unlock code become 0 ; input and unlock last digit as reminder ; find min rotation ; update code and input ; Driver Code
function minRotation ( input , unlock_code ) { let rotation = 0 ; let input_digit , code_digit ; while ( input > 0 unlock_code > 0 ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += Math . min ( Math . abs ( input_digit - code_digit ) , 10 - Math . abs ( input_digit - code_digit ) ) ; input = Math . floor ( input / 10 ) ; unlock_code = Math . floor ( unlock_code / 10 ) ; } return rotation ; } let input = 28756 ; let unlock_code = 98234 ; document . write ( " " + minRotation ( input , unlock_code ) ) ;
Minimum cost for acquiring all coins with k extra coins allowed with every coin | Converts coin [ ] to prefix sum array ; sort the coins value ; Maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver Code
function preprocess ( coin , n ) { coin . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 1 ; i <= n - 1 ; i ++ ) coin [ i ] += coin [ i - 1 ] ; } function minCost ( coin , n , k ) { let coins_needed = Math . ceil ( 1.0 * n / ( k + 1 ) ) ; return coin [ coins_needed - 1 ] ; } let coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ] ; let n = coin . length ; preprocess ( coin , n ) ; let k = 3 ; document . write ( minCost ( coin , n , k ) + " " ) ; k = 7 ; document . write ( minCost ( coin , n , k ) ) ;
Program for Worst Fit algorithm in Memory Management | Method to allocate memory to blocks as per worst fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; Pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; Allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver code
function worstFit ( blockSize , m , processSize , n ) { let allocation = new Array ( n ) ; for ( let i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let wstIdx = - 1 ; for ( let j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( wstIdx == - 1 ) wstIdx = j ; else if ( blockSize [ wstIdx ] < blockSize [ j ] ) wstIdx = j ; } } if ( wstIdx != - 1 ) { allocation [ i ] = wstIdx ; blockSize [ wstIdx ] -= processSize [ i ] ; } } document . write ( " " + " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( " " + ( i + 1 ) + " " + " " + processSize [ i ] + " " ) ; if ( allocation [ i ] != - 1 ) document . write ( allocation [ i ] + 1 ) ; else document . write ( " " ) ; document . write ( " " ) ; } } let blockSize = [ 100 , 500 , 200 , 300 , 600 ] ; let processSize = [ 212 , 417 , 112 , 426 ] ; let m = blockSize . length ; let n = processSize . length ; worstFit ( blockSize , m , processSize , n ) ;
Maximize array sum after K negations | Set 1 | This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; This the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver code
function maximumSum ( arr , n , k ) { for ( let i = 1 ; i <= k ; i ++ ) { let min = + 2147483647 ; let index = - 1 ; for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ index ] = - arr [ index ] ; } let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } let arr = [ - 2 , 0 , 5 , - 1 , 2 ] ; let k = 4 ; let n = arr . length ; document . write ( maximumSum ( arr , n , k ) ) ;
Maximize array sum after K negations | Set 1 | JavaScript program to find maximum array sum after at most k negations . ; Sorting given array using in - built java sort function ; If we find a 0 in our sorted array , we stop ; Calculating sum ; Driver code
function sol ( arr , k ) { arr . sort ( ) ; let sum = 0 ; let i = 0 ; while ( k > 0 ) { if ( arr [ i ] >= 0 ) k = 0 ; else { arr [ i ] = ( - 1 ) * arr [ i ] ; k = k - 1 ; } i ++ ; } for ( let j = 0 ; j < arr . length ; j ++ ) { sum += arr [ j ] ; } return sum ; } let arr = [ - 2 , 0 , 5 , - 1 , 2 ] ; document . write ( sol ( arr , 4 ) ) ;
Maximize array sum after K negations | Set 1 | Function to calculate sum of the array ; Iterate from 0 to n - 1 ; Function to maximize sum ; Iterate from 0 to n - 1 ; Driver Code ; Function Call
function sumArray ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } return sum ; } function maximizeSum ( arr , n , k ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; let i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( k != 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; continue ; } break ; } if ( i == n ) i -- ; if ( k == 0 k % 2 == 0 ) { return sumArray ( arr , n ) ; } if ( i != 0 && Math . abs ( arr [ i ] ) >= Math . abs ( arr [ i - 1 ] ) ) { i -- ; } arr [ i ] *= - 1 ; return sumArray ( arr , n ) ; } let n = 5 ; let k = 4 ; let arr = [ - 3 , - 2 , - 1 , 5 , 6 ] ; document . write ( maximizeSum ( arr , n , k ) ) ;