codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations of given type to make all elements of a matrix equal | Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; will not work for negative elements , so . . adding this ; adding this to handle negative elements too . ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; changed here as in case of even elements there will be 2 medians ; Return minimum operations required ; Driver code
function minOperations ( n , m , k , matrix ) { let arr = [ ] ; let mod ; if ( matrix [ 0 ] [ 0 ] < 0 ) { mod = k - ( Math . abs ( matrix [ 0 ] [ 0 ] ) % k ) ; } else { mod = matrix [ 0 ] [ 0 ] % k ; } for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < m ; ++ j ) { arr . push ( matrix [ i ] [ j ] ) ; let val = matrix [ i ] [ j ] ; if ( val < 0 ) { let res = k - ( Math . abs ( val ) % k ) ; if ( res != mod ) { return - 1 ; } } else { let foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return - 1 ; } } } } arr . sort ( function ( a , b ) { return a - b ; } ) ; let median = arr [ ( n * m ) / 2 ] ; let minOperations = 0 ; for ( let i = 0 ; i < n * m ; ++ i ) minOperations += Math . abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { let median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; let minOperations2 = 0 ; for ( let i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . abs ( arr [ i ] - median2 ) / k ; minOperations = Math . min ( minOperations , minOperations2 ) ; } return minOperations ; } let matrix = [ [ 2 , 4 , 6 ] , [ 8 , 10 , 12 ] , [ 14 , 16 , 18 ] , [ 20 , 22 , 24 ] ] ; let n = matrix . length ; let m = matrix [ 0 ] . length ; let k = 2 ; document . write ( minOperations ( n , m , k , matrix ) ) ;
Smallest subarray containing minimum and maximum values | Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code
function minSubarray ( A , n ) { let minValue = Number . MAX_VALUE ; let maxValue = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { minValue = Math . min ( minValue , A [ i ] ) ; maxValue = Math . max ( maxValue , A [ i ] ) ; } let pos_min = - 1 , pos_max = - 1 , ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != - 1 && pos_min != - 1 ) ans = Math . min ( ans , Math . abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } let A = [ 1 , 5 , 9 , 7 , 1 , 9 , 4 ] ; let n = A . length ; document . write ( minSubarray ( A , n ) ) ;
Minimum number of consecutive sequences that can be formed in an array | Javascript program find the minimum number of consecutive sequences in an array ; Driver program ; function call to print required answer
function countSequences ( arr , n ) { let count = 1 ; arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; return count ; } let arr = [ 1 , 7 , 3 , 5 , 10 ] ; let n = arr . length ; document . write ( countSequences ( arr , n ) ) ;
Minimum number of increment / decrement operations such that array contains all elements from 1 to N | Function to find the minimum operations ; Sort the given array ; Count operations by assigning a [ i ] = i + 1 ; Driver code
function minimumMoves ( a , n ) { let operations = 0 ; a . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) operations += Math . abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } let arr = [ 5 , 3 , 2 ] ; let n = arr . length ; document . write ( minimumMoves ( arr , n ) ) ;
Print a case where the given sorting algorithm fails | Function to print a case where the given sorting algorithm fails ; only case where it fails ; Driver Code
function printCase ( n ) { if ( n <= 2 ) { document . write ( - 1 ) ; return ; } for ( let i = n ; i >= 1 ; i -- ) document . write ( i + " " ) ; } let n = 3 ; printCase ( n ) ;
Check whether it is possible to make both arrays equal by modifying a single element | Function to check if both sequences can be made equal ; Sorting both the arrays ; Flag to tell if there are more than one mismatch ; To stores the index of mismatched element ; If there is more than one mismatch then return False ; If there is no mismatch or the difference between the mismatching elements is <= k then return true ; Driver code
function check ( n , k , a , b ) { a . sort ( ) ; b . sort ( ) ; let fl = false ; let ind = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { if ( fl == true ) { return false ; } fl = true ; ind = i ; } } if ( ind == - 1 | Math . abs ( a [ ind ] - b [ ind ] ) <= k ) { return true ; } return false ; } let n = 2 , k = 4 ; let a = [ 1 , 5 ] ; let b = [ 1 , 1 ] ; if ( check ( n , k , a , b ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Sum of width ( max and min diff ) of all Subsequences | Javascript implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver program
var MOD = 1000000007 function SubseqWidths ( A , n ) { A . sort ( ( a , b ) => a - b ) var pow2 = Array ( n ) . fill ( 0 ) ; pow2 [ 0 ] = 1 ; for ( var i = 1 ; i < n ; ++ i ) pow2 [ i ] = ( pow2 [ i - 1 ] * 2 ) % MOD ; var ans = 0 ; for ( var i = 0 ; i < n ; ++ i ) ans = ( ans + ( pow2 [ i ] - pow2 [ n - 1 - i ] ) * A [ i ] ) % MOD ; return ans ; } var A = [ 5 , 6 , 4 , 3 , 8 ] ; var n = A . length ; document . write ( SubseqWidths ( A , n ) ) ;
Covering maximum array elements with given value | Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; driver code
function maxArrayCover ( a , n , x ) { a . sort ( ) ; let cc = 0 ; let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { s += a [ i ] ; if ( s > x ) { break ; } cc += 1 ; } var sum = a . reduce ( function ( a , b ) { return a + b ; } , 0 ) ; if ( sum == x ) { return n ; } else { if ( cc == n ) { return n - 1 ; } else { return cc ; } } } let n = 3 ; let x = 70 ; let a = [ 10 , 20 , 30 ] ; document . write ( maxArrayCover ( a , n , x ) ) ;
Check if a Linked List is Pairwise Sorted | A linked list node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; The constructed linked list is : 10.15 . 9.9 .1 .5
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var start = null ; function isPairWiseSorted ( head ) { var flag = true ; var temp = head ; while ( temp != null && temp . next != null ) { if ( temp . data > temp . next . data ) { flag = false ; break ; } temp = temp . next . next ; } return flag ; } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; start = head_ref ; } start = null ; push ( start , 5 ) ; push ( start , 1 ) ; push ( start , 9 ) ; push ( start , 9 ) ; push ( start , 15 ) ; push ( start , 10 ) ; if ( isPairWiseSorted ( start ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum Sum of Products of Two Arrays | Function that calculates maximum sum of products of two arrays ; Variable to store the sum of products of array elements ; length of the arrays ; Sorting both the arrays ; Traversing both the arrays and calculating sum of product ; Driver code
function maximumSOP ( a , b ) { let sop = 0 ; let n = a . length ; a . sort ( ) ; b . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { sop += ( a [ i ] * b [ i ] ) ; } return sop ; } let A = [ 1 , 2 , 3 ] ; let B = [ 4 , 5 , 1 ] ; document . write ( maximumSOP ( A , B ) ) ;
Count number of triplets with product equal to given number | Set 2 | Function to count such triplets ; Sort the array ; three pointer technique ; Calculate the product of a triplet ; Check if that product is greater than m , decrement mid ; Check if that product is smaller than m , increment start ; Check if that product is equal to m , decrement mid , increment start and increment the count of pairs ; Driver code
function countTriplets ( arr , n , m ) { let count = 0 ; arr . sort ( function ( a , b ) { return a - b } ) ; let end , start , mid ; for ( end = n - 1 ; end >= 2 ; end -- ) { start = 0 ; mid = end - 1 ; while ( start < mid ) { let prod = arr [ end ] * arr [ start ] * arr [ mid ] ; if ( prod > m ) mid -- ; else if ( prod < m ) start ++ ; else if ( prod == m ) { count ++ ; mid -- ; start ++ ; } } } return count ; } let arr = [ 1 , 1 , 1 , 1 , 1 , 1 ] ; let n = arr . length ; let m = 1 ; document . write ( countTriplets ( arr , n , m ) ) ;
Equally divide into two sets such that one set has maximum distinct elements | Javascript program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Insert all the resources in the set There will be unique resources in the set ; return minimum of distinct resources and n / 2 ; Driver code
function distribution ( arr , n ) { let resources = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) resources . add ( arr [ i ] ) ; return Math . min ( resources . size , parseInt ( n / 2 , 10 ) ) ; } let arr = [ 1 , 1 , 2 , 1 , 3 , 4 ] ; let n = arr . length ; document . write ( distribution ( arr , n ) + " " ) ;
Sort 3 numbers | Javascript program to sort an array of size 3 ; Insert arr [ 1 ] ; Insert arr [ 2 ] ; Driver code
function sort3 ( arr , temp ) { if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } if ( arr [ 2 ] < arr [ 1 ] ) { temp [ 0 ] = arr [ 1 ] ; arr [ 1 ] = arr [ 2 ] ; arr [ 2 ] = temp [ 0 ] ; if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } } } let a = [ 10 , 12 , 5 ] ; let temp1 = [ 10 ] ; sort3 ( a , temp1 ) ; for ( let i = 0 ; i < 3 ; i ++ ) document . write ( a [ i ] + " " ) ;
Merge Sort with O ( 1 ) extra space merge and O ( n lg n ) time [ Unsigned Integers Only ] | Javascript program to sort an array using merge sort such that merge operation takes O ( 1 ) extra space . ; Obtaining actual values ; Recursive merge sort with extra parameter , naxele ; This functions finds max element and calls recursive merge sort . ; Driver code
function merge ( arr , beg , mid , end , maxele ) { let i = beg ; let j = mid + 1 ; let k = beg ; while ( i <= mid && j <= end ) { if ( arr [ i ] % maxele <= arr [ j ] % maxele ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } else { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } } while ( i <= mid ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } while ( j <= end ) { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } for ( i = beg ; i <= end ; i ++ ) { arr [ i ] = Math . floor ( arr [ i ] / maxele ) ; } } function mergeSortRec ( arr , beg , end , maxele ) { if ( beg < end ) { let mid = Math . floor ( ( beg + end ) / 2 ) ; mergeSortRec ( arr , beg , mid , maxele ) ; mergeSortRec ( arr , mid + 1 , end , maxele ) ; merge ( arr , beg , mid , end , maxele ) ; } } function mergeSort ( arr , n ) { let maxele = Math . max ( ... arr ) + 1 ; mergeSortRec ( arr , 0 , n - 1 , maxele ) ; } let arr = [ 999 , 612 , 589 , 856 , 56 , 945 , 243 ] ; let n = arr . length ; mergeSort ( arr , n ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Print triplets with sum less than k | JavaScript program to print triplets with sum smaller than a given value ; Sort input array ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there are total k - j third elements . ; Driver Code
function printTriplets ( arr , n , sum ) { arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < n - 2 ; i ++ ) { let j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { for ( let x = j + 1 ; x <= k ; x ++ ) document . write ( arr [ i ] + " " + arr [ j ] + " " + arr [ x ] + " " ) ; j ++ ; } } } } let arr = [ 5 , 1 , 3 , 4 , 7 ] ; let n = arr . length ; let sum = 12 ; printTriplets ( arr , n , sum ) ;
Sort string of characters using Stack | Function to print the characters in sorted order ; Primary stack ; Secondary stack ; Append first character ; Iterate for all character in string ; i - th character ASCII ; stack 's top element ASCII  ; If greater or equal to top element then push to stack ; If smaller , then push all element to the temporary stack ; Push all greater elements ; Push operation ; Push till the stack is not - empty ; Push the i - th character ; Push the tempstack back to stack ; Print the stack in reverse order ; Driver Code
function printSorted ( s , l ) { var Stack = [ ] ; var tempstack = [ ] ; Stack . push ( s [ 0 ] . charCodeAt ( 0 ) ) ; for ( var i = 1 ; i < l ; i ++ ) { var a = s [ i ] . charCodeAt ( 0 ) ; var b = Stack [ Stack . length - 1 ] ; if ( ( a - b ) >= 1 || ( a == b ) ) Stack . push ( s [ i ] . charCodeAt ( 0 ) ) ; else if ( ( b - a ) >= 1 ) { while ( ( b - a ) >= 1 ) { tempstack . push ( Stack [ Stack . length - 1 ] ) ; Stack . pop ( ) ; if ( Stack . length > 0 ) b = Stack [ Stack . length - 1 ] ; else break ; } Stack . push ( s [ i ] . charCodeAt ( 0 ) ) ; while ( tempstack . length > 0 ) { Stack . push ( tempstack [ tempstack . length - 1 ] ) ; tempstack . pop ( ) ; } } } var answer = " " ; while ( Stack . length > 0 ) { answer = String . fromCharCode ( Stack [ Stack . length - 1 ] ) + answer ; Stack . pop ( ) ; } document . write ( answer ) } var s = " " ; var l = s . length ; printSorted ( s , l ) ;
Check whether an array can be fit into another array rearranging the elements in the array | Returns true if the array A can be fit into array B , otherwise false ; Sort both the arrays ; Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B ; Driver Code
function checkFittingArrays ( A , B , N ) { A . sort ( function ( a , b ) { return a - b ; } ) ; B . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < N ; i ++ ) if ( A [ i ] > B [ i ] ) return false ; return true ; } let A = [ 7 , 5 , 3 , 2 ] ; let B = [ 5 , 4 , 8 , 7 ] ; let N = A . length ; if ( checkFittingArrays ( A , B , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximise the number of toys that can be purchased with amount K | This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment count ; Driver Code
function maximum_toys ( cost , N , K ) { let count = 0 , sum = 0 ; cost . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( sum + cost [ i ] <= K ) { sum = sum + cost [ i ] ; count ++ ; } } return count ; } let K = 50 ; let cost = [ 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 ] ; let N = cost . length ; document . write ( maximum_toys ( cost , N , K ) ) ;
Find if k bookings possible with given arrival and departure times | Javascript code implementation of the above approach ; Driver code
function areBookingsPossible ( A , B , K , N ) { A . sort ( ) ; B . sort ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( i + K < N && A [ i + K ] < B [ i ] ) { return " " ; } } return " " ; } let arrival = [ 1 , 2 , 3 ] ; let departure = [ 2 , 3 , 4 ] ; let N = arrival . length ; let K = 1 ; document . write ( areBookingsPossible ( arrival , departure , K , N ) ) ;
Insertion Sort by Swapping Elements | Utility function to print a Vector ; Function to perform Insertion Sort recursively ; General Case Sort V till second last element and then insert last element into V ; Insertion step ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j ; Driver Code
function printVector ( V ) { for ( let e of V ) { document . write ( e + " " ) ; } document . write ( " " ) ; } function insertionSortRecursive ( V , N ) { if ( N <= 1 ) return ; insertionSortRecursive ( V , N - 1 ) ; let j = N - 1 ; while ( j > 0 && V [ j ] < V [ j - 1 ] ) { let temp = V [ j ] ; V [ j ] = V [ j - 1 ] ; V [ j - 1 ] = temp ; j -= 1 ; } } let A = [ 9 , 8 , 7 , 5 , 2 , 1 , 2 , 3 ] ; document . write ( " " ) ; printVector ( A ) ; document . write ( " " ) ; insertionSortRecursive ( A , A . length ) ; printVector ( A ) ;
Most frequent word in an array of strings | Function returns word with highest frequency ; Create Dictionary to store word and it 's frequency ; Iterate through array of words ; If word already exist in Dictionary then increase it 's count by 1 ; Otherwise add word to Dictionary ; Create set to iterate over Dictionary ; Check for word having highest frequency ; Return word having highest frequency ; Driver code ; Print word having highest frequency
function findWord ( arr ) { var hs = { } ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( hs . hasOwnProperty ( arr [ i ] ) ) { hs [ arr [ i ] ] = hs [ arr [ i ] ] + 1 ; } else { hs [ arr [ i ] ] = 1 ; } } var Key = " " ; var Value = 0 ; for ( const [ key , value ] of Object . entries ( hs ) ) { if ( value > Value ) { Value = value ; Key = key ; } } return Key ; } var arr = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , ] ; var sol = findWord ( arr ) ; document . write ( sol ) ;
Check if given array is almost sorted ( elements are at | function for checking almost sort ; One by one compare adjacents . ; Check whether resultant is sorted or not ; is resultant is sorted return true ; Driver Code
function almostSort ( A , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { let temp = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = temp ; i ++ ; } } for ( let i = 0 ; i < n - 1 ; i ++ ) if ( A [ i ] > A [ i + 1 ] ) return false ; return true ; } let A = [ 1 , 3 , 2 , 4 , 6 , 5 ] ; let n = A . length ; if ( almostSort ( A , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Efficiently merging two sorted arrays with O ( 1 ) extra space | Function to find next gap . ; comparing elements in the first array . ; comparing elements in both arrays . ; comparing elements in the second array . ; Driver code ; Function Call
function nextGap ( gap ) { if ( gap <= 1 ) return 0 ; return parseInt ( gap / 2 , 10 ) + ( gap % 2 ) ; } function merge ( arr1 , arr2 , n , m ) { let i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { let temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) { let temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ j ] ; arr2 [ j ] = temp ; } if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) { let temp = arr2 [ j ] ; arr2 [ j ] = arr2 [ j + gap ] ; arr2 [ j + gap ] = temp ; } } } } let a1 = [ 10 , 27 , 38 , 43 , 82 ] ; let a2 = [ 3 , 9 ] ; merge ( a1 , a2 , a1 . length , a2 . length ) ; document . write ( " " ) ; for ( let i = 0 ; i < a1 . length ; i ++ ) { document . write ( a1 [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = 0 ; i < a2 . length ; i ++ ) { document . write ( a2 [ i ] + " " ) ; }
Sort array after converting elements to their squares | Function to sort an square array ; First convert each array elements into its square ; Sort an array using " inbuild ▁ sort ▁ function " in Arrays class . ; Driver Code
function sortSquares ( arr ) { let n = arr . length ; for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; arr . sort ( ) ; } let arr = [ - 6 , - 3 , - 1 , 2 , 4 , 5 ] ; let n = arr . length ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; sortSquares ( arr ) ; document . write ( " " + " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Sort an array when two halves are sorted | Merge two sorted halves of Array into single sorted array ; Temp Array store sorted resultant array ; First Find the point where array is divide into two half ; If Given array is all - ready sorted ; Merge two sorted arrays in single sorted array ; Copy the remaining elements of A [ i to half_ ! ] ; Copy the remaining elements of A [ half_ ! to n ] ; Driver code ; Print sorted Array
function mergeTwoHalf ( A , n ) { let temp = new Array ( n ) ; temp . fill ( 0 ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { half_i = i + 1 ; break ; } } if ( half_i == 0 ) return ; let i = 0 , j = half_i , k = 0 ; while ( i < half_i && j < n ) { if ( A [ i ] < A [ j ] ) temp [ k ++ ] = A [ i ++ ] ; else temp [ k ++ ] = A [ j ++ ] ; } while ( i < half_i ) temp [ k ++ ] = A [ i ++ ] ; while ( j < n ) temp [ k ++ ] = A [ j ++ ] ; for ( let i = 0 ; i < n ; i ++ ) A [ i ] = temp [ i ] ; } let A = [ 2 , 3 , 8 , - 1 , 7 , 10 ] ; let n = A . length ; mergeTwoHalf ( A , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ;
Chocolate Distribution Problem | arr [ 0. . n - 1 ] represents sizes of packets . m is number of students . Returns minimum difference between maximum and minimum values of distribution . ; If there are no chocolates or number of students is 0 ; Sort the given packets ; Number of students cannot be more than number of packets ; Largest number of chocolates ; Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum . ; Driver code ; Number of students
function findMinDiff ( arr , n , m ) { if ( m == 0 n == 0 ) return 0 ; arr . sort ( function ( a , b ) { return a - b } ) ; if ( n < m ) return - 1 ; let min_diff = Number . MAX_VALUE ; for ( let i = 0 ; i + m - 1 < n ; i ++ ) { let diff = arr [ i + m - 1 ] - arr [ i ] ; if ( diff < min_diff ) min_diff = diff ; } return min_diff ; } let arr = [ 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 ] ; let m = 7 ; let n = arr . length ; document . write ( " " + findMinDiff ( arr , n , m ) ) ;
Absolute distinct count in a sorted array | The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code
function distinctCount ( arr , n ) { let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) s . add ( Math . abs ( arr [ i ] ) ) ; return s . size ; } let arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; let n = arr . length ; document . write ( " " + distinctCount ( arr , n ) ) ;