codeparrot/xlcost-text-to-code huggingface dataset:
Minimize maximum array element by splitting array elements into powers of two at most K times | Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver code
function minimumSize ( arr , N , K ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; reverse ( arr ) ; if ( count ( arr , 0 ) == N ) document . write ( 0 ) ; else if ( K >= N ) document . write ( 1 ) ; else document . write ( arr [ K ] ) ; } function reverse ( a ) { let i , k , t ; let n = a . length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } function count ( a , n ) { let freq = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } let arr = [ 2 , 4 , 8 , 2 ] ; let K = 2 ; let N = arr . length ; minimumSize ( arr , N , K ) ;
Check if removal of a subsequence of non | Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver Code
function isPossibleToSort ( arr , N ) { var idx = - 1 ; var i ; for ( i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == - 1 ) { document . write ( " " ) ; return ; } for ( i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var arr = [ 1 , 0 , 1 , 0 , 1 , 1 , 0 ] ; var N = arr . length ; isPossibleToSort ( arr , N ) ;
Maximize ropes of consecutive length possible by connecting given ropes | Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; If size of the current rope is less than or equal to current maximum possible size + 1 , update the range to curSize + ropes [ i ] ; If a rope of size ( curSize + 1 ) cannot be obtained ; Input ; Function Call
function maxConsecutiveRopes ( ropes , N ) { let curSize = 0 ; ropes . sort ( ( a , b ) => a - b ) for ( let i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } let N = 5 ; let ropes = [ 1 , 2 , 7 , 1 , 1 ] ; document . write ( maxConsecutiveRopes ( ropes , N ) ) ;
Maximum ranges that can be uniquely represented by any integer from the range | Function to find the maximum number of ranges where each range can be uniquely represented by an integer ; Sort the ranges in ascending order ; Stores the count of ranges ; Stores previously assigned range ; Traverse the vector arr [ ] ; Skip the similar ranges of size 1 ; Find the range of integer available to be assigned ; Check if an integer is available to be assigned ; Update the previously assigned range ; Update the count of range ; Return the maximum count of ranges ; Driver Code
function maxRanges ( arr , N ) { arr . sort ( ( a , b ) => { return b [ 0 ] - a [ 0 ] } ) let count = 1 ; let prev = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { let last = arr [ i - 1 ] ; let current = arr [ i ] ; if ( last [ 0 ] == current [ 0 ] && last [ 1 ] == current [ 1 ] && current [ 1 ] == current [ 0 ] ) { continue ; } let start = Math . max ( prev [ 0 ] , current [ 0 ] - 1 ) ; let end = Math . max ( prev [ 1 ] , current [ 1 ] ) ; if ( ( end - start ) > 0 ) { prev [ 0 ] = 1 + start ; prev [ 1 ] = end ; count ++ ; } } return count ; } let range = [ [ 1 , 4 ] , [ 4 , 4 ] , [ 2 , 2 ] , [ 3 , 4 ] , [ 1 , 1 ] ] ; let N = range . length ; document . write ( maxRanges ( range , N ) ) ;
Sort a string lexicographically by reversing a substring | JavaScript program for the above approach ; Function to find the substring in S required to be reversed ; Stores the size of the string ; Stores the starting point of the substring ; Iterate over the string S while i < N ; Increment the value of i ; Stores the ending index of the substring ; If start <= 0 or i >= N ; If start >= 1 and i <= N ; Return the boolean value ; If start >= 1 ; Return the boolean value ; If i < N ; Return true if S [ start ] is less than or equal to S [ i ] ; Otherwise ; Function to check if it is possible to sort the string or not ; Stores the starting and the ending index of substring ; Stores whether it is possible to sort the substring ; Traverse the range [ 1 , N ] ; If S [ i ] is less than S [ i - 1 ] ; If flag stores true ; If adjust ( S , i , start , end ) return false ; Print - 1 ; Unset the flag ; Otherwise ; Print - 1 ; If start is equal to - 1 ; Update start and end ; Print the value of start ; Driver code
var i , start , end ; function adjust ( S ) { var N = S . length ; start = i - 1 ; while ( i < N && S [ i ] < S [ i - 1 ] ) { i ++ ; } end = i - 1 ; if ( start <= 0 && i >= N ) return true ; if ( start >= 1 && i <= N ) { return S [ end ] >= S [ start - 1 ] && S [ start ] <= S [ i ] ; } if ( start >= 1 ) { return S [ end ] >= S [ start - 1 ] ; } if ( i < N ) { return S [ start ] <= S [ i ] ; } return false ; } function isPossible ( S , N ) { start = - 1 ; end = - 1 ; var flag = true ; for ( i = 1 ; i < N ; i ++ ) { if ( S [ i ] < S [ i - 1 ] ) { if ( flag ) { if ( adjust ( S ) === false ) { document . write ( - 1 ) ; return ; } flag = false ; } else { document . write ( - 1 ) ; return ; } } } if ( start === - 1 ) { start = end = 1 ; } document . write ( start + " " + end + " " ) ; } var S = " " ; var N = S . length ; isPossible ( S , N ) ;
Split array into K non | Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; S . reverse ( ) ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code
function maximumSum ( arr , S , N , K ) { var ans = 0 ; var i ; arr . sort ( ( a , b ) => b - a ) ; for ( i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; S . sort ( ) ; for ( i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -= 1 ; } var counter = K - 1 ; for ( i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } var arr = [ 1 , 13 , 7 , 17 ] ; var S = [ 1 , 3 ] ; var N = arr . length ; var K = S . length ; document . write ( maximumSum ( arr , S , N , K ) ) ;
Modify an array by sorting after reversal of bits of each array element | Function to convert binary number to equivalent decimal ; Set base value to 1 , i . e 2 ^ 0 ; Function to convert a decimal to equivalent binary representation ; Stores the binary representation ; Since ASCII value of '0' , '1' are 48 and 49 ; As the string is already reversed , no further reversal is required ; Function to convert the reversed binary representation to equivalent integer ; Stores reversed binary representation of given decimal ; Stores equivalent decimal value of the binary representation ; Return the resultant integer ; Utility function to print the sorted array ; Sort the array ; Traverse the array ; Print the array elements ; Utility function to reverse the binary representations of all array elements and sort the modified array ; Traverse the array ; Passing array elements to reversedBinaryDecimal function ; Pass the array to the sorted array ; Driver Code
function binaryToDecimal ( n ) { let num = n ; let dec_value = 0 ; let base = 1 ; let len = num . length ; for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == ' ' ) dec_value += base ; base = base * 2 ; } return dec_value ; } function decimalToBinary ( n ) { let binstr = " " ; while ( n > 0 ) { binstr += String . fromCharCode ( n % 2 + 48 ) ; n = Math . floor ( n / 2 ) ; } return binstr ; } function reversedBinaryDecimal ( N ) { let decimal_to_binar = decimalToBinary ( N ) ; let binary_to_decimal = binaryToDecimal ( decimal_to_binar ) ; return binary_to_decimal ; } function printSortedArray ( arr , size ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) } function modifyArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) { arr [ i ] = reversedBinaryDecimal ( arr [ i ] ) ; } printSortedArray ( arr , size ) ; } let arr = [ 98 , 43 , 66 , 83 ] ; let n = arr . length ; modifyArray ( arr , n ) ;
Check if sequence of removed middle elements from an array is sorted or not | Function to check if sequence of removed middle elements from an array is sorted or not ; Points toa the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
function isSortedArray ( arr , n ) { var l = 0 var r = ( n - 1 ) while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l += 1 r -= 1 } else return false } return true } var arr = [ 4 , 3 , 1 , 2 , 5 ] var N = arr . length if ( isSortedArray ( arr , N ) ) console . log ( " " ) else console . log ( " " )
Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A [ i ] â ‰¥ A [ i + 1 ] | Function to find the length of the longest subset satisfying given conditions ; Sort the array in ascending order ; Stores the starting index and maximum length of the required subset ; Pointer to traverse the array ; Iterate while i < n ; Stores end point of current subset ; Store the length of the current subset ; Continue adding elements to the current subset till the condition satisfies ; Increment length of the current subset ; Increment the pointer j ; If length of the current subset exceeds overall maximum length ; Update maxlen ; Update index ; Increment j ; Update i ; Store the starting index of the required subset in i ; Print the required subset ; Print the array element ; Decrement maxlen ; Increment i ; Given array ; Store the size of the array
function maxLenSubset ( a , n ) { a . sort ( function ( a , b ) { return a - b ; } ) let index = 0 , maxlen = - 1 ; let i = 0 ; while ( i < n ) { let j = i ; let len = 1 ; while ( j < n - 1 ) { if ( 2 * a [ j ] >= a [ j + 1 ] ) { len ++ ; } else break ; j ++ ; } if ( maxlen < len ) { maxlen = len ; index = i ; } j ++ ; i = j ; } i = index ; while ( maxlen > 0 ) { document . write ( a [ i ] + " " ) maxlen -- ; i ++ ; } } let a = [ 3 , 1 , 5 , 11 ] ; let n = a . length maxLenSubset ( a , n )
Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driver Code ; Function Call to check if an array can be reduced to a single element
function canReduceArray ( arr , N , K ) { arr . sort ( ) ; let b = Array ( N ) . fill ( 0 ) ; let j = N ; for ( let i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let arr = [ 2 , 1 , 1 , 3 ] ; let N = arr . length ; let K = 1 ; canReduceArray ( arr , N , K ) ;
Lexicographically smallest string possible by merging two sorted strings | Function to find lexicographically smallest string possible by merging two sorted strings ; Stores length of string s1 ; Stores length of string s2 ; Pointer to beginning of string1 i . e . , s1 ; Pointer to beginning of string2 i . e . , s2 ; Stores the final string ; Traverse the strings ; Append the smaller of the two current characters ; Append the remaining characters of any of the two strings ; Print the final string ; Driver Code ; Function Call
function mergeStrings ( s1 , s2 ) { var len1 = s1 . length ; var len2 = s2 . length ; var pntr1 = 0 ; var pntr2 = 0 ; var ans = " " ; while ( pntr1 < len1 && pntr2 < len2 ) { if ( s1 [ pntr1 ] < s2 [ pntr2 ] ) { ans += s1 [ pntr1 ] ; pntr1 ++ ; } else { ans += s2 [ pntr2 ] ; pntr2 ++ ; } } if ( pntr1 < len1 ) { ans += s1 . substr ( pntr1 , len1 ) ; } if ( pntr2 < len2 ) { ans += s2 . substr ( pntr2 , len2 ) ; } document . write ( ans ) ; } var S1 = " " ; var S2 = " " ; mergeStrings ( S1 , S2 ) ;
Sort a string without altering the position of vowels | Function to sort the string leaving the vowels unchanged ; Length of string S ; Traverse the string S ; Sort the string temp ; Pointer to traverse the sorted string of consonants ; Traverse the string S ; Driver Code
function sortStr ( str ) { var S = str . split ( ' ' ) ; var N = S . length ; var temp = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' ) temp . push ( S [ i ] ) ; } if ( temp . length != 0 ) temp . sort ( ) ; var ptr = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' && S [ i ] != ' ' ) S [ i ] = temp [ ptr ++ ] ; } var str = " " ; for ( var i = 0 ; i < S . length ; i ++ ) str += S [ i ] ; document . write ( str ) ; } var S = " " ; sortStr ( S ) ;
Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K | Javascript program for the above approach ; Function to count the number having quotient of division of larger element by the smaller element in the pair not exceeding K ; Sort the array in ascending order ; Store the required result ; Traverse the array ; Store the upper bound for the current array element ; Update the number of pairs ; Print the result ; Given array , arr [ ] ; Store the size of the array
function upper_bound ( arr , key ) { let l = - 1 , r = arr . length ; while ( l + 1 < r ) { let m = ( l + r ) >>> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; } function countPairs ( arr , n , k ) { arr . sort ( ) ; let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let high = upper_bound ( arr , k * arr [ i ] ) ; ans += high - i - 1 ; } document . write ( ans ) ; } let arr = [ 2 , 3 , 9 , 5 ] ; let n = arr . length ; let k = 2 ; countPairs ( arr , n , k ) ;
Kth highest XOR of diagonal elements from a Matrix | Function to find K - th maximum XOR of any diagonal in the matrix ; Number or rows ; Number of columns ; Store XOR of diagonals ; Traverse each diagonal ; Starting column of diagonal ; Count total elements in the diagonal ; Store XOR of current diagonal ; Push XOR of current diagonal ; Sort XOR values of diagonals ; Print the K - th Maximum XOR ; Driver Code
function findXOR ( mat , K ) { let N = mat . length ; let M = mat [ 0 ] . length ; let digXOR = [ ] ; for ( let l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { let s_col = Math . max ( 0 , l - N ) ; let count = Math . min ( l , ( M - s_col ) , N ) ; let currXOR = 0 ; for ( let j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ Math . min ( N , l ) - j - 1 ] [ s_col + j ] ) ; } digXOR . push ( currXOR ) ; } digXOR . sort ( ( a , b ) => a - b ) ; document . write ( digXOR [ N + M - 1 - K ] ) ; } let mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; let K = 3 ; findXOR ( mat , K ) ;
Maximum sum of absolute differences between distinct pairs of a triplet from an array | Function to find the maximum sum of absolute differences between distinct pairs of triplet in array ; Stores the maximum sum ; Sort the array in ascending order ; Sum of differences between pairs of the triplet ; Print the sum ; Driver Code
function maximumSum ( arr , N ) { let sum ; arr . sort ( ) ; sum = ( arr [ N - 1 ] - arr [ 0 ] ) + ( arr [ N - 2 ] - arr [ 0 ] ) + ( arr [ N - 1 ] - arr [ N - 2 ] ) ; document . write ( sum ) ; } let arr = [ 1 , 3 , 4 , 2 ] ; let N = arr . length ; maximumSum ( arr , N ) ;
Lexicographically smallest permutation having maximum sum of differences between adjacent elements | Function to find the lexicographically smallest permutation of an array such that the sum of the difference between adjacent elements is maximum ; Stores the size of the array ; Sort the given array in increasing order ; Swap the first and last array elements ; Prvar the required permutation ; Driver Code
function maximumSumPermutation ( arr ) { var N = arr . length ; arr . sort ( ( a , b ) => a - b ) ; var temp = arr [ 0 ] ; arr [ 0 ] = arr [ N - 1 ] ; arr [ N - 1 ] = temp ; document . write ( arr ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; maximumSumPermutation ( arr ) ;
Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Print the answer ; Driver Code
function findMaxNumbers ( arr , n , k ) { arr . sort ( ) ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let opr = Math . ceil ( Math . log2 ( arr [ i ] ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } document . write ( cnt ) ; } let arr = [ 5 , 8 , 4 , 7 ] ; let N = arr . length ; let K = 5 ; findMaxNumbers ( arr , N , K ) ;
Mean of given array after removal of K percent of smallest and largest array elements | Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
function meanOfRemainingElements ( arr , N , K ) { arr . sort ( function ( a , b ) { return a - b } ) ; let kthPercent = Math . floor ( ( N * K ) / 100 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; let mean = sum / ( N - 2 * kthPercent ) ; document . write ( mean . toFixed ( 5 ) ) ; } let arr = [ 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 ] ; let arr_size = arr . length ; let K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ;
Make the array elements equal by performing given operations minimum number of times | Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Given array ; Size of the array
function minOperation ( a , N ) { var totOps = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { totOps += Math . abs ( a [ i ] - a [ i + 1 ] ) ; } var maxOps = Math . max ( Math . abs ( a [ 0 ] - a [ 1 ] ) , Math . abs ( a [ N - 1 ] - a [ N - 2 ] ) ) ; for ( i = 1 ; i < N - 1 ; i ++ ) { maxOps = Math . max ( maxOps , Math . abs ( a [ i ] - a [ i - 1 ] ) + Math . abs ( a [ i ] - a [ i + 1 ] ) - Math . abs ( a [ i - 1 ] - a [ i + 1 ] ) ) ; } document . write ( totOps - maxOps ) ; } var arr = [ 1 , - 1 , 0 , 1 , 1 ] ; var N = arr . length ; minOperation ( arr , N ) ;
Find the segment that overlaps with maximum number of segments | JavaScript program for the above approach ; Function to find the segment which overlaps with maximum number of segments ; ' L ' & ' R ' co - ordinates of all segments are stored in lvalues & rvalues ; Assign co - ordinates ; Co - ordinate compression ; Stores the required segment ; Stores the current maximum number of intersections ; Find number of ' R ' coordinates which are less than the ' L ' value of the current segment ; Find number of ' L ' coordinates which are greater than the ' R ' value of the current segment ; Segments excluding ' lesser ' and ' greater ' gives the number of intersections ; Update the current maximum ; Print segment coordinates ; Given segments ; Size of segments array
function lower_bound ( a , low , high , element ) { while ( low < high ) { var middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { var middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } function maxIntersection ( segments , N ) { let rvalues = Array ( N ) . fill ( 0 ) ; let lvalues = Array ( N ) . fill ( 0 ) ; for ( i = 0 ; i < N ; ++ i ) { lvalues [ i ] = segments [ i ] [ 0 ] ; rvalues [ i ] = segments [ i ] [ 1 ] ; } lvalues . sort ( ( a , b ) => a - b ) ; rvalues . sort ( ( a , b ) => a - b ) ; let answer = [ - 1 , - 1 ] ; var numIntersections = 0 ; for ( var i = 0 ; i < N ; ++ i ) { var lesser = lower_bound ( rvalues , 0 , segments . length , segments [ i ] [ 0 ] ) ; var greater = Math . max ( 0 , N - ( upper_bound ( lvalues , 0 , segments . length , segments [ i ] [ 1 ] ) ) ) ; if ( ( N - lesser - greater ) >= numIntersections ) { answer = [ segments [ i ] [ 0 ] , segments [ i ] [ 1 ] ] ; numIntersections = ( N - lesser - greater ) ; } } document . write ( answer [ 0 ] + " " + answer [ 1 ] ) ; } var segments = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 3 , 6 ] ] ; var N = segments . length ; maxIntersection ( segments , N ) ;
Count swaps required to sort an array using Insertion Sort | Stores the sorted array elements ; Function to count the number of swaps required to merge two sorted subarray in a sorted form ; Stores the count of swaps ; Function to count the total number of swaps required to sort the array ; Stores the total count of swaps required ; Find the middle index splitting the two halves ; Count the number of swaps required to sort the left subarray ; Count the number of swaps required to sort the right subarray ; Count the number of swaps required to sort the two sorted subarrays ; Driver Code
let temp = new Array ( 100000 ) . fill ( 0 ) ; function merge ( A , left , mid , right ) { let swaps = 0 ; let i = left , j = mid , k = left ; while ( i < mid && j <= right ) { if ( A [ i ] <= A [ j ] ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } else { temp [ k ] = A [ j ] ; k ++ ; j ++ ; swaps += mid - i ; } } while ( i < mid ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } while ( j <= right ) { temp [ k ] = A [ j ] ; k ++ ; j ++ ; } while ( left <= right ) { A [ left ] = temp [ left ] ; left ++ ; } return swaps ; } function mergeInsertionSwap ( A , left , right ) { let swaps = 0 ; if ( left < right ) { let mid = left + ( right - left ) / 2 ; swaps += mergeInsertionSwap ( A , left , mid ) ; swaps += mergeInsertionSwap ( A , mid + 1 , right ) ; swaps += merge ( A , left , mid + 1 , right ) ; } return swaps ; } let A = [ 2 , 1 , 3 , 1 , 2 ] ; let N = A . length ; document . write ( mergeInsertionSwap ( A , 0 , N - 1 ) ) ;