codeparrot/xlcost-text-to-code huggingface dataset:
Maximum count of sub | Function to return the count of the required sub - strings ; Iterate over all characters ; Count with current character ; If the substring has a length k then increment count with current character ; Update max count ; Driver Code
function maxSubStrings ( s , k ) { var maxSubStr = 0 , n = s . length ; for ( var c = 0 ; c < 26 ; c ++ ) { var ch = String . fromCharCode ( " " . charCodeAt ( 0 ) + c ) ; var curr = 0 ; for ( var i = 0 ; i <= n - k ; i ++ ) { if ( s [ i ] !== ch ) continue ; var cnt = 0 ; while ( i < n && s [ i ] === ch && cnt !== k ) { i ++ ; cnt ++ ; } i -- ; if ( cnt === k ) curr ++ ; } maxSubStr = Math . max ( maxSubStr , curr ) ; } return maxSubStr ; } var s = " " ; var k = 2 ; document . write ( maxSubStrings ( s , k ) ) ;
Count valid pairs in the array satisfying given conditions | Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to prvar required answer
function ValidPairs ( arr , n ) { var count = Array ( 121 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] += 1 ; var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) continue ; if ( Math . abs ( arr [ i ] - arr [ j ] ) % 2 == 1 ) continue ; ans += count [ arr [ i ] ] * count [ arr [ j ] ] ; if ( arr [ i ] == arr [ j ] ) ans -= count [ arr [ i ] ] ; } return ans ; } var arr = [ 16 , 17 , 18 ] ; var n = arr . length ; document . write ( ValidPairs ( arr , n ) ) ;
Distribution of candies according to ages of students | Function to check The validity of distribution ; Stroring the max age of all students + 1 ; Stroring the max candy + 1 ; Creating the frequency array of the age of students ; Creating the frequency array of the packets of candies ; Pointer to tell whether we have reached the end of candy frequency array ; Flag to tell if distribution is possible or not ; Flag to tell if we can choose some candy packets for the students with age j ; If the quantity of packets is greater than or equal to the number of students of age j , then we can choose these packets for the students ; Start searching from k + 1 in next operation ; If we cannot choose any packets then the answer is NO ; Driver code
function check_distribution ( n , k , age , candy ) { let mxage = age [ 0 ] ; for ( let i = 0 ; i < age . length ; i ++ ) { if ( mxage < age [ i ] ) { mxage = age [ i ] ; } } let mxcandy = candy [ 0 ] ; for ( let i = 0 ; i < candy . length ; i ++ ) { if ( mxcandy < candy [ i ] ) { mxcandy = candy [ i ] ; } } let fr1 = new Array ( mxage + 1 ) ; fr1 . fill ( 0 ) ; let fr2 = new Array ( mxcandy + 1 ) ; fr2 . fill ( 0 ) ; for ( let j = 0 ; j < n ; j ++ ) { fr1 [ age [ j ] ] += 1 ; } for ( let j = 0 ; j < k ; j ++ ) { fr2 [ candy [ j ] ] += 1 ; } k = 0 ; let Tf = true ; for ( let j = 0 ; j < mxage ; j ++ ) { if ( fr1 [ j ] == 0 ) { continue ; } let flag = false ; while ( k < mxcandy ) { if ( fr1 [ j ] <= fr2 [ k ] ) { flag = true ; break ; } k += 1 ; } k = k + 1 ; if ( flag == false ) { Tf = false ; break ; } } if ( Tf ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let age = [ 5 , 15 , 10 ] ; let candy = [ 2 , 2 , 2 , 3 , 3 , 4 ] ; let n = age . length ; let k = candy . length ; check_distribution ( n , k , age , candy ) ;
Find a palindromic string B such that given String A is a subsequense of B | Function to check if a string is palindrome ; Reversing a string ; Check if reversed string is equal to given string ; Function to find a palindromic string B such that given String A is a subsequense of B ; Reversing the string A ; If the string A is already a palindrome return A ; Else return B ; Swap values of left and right ; Driver Code
function checkPalindrome ( s ) { let x = reverse ( s ) ; return x == ( s ) ; } function findStringB ( A ) { let B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } function reverse ( input ) { let temparray = input . split ( " " ) ; let left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { let temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return ( temparray ) . join ( " " ) ; } let A = " " ; document . write ( findStringB ( A ) ) ;
Minimum number of 1 's to be replaced in a binary array | Function to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; return final answer ; Driver Code
function minChanges ( A , n ) { var cnt = 0 ; for ( var i = 0 ; i < n - 2 ; ++ i ) { if ( ( i - 1 >= 0 ) && A [ i - 1 ] == 1 && A [ i + 1 ] == 1 && A [ i ] == 0 ) { A [ i + 1 ] = 0 ; cnt ++ ; } } return cnt ; } var A = [ 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 ] ; var n = A . length ; document . write ( minChanges ( A , n ) ) ;
Number of closing brackets needed to complete a regular bracket sequence | Function to find number of closing brackets and complete a regular bracket sequence ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver Code
function completeSequence ( s ) { let n = s . length ; let open = 0 , close = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) open ++ ; else close ++ ; if ( close > open ) { document . write ( " " ) ; return ; } } document . write ( s ) ; for ( let i = 0 ; i < open - close ; i ++ ) document . write ( " " ) ; } let s = " " ; completeSequence ( s ) ;
Lexicographically smallest permutation with no digits at Original Index | Function to print the smallest permutation ; when n is even ; when n is odd ; handling last 3 digit ; add EOL and print result ; Driver Code
function smallestPermute ( n ) { var res = Array ( n + 1 ) . fill ( 0 ) ; if ( n % 2 == 0 ) { for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = 48 + i + 2 ; else res [ i ] = 48 + i ; } } else { for ( var i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = 48 + i + 2 ; else res [ i ] = 48 + i ; } res [ n - 1 ] = 48 + n - 2 ; res [ n - 2 ] = 48 + n ; res [ n - 3 ] = 48 + n - 1 ; } for ( var i = 0 ; i < res . length ; i ++ ) { res [ i ] = String . fromCharCode ( res [ i ] ) ; } return res . join ( " " ) ; } var n = 7 ; document . write ( smallestPermute ( n ) ) ;
Minimum array insertions required to make consecutive difference <= K | Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver Code
function minInsertions ( H , n , K ) { var inser = 0 ; for ( var i = 1 ; i < n ; ++ i ) { var diff = Math . abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += Math . ceil ( diff / K ) - 1 ; } return inser ; } var H = [ 2 , 4 , 8 , 16 ] ; var K = 3 ; var n = H . length ; document . write ( minInsertions ( H , n , K ) ) ;
Minimum number of operations required to reduce N to 1 | Function that returns the minimum number of operations to be performed to reduce the number to 1 ; To stores the total number of operations to be performed ; if n is divisible by 3 then reduce it to n / 3 ; if n modulo 3 is 1 decrement it by 1 ; if n modulo 3 is 2 then increment it by 1 ; update the counter ; Driver code
function count_minimum_operations ( n ) { let count = 0 ; while ( n > 1 ) { if ( n % 3 == 0 ) n /= 3 ; else if ( n % 3 == 1 ) n -- ; else { if ( n == 2 ) n -- ; else n ++ ; } count ++ ; } return count ; } let n = 4 ; let ans = count_minimum_operations ( n ) ; document . write ( ans ) ;
Minimum number of operations required to reduce N to 1 | Function that returns the minimum number of operations to be performed to reduce the number to 1 ; Base cases ; Driver code
function count_minimum_operations ( n ) { if ( n == 2 ) { return 1 ; } else if ( n == 1 ) { return 0 ; } if ( n % 3 == 0 ) { return 1 + count_minimum_operations ( n / 3 ) ; } else if ( n % 3 == 1 ) { return 1 + count_minimum_operations ( n - 1 ) ; } else { return 1 + count_minimum_operations ( n + 1 ) ; } } let n = 4 ; let ans = count_minimum_operations ( n ) ; document . write ( ans ) ;
Maximize the sum of array by multiplying prefix of array with | Javascript implementation of the above approach ; To store sum ; To store ending indices of the chosen prefix array vect ; Adding the absolute value of a [ i ] ; If i == 0 then there is no index to be flipped in ( i - 1 ) position ; print the maximised sum ; print the ending indices of the chosen prefix arrays ; driver code
function maxSum ( a , n ) { let l = [ ] ; let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { s += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) continue ; if ( i == 0 ) l . push ( i + 1 ) ; else { l . push ( i + 1 ) ; l . push ( i ) ; } } document . write ( s + " " ) ; for ( let i = 0 ; i < l . length ; i ++ ) document . write ( l [ i ] + " " ) ; } let n = 4 ; let a = [ 1 , - 2 , - 3 , 4 ] ; maxSum ( a , n ) ;
Find the longest common prefix between two strings after performing swaps on second string | JavaScript program to find the long common prefix between two strings after performing swaps on the second string ; let a = x . length ; length of x let b = y . length ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; driver code
function LengthLCP ( x , y ) { let fr = Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < b ; i ++ ) { fr [ y [ i ] . charCodeAt ( ) - 97 ] += 1 ; } let c = 0 ; for ( let i = 0 ; i < a ; i ++ ) { if ( fr [ x [ i ] . charCodeAt ( ) - 97 ] > 0 ) { c += 1 ; fr [ x [ i ] . charCodeAt ( ) - 97 ] -= 1 ; } else break ; } document . write ( ( c ) ) ; } let x = " " , y = " " ; LengthLCP ( x , y ) ;
All possible co | Function to count possible pairs ; total count of numbers in range ; printing count of pairs ; driver code
function CountPair ( L , R ) { let x = ( R - L + 1 ) ; document . write ( x / 2 + " " ) ; } let L , R ; L = 1 ; R = 8 ; CountPair ( L , R ) ;
Problems not solved at the end of Nth day | Function to find problems not solved at the end of Nth day ; Driver Code
function problemsLeft ( K , P , N ) { if ( K <= P ) return 0 ; else return ( K - P ) * N ; } let K , P , N ; K = 4 ; P = 1 ; N = 10 ; document . write ( problemsLeft ( K , P , N ) ) ;
Kruskal 's Algorithm (Simple Implementation for Adjacency Matrix) | Simple Javascript implementation for Kruskal 's algorithm ; Find set of vertex i ; Does union of i and j . It returns false if i and j are already in same set . ; Finds MST using Kruskal 's algorithm ; Initialize sets of disjoint sets . ; Include minimum weight edges one by one ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution
var V = 5 ; var parent = Array ( V ) . fill ( 0 ) ; var INF = 1000000000 ; function find ( i ) { while ( parent [ i ] != i ) i = parent [ i ] ; return i ; } function union1 ( i , j ) { var a = find ( i ) ; var b = find ( j ) ; parent [ a ] = b ; } function kruskalMST ( cost ) { for ( var i = 0 ; i < V ; i ++ ) parent [ i ] = i ; var edge_count = 0 ; while ( edge_count < V - 1 ) { var min = INF , a = - 1 , b = - 1 ; for ( var i = 0 ; i < V ; i ++ ) { for ( var j = 0 ; j < V ; j ++ ) { if ( find ( i ) != find ( j ) && cost [ i ] [ j ] < min ) { min = cost [ i ] [ j ] ; a = i ; b = j ; } } } union1 ( a , b ) ; document . write ( ` ${ edge_count ++ } ${ a } ${ b } ${ min } ` ) ; mincost += min ; } document . write ( ` ${ mincost } ` ) ; } var cost = [ [ INF , 2 , INF , 6 , INF ] , [ 2 , INF , 3 , 8 , 5 ] , [ INF , 3 , INF , INF , 7 ] , [ 6 , 8 , INF , INF , 9 ] , [ INF , 5 , 7 , 9 , INF ] ] ; kruskalMST ( cost ) ;
Number of chocolates left after k iterations | Function to find the chocolates left ; Driver code
function results ( n , k ) { return parseInt ( Math . round ( Math . pow ( n , ( 1.0 / Math . pow ( 2.0 , k ) ) ) ) ) ; } var k = 3 , n = 100000000 ; document . write ( " " + k + " " + results ( n , k ) ) ;
Subarray whose absolute sum is closest to K | Javascript code to find sub - array whose sum shows the minimum deviation ; Starting index , ending index , Deviation ; Iterate i and j to get all subarrays ; Found sub - array with less sum ; Exactly same sum ; driver code ; Array to store return values
function getSubArray ( arr , n , K ) { let i = - 1 ; let j = - 1 ; let currSum = 0 ; let result = [ i , j , Math . abs ( K - Math . abs ( currSum ) ) ] ; for ( i = 0 ; i < n ; i ++ ) { currSum = 0 ; for ( j = i ; j < n ; j ++ ) { currSum += arr [ j ] ; let currDev = Math . abs ( K - Math . abs ( currSum ) ) ; if ( currDev < result [ 2 ] ) { result [ 0 ] = i ; result [ 1 ] = j ; result [ 2 ] = currDev ; } if ( currDev == 0 ) return result ; } } return result ; } let arr = [ 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 ] ; let n = arr . length ; let K = 50 ; let ans = getSubArray ( arr , n , K ) ; if ( ans [ 0 ] == - 1 ) { document . write ( " " + " " ) ; } else { for ( let i = ans [ 0 ] ; i <= ans [ 1 ] ; i ++ ) document . write ( arr [ i ] + " " ) ; }
Longest subsequence whose average is less than K | Function to print the length for evey query ; sort array of N elements ; Array to store average from left ; Sort array of average ; number of queries ; print answer to every query using binary search ; Driver Code ; 4 queries
function longestSubsequence ( a , n , q , m ) { a . sort ( function ( a , b ) { return a - b } ) ; let sum = 0 ; let b = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; let av = parseInt ( ( sum ) / ( i + 1 ) , 10 ) ; b [ i ] = ( av + 1 ) ; } b . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < m ; i ++ ) { let k = q [ i ] ; let longest = upperBound ( b , 0 , n , k ) ; document . write ( " " + ( i + 1 ) + " " + longest + " " ) ; } } function upperBound ( a , low , high , element ) { while ( low < high ) { let middle = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let a = [ 1 , 3 , 2 , 5 , 4 ] ; let n = a . length ; let q = [ 4 , 2 , 1 , 5 ] ; let m = q . length ; longestSubsequence ( a , n , q , m ) ;
Make array elements equal in Minimum Steps | Returns the minimum steps required to make an array of N elements equal , where the first array element equals M ; Corner Case 1 : When N = 1 ; Corner Case 2 : When N = 2 else if ( N == 2 ) corner case 2 ; Driver Code
function steps ( N , M ) { if ( N == 1 ) return 0 ; return M ; return 2 * M + ( N - 3 ) ; } let N = 4 , M = 4 ; document . write ( steps ( N , M ) ) ;
Minimum increment / decrement to make array non | Javascript code to count the change required to convert the array into non - increasing array ; min heap ; Here in the loop we will check that whether the upcoming element of array is less than top of priority queue . If yes then we calculate the difference . After that we will remove that element and push the current element in queue . And the sum is incremented by the value of difference ; Driver Code
function DecreasingArray ( a , n ) { var sum = 0 , dif = 0 ; var pq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( pq . length != 0 && pq [ pq . length - 1 ] < a [ i ] ) { dif = a [ i ] - pq [ pq . length - 1 ] ; sum += dif ; pq . pop ( ) ; } pq . push ( a [ i ] ) ; pq . sort ( ( a , b ) => b - a ) ; } return sum ; } var a = [ 3 , 1 , 2 , 1 ] ; var n = a . length ; document . write ( DecreasingArray ( a , n ) ) ;
Schedule jobs so that each server gets equal load | Function to find new array a ; find sum S of both arrays a and b . ; Single element case . ; This checks whether sum s can be divided equally between all array elements . i . e . whether all elements can take equal value or not . ; Compute possible value of new array elements . ; Possibility 1 ; ensuring that all elements of array b are used . ; If a ( i ) already updated to x move to next element in array a . ; Possibility 2 ; Possibility 3 ; Possibility 4 ; If a ( i ) can not be made equal to x even after adding all possible elements from b ( i ) then print - 1. ; check whether all elements of b are used . ; Return the new array element value . ; Driver Code
function solve ( a , b , n ) { let i ; let s = 0 ; for ( i = 0 ; i < n ; i ++ ) s += ( a [ i ] + b [ i ] ) ; if ( n == 1 ) return a [ 0 ] + b [ 0 ] ; if ( s % n != 0 ) return - 1 ; let x = s / n ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) return - 1 ; if ( i > 0 ) { a [ i ] += b [ i - 1 ] ; b [ i - 1 ] = 0 ; } if ( a [ i ] == x ) continue ; let y = a [ i ] + b [ i ] ; if ( i + 1 < n ) y += b [ i + 1 ] ; if ( y == x ) { a [ i ] = y ; b [ i ] = 0 ; continue ; } if ( a [ i ] + b [ i ] == x ) { a [ i ] += b [ i ] ; b [ i ] = 0 ; continue ; } if ( i + 1 < n && a [ i ] + b [ i + 1 ] == x ) { a [ i ] += b [ i + 1 ] ; b [ i + 1 ] = 0 ; continue ; } return - 1 ; } for ( i = 0 ; i < n ; i ++ ) if ( b [ i ] != 0 ) return - 1 ; return x ; } let a = [ 6 , 14 , 21 , 1 ] ; let b = [ 15 , 7 , 10 , 10 ] ; let n = a . length ; document . write ( solve ( a , b , n ) ) ;
Check if it is possible to survive on Island | function to find the minimum days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver Code
function survival ( S , N , M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) M > N ) document . write ( " " ) ; else { let days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; document . write ( " " + Math . round ( days ) ) ; } } let S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ;
Lexicographically largest subsequence such that every character occurs at least k times | Find lexicographically largest subsequence of s [ 0. . n - 1 ] such that every character appears at least k times . The result is filled in t [ ] ; Starting from largest charter ' z ' to ' a ' ; Counting the frequency of the character ; If frequency is greater than k ; From the last point we leave ; check if string contain ch ; If yes , append to output string ; Update the last point . ; Driver code
function subsequence ( s , t , n , k ) { var last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( var ch = ' ' . charCodeAt ( 0 ) ; ch >= ' ' . charCodeAt ( 0 ) ; ch -- ) { cnt = 0 ; for ( var i = last ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) == ch ) cnt ++ ; } if ( cnt >= k ) { for ( var i = last ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) == ch ) { t [ size ++ ] = String . fromCharCode ( ch ) ; new_last = i ; } } last = new_last ; } } } var s = " " ; var n = s . length ; var k = 2 ; var t = Array ( n ) ; subsequence ( s , t , n - 1 , k ) ; document . write ( t . join ( ' ' ) ) ;
Program for Least Recently Used ( LRU ) Page Replacement algorithm | Method to find page faults using indexes ; To represent set of current pages . We use an unordered_set so that we quickly check if a page is present in set or not ; To store least recently used indexes of pages . ; Start from initial page ; Check if the set can hold more pages ; Insert it into set if not present already which represents page fault ; increment page fault ; Store the recently used index of each page ; If the set is full then need to perform lru i . e . remove the least recently used page and insert the current page ; Check if current page is not already present in the set ; Find the least recently used pages that is present in the set ; Remove the indexes page ; remove lru from hashmap ; insert the current page ; Increment page faults ; Update the current page index ; Driver method
function pageFaults ( pages , n , capacity ) { let s = new Set ( ) ; let indexes = new Map ( ) ; let page_faults = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s . size < capacity ) { if ( ! s . has ( pages [ i ] ) ) { s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . set ( pages [ i ] , i ) ; } else { if ( ! s . has ( pages [ i ] ) ) { let lru = Number . MAX_VALUE , val = Number . MIN_VALUE ; for ( let itr of s . values ( ) ) { let temp = itr ; if ( indexes [ temp ] < lru ) { lru = indexes [ temp ] ; val = temp ; } } s . delete ( val ) ; indexes . delete ( val ) ; s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . set ( pages [ i ] , i ) ; } } return page_faults ; } let pages = [ 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 ] ; let capacity = 4 ; document . write ( pageFaults ( pages , pages . length , capacity ) ) ;
Largest permutation after at most k swaps | Function to find the largest permutation after k swaps ; Storing the elements and their index in map ; If number of swaps allowed are equal to number of elements then the resulting permutation will be descending order of given permutation . ; if j is not at it 's best index ; Change the index of the element which was at position 0. Swap the element basically . ; decrement number of swaps ; Driver code ; K is the number of swaps ; n is the size of the array ; Function calling
function bestpermutation ( arr , k , n ) { let h = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { h . set ( arr [ i ] , i ) ; } if ( n <= k ) { arr . sort ( function ( a , b ) { return b - a ; } ) ; } else { for ( let j = n ; j >= 1 ; j -- ) { if ( k > 0 ) { let initial_index = h [ j ] ; let best_index = n - j ; if ( initial_index != best_index ) { h . set ( j , best_index ) ; let element = arr . get ( best_index ) ; h . set ( element , initial_index ) ; let temp = arr [ best_index ] ; arr . set ( best_index , arr [ initial_index ] ) ; arr . set ( initial_index , temp ) ; k -- ; } } } } } let arr = [ 3 , 1 , 4 , 2 , 5 ] ; let k = 10 ; let n = arr . length ; bestpermutation ( arr , k , n ) ; document . write ( " " + k + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Bin Packing Problem ( Minimize number of used Bins ) | Returns number of bins required using best fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that can accommodate weight [ i ] ; Initialize minimum space left and index of best bin ; If no bin could accommodate weight [ i ] , create a new bin ; } else Assign the item to best bin ; Driver code
function bestFit ( weight , n , c ) { var res = 0 ; var bin_rem = Array ( n ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { var j ; var min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; bin_rem [ bi ] -= weight [ i ] ; } return res ; } var weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; var c = 10 ; var n = weight . length ; document . write ( " " + bestFit ( weight , n , c ) ) ;
Bin Packing Problem ( Minimize number of used Bins ) | Returns number of bins required using worst fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that ca \ n accommodate weight [ i ] ; Initialize maximum space left and index of worst bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver program
function worstFit ( weight , n , c ) { var res = 0 ; var bin_rem = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { var j ; var mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } var weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; var c = 10 ; var n = weight . length ; document . write ( " " + worstFit ( weight , n , c ) ) ;