codeparrot/xlcost-text-to-code huggingface dataset:
Maximize count of subsets into which the given array can be split such that it satisfies the given condition | Function to count maximum subsets into which the given array can be split such that it satisfies the given condition ; Sort the array in decreasing order ; Stores count of subsets possible ; Stores count of elements in current subset ; Traverse the array arr [ ] ; Update size ; If product of the smallest element present in the current subset and size of current subset is >= K ; Update maxSub ; Update size ; Given array ; Size of the array ; Given value of X
function maxDivisions ( arr , N , X ) { arr . sort ( ) ; let maxSub = 0 ; let size = 0 ; for ( let i = 0 ; i < N ; i ++ ) { size ++ ; if ( arr [ i ] * size >= X ) { maxSub ++ ; size = 0 ; } } document . write ( maxSub + " " ) ; } let arr = [ 1 , 3 , 3 , 7 ] ; let N = arr . length ; let X = 3 ; maxDivisions ( arr , N , X ) ;
Maximize sum of second minimums in all quadruples of a given array | Function to find maximum possible sum of second minimums in each quadruple ; Sort the array ; Add the second minimum ; Prlet maximum possible sum ; Given array ; Size of the array
function maxPossibleSum ( arr , N ) { arr . sort ( ) ; let sum = 0 ; let j = N - 3 ; while ( j >= 0 ) { sum += arr [ j ] ; j -= 3 ; } document . write ( sum ) ; } let arr = [ 7 , 4 , 5 , 2 , 3 , 1 , 5 , 9 ] ; let N = arr . length ; maxPossibleSum ( arr , N ) ;
Difference between Insertion sort and Selection sort | Function to sort an array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key to one position ahead of their current position ; Function to print an array of size N ; Print the array ; Driver code ; Function Call
function insertionSort ( arr , n ) { let i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } function printArray ( arr , n ) { let i ; for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 ] ; let N = arr . length ; insertionSort ( arr , N ) ; printArray ( arr , N ) ;
Count pairs ( i , j ) from given array such that i K * arr [ j ] | Function to find the count required pairs ; Stores count of pairs ; Traverse the array ; Check if the condition is satisfied or not ; Driver Code ; Function Call
function getPairs ( arr , N , K ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > K * arr [ i + 1 ] ) count ++ ; } } document . write ( count ) ; } let arr = [ 5 , 6 , 2 , 1 ] ; let N = arr . length ; let K = 2 ; getPairs ( arr , N , K ) ;
Count pairs ( i , j ) from given array such that i K * arr [ j ] | Function to merge two sorted arrays ; i : index to left subarray ; j : index to right subarray ; Stores count of pairs that satisfy the given condition ; Traverse to check for the valid conditions ; If condition satisfies ; All elements in the right side of the left subarray also satisfies ; Sort the two given arrays and store in the resultant array ; Elements which are left in the left subarray ; Elements which are left in the right subarray ; Return the count obtained ; Function to partition array into two halves ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; Call the merging function ; Function to print the count of required pairs using Merge Sort ; Driver code ; Function Call
function merge ( arr , temp , l , m , r , K ) { let i = l ; let j = m + 1 ; let cnt = 0 ; for ( i = l ; i <= m ; i ++ ) { let found = false ; while ( j <= r ) { if ( arr [ i ] >= K * arr [ j ] ) { found = true ; } else break ; j ++ ; } if ( found == true ) { cnt += j - ( m + 1 ) ; j -- ; } } let k = l ; i = l ; j = m + 1 ; while ( i <= m && j <= r ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else temp [ k ++ ] = arr [ j ++ ] ; } while ( i <= m ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= r ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = l ; i <= r ; i ++ ) arr [ i ] = temp [ i ] ; return cnt ; } function mergeSortUtil ( arr , temp , l , r , K ) { let cnt = 0 ; if ( l < r ) { let m = parseInt ( ( l + r ) / 2 , 10 ) ; cnt += mergeSortUtil ( arr , temp , l , m , K ) ; cnt += mergeSortUtil ( arr , temp , m + 1 , r , K ) ; cnt += merge ( arr , temp , l , m , r , K ) ; } return cnt ; } function mergeSort ( arr , N , K ) { let temp = new Array ( N ) ; document . write ( mergeSortUtil ( arr , temp , 0 , N - 1 , K ) ) ; } let arr = [ 5 , 6 , 2 , 5 ] ; let N = arr . length ; let K = 2 ; mergeSort ( arr , N , K ) ;
Minimize consecutive removals of elements of the same type to empty given array | Function to count minimum consecutive removals of elements of the same type ; Sort the array ; Stores the maximum element present in the array ; Stores sum of the array ; Calculate sum of the array ; Driver Code ; Function call
function minRemovals ( A , N ) { A . sort ( ) ; let mx = A [ N - 1 ] ; let sum = 1 ; for ( let i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; } if ( sum - mx >= mx ) { document . write ( 0 ) ; } else { document . write ( 2 * mx - sum ) ; } } let A = [ 3 , 3 , 2 ] ; let N = A . length ; minRemovals ( A , N ) ;
Rearrange given array such that no array element is same as its index | Function to rearrange the array a [ ] such that none of the array elements is same as its index ; Sort the array ; Traverse the indices [ 0 , N - 2 ] of the given array ; Check if the current element is equal to its index ; If found to be true , swap current element with the next element ; Check if the last element is same as its index ; If found to be true , swap current element with the previous element ; Prlet the modified array ; Driver code ; Function Call
function rearrangeArray ( a , n ) { a . sort ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] == i + 1 ) { let temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; } } if ( a [ n - 1 ] == n ) { let temp = a [ n - 1 ] ; a [ n - 1 ] = a [ n - 2 ] ; a [ n - 2 ] = temp ; } for ( let i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let arr = [ 1 , 5 , 3 , 2 , 4 ] ; let N = arr . length ; rearrangeArray ( arr , N ) ;
Count minimum number of moves to front or end to sort an array | Function that counts the minimum moves required to covert arr [ ] to brr [ ] ; Base Case ; If arr [ i ] < arr [ j ] ; Include the current element ; Otherwise , excluding the current element ; Function that counts the minimum moves required to sort the array ; If both the arrays are equal ; No moves required ; Otherwise ; Print minimum operations required ; Driver code
function minOperations ( arr1 , arr2 , i , j , n ) { let f = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) f = 1 ; break ; } if ( f == 0 ) return 0 ; if ( i >= n j >= n ) return 0 ; if ( arr1 [ i ] < arr2 [ j ] ) return 1 + minOperations ( arr1 , arr2 , i + 1 , j + 1 , n ) ; return Math . max ( minOperations ( arr1 , arr2 , i , j + 1 , n ) , minOperations ( arr1 , arr2 , i + 1 , j , n ) ) ; } function minOperationsUtil ( arr , n ) { let brr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; brr . sort ( ) ; let f = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != brr [ i ] ) f = 1 ; break ; } if ( f == 1 ) document . write ( minOperations ( arr , brr , 0 , 0 , n ) ) ; else cout << " " ; } let arr = [ 4 , 7 , 2 , 3 , 9 ] ; let n = arr . length ; minOperationsUtil ( arr , n ) ;
Check if a string can be transformed to another by sorting substrings | Function to check if str1 can be transformed to t by sorting substrings ; Occur [ i ] stores the indices of char ( ' a ' + i ) in string s ; idx [ i ] stores the next available index of char ( ' a ' + i ) in occur [ i ] ; If this char is not available anymore ; Conversion not possible ; If one of the smaller characters is available and occurs before ; Conversion not possible ; Print the answer ; Driver Code
function canTransform ( s , t ) { var n = s . length ; var occur = Array . from ( Array ( 26 ) , ( ) => new Array ( ) ) ; for ( var x = 0 ; x < n ; x ++ ) { var ch = s [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; occur [ ch ] . push ( x ) ; } var idx = Array ( 26 ) . fill ( 0 ) ; var poss = true ; for ( var x = 0 ; x < n ; x ++ ) { var ch = t [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( idx [ ch ] >= occur [ ch ] . length ) { poss = false ; break ; } for ( var small = 0 ; small < ch ; small ++ ) { if ( idx [ small ] < occur [ small ] . length && occur [ small ] [ idx [ small ] ] < occur [ ch ] [ idx [ ch ] ] ) { poss = false ; break ; } } idx [ ch ] ++ ; } if ( poss ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var s , t ; s = " " ; t = " " ; canTransform ( s , t ) ;
Check whether two strings can be made equal by reversing substring of equal length from both strings | function to count inversion count of the string ; For storing frequency ; Add all the characters which are less than the ith character before i . ; Adding the count to inversion count ; Updating the character in the frequency array ; Function to check whether any of the string have a repeated character ; Function to check whether the string S1 and S2 can be made equal by reversing sub strings of same size in both strings ; Frequency array to check whether both string have same character or not ; Adding the frequency ; ; If the character is not in S1 ; Decrementing the frequency ; If both string doesnot have same characters or not ; Finding inversion count of both strings ; If inversion count is same , or have same parity or if any of the string have a repeated character then the answer is Yes else No ; Driver code
function inversionCount ( s ) { var freq = Array ( 26 ) . fill ( 0 ) ; var inv = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { var temp = 0 ; for ( var j = 0 ; j < String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; j ++ ) temp += freq [ j ] ; inv += ( i - temp ) ; freq [ s [ i ] - ' ' ] ++ ; } return inv ; } function haveRepeated ( S1 , S2 ) { var freq = Array ( 26 ) . fill ( 0 ) ; S1 . forEach ( i => { if ( freq [ i - ' ' ] > 0 ) return true ; freq [ i - ' ' ] ++ ; } ) ; for ( var i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; S2 . split ( ' ' ) . forEach ( i => { if ( freq [ i - ' ' ] > 0 ) return true ; freq [ i - ' ' ] ++ ; } ) ; return false ; } function checkToMakeEqual ( S1 , S2 ) { var freq = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < S1 . length ; i ++ ) { freq [ S1 [ i ] - ' ' ] ++ ; } var flag = 0 ; for ( var i = 0 ; i < S2 . length ; i ++ ) { if ( freq [ S2 [ i ] - ' ' ] == 0 ) { flag = true ; break ; } freq [ S2 [ i ] - ' ' ] -- ; } if ( flag == true ) { document . write ( " " ) ; return ; } var invCount1 = inversionCount ( S1 ) ; var invCount2 = inversionCount ( S2 ) ; if ( invCount1 == invCount2 || ( invCount1 & 1 ) == ( invCount2 & 1 ) || haveRepeated ( S1 , S2 ) ) { document . write ( " " ) ; } else document . write ( " " ) ; } var S1 = " " , S2 = " " ; checkToMakeEqual ( S1 , S2 ) ;
Sort a Bitonic Array | Function to Sort a Bitonic array in constant space ; Initialize the value of k ; In each iteration compare elements k distance apart and swap if they are not in order ; k is reduced to half after every iteration ; Print the array elements ; Given array arr [ ] ; Function call
function sortArr ( a , n ) { let i , k ; k = parseInt ( Math . log ( n ) / Math . log ( 2 ) ) ; k = parseInt ( Math . pow ( 2 , k ) ) ; while ( k > 0 ) { for ( i = 0 ; i + k < n ; i ++ ) if ( a [ i ] > a [ i + k ] ) { let tmp = a [ i ] ; a [ i ] = a [ i + k ] ; a [ i + k ] = tmp ; } k = k / 2 ; } for ( i = 0 ; i < n ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let arr = [ 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 ] ; let n = arr . length ; sortArr ( arr , n ) ;
Split array into K subsets to maximize their sum of maximums and minimums | Function that prlets the maximum sum possible ; Find elements in each group ; Sort all elements in non - descending order ; Add K largest elements ; For sum of minimum elements from each subset ; Prleting the maximum sum ; Driver Code
function maximumSum ( arr , n , k ) { let elt = ( n / k ) ; let sum = 0 ; arr . sort ( ( a , b ) => a - b ) ; let count = 0 ; let i = n - 1 ; while ( count < k ) { sum += arr [ i ] ; i -- ; count ++ ; } count = 0 ; i = 0 ; while ( count < k ) { sum += arr [ i ] ; i += elt - 1 ; count ++ ; } document . write ( sum ) ; } let Arr = [ 1 , 13 , 7 , 17 , 6 , 5 ] ; let K = 2 ; let size = Arr . length ; maximumSum ( Arr , size , K ) ;
Minimize sum of smallest elements from K subsequences of length L | Function to find the minimum sum ; Sort the array ; Calculate sum of smallest K elements ; Return the sum ; Driver Code
function findMinSum ( arr , K , L , size ) { if ( K * L > size ) return - 1 ; let minsum = 0 ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < K ; i ++ ) minsum += arr [ i ] ; return minsum ; } let arr = [ 2 , 15 , 5 , 1 , 35 , 16 , 67 , 10 ] ; let K = 3 ; let L = 2 ; let length = arr . length ; document . write ( findMinSum ( arr , K , L , length ) ) ;
Kth smallest or largest element in unsorted Array | Set 4 | Function to find the Kth smallest element in Unsorted Array ; Initialize the max Element as 0 ; Iterate arr [ ] and find the maximum element in it ; Frequency array to store the frequencies ; Counter variable ; Counting the frequencies ; Iterate through the freq [ ] ; Check if num is present in the array ; Increment the counter with the frequency of num ; Checking if we have reached the Kth smallest element ; Return the Kth smallest element ; Given array ; Function call
function findKthSmallest ( arr , n , k ) { let max = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } let counter = Array . from ( { length : max + 1 } , ( _ , i ) => 0 ) ; let smallest = 0 ; for ( let i = 0 ; i < n ; i ++ ) { counter [ arr [ i ] ] ++ ; } for ( let num = 1 ; num <= max ; num ++ ) { if ( counter [ num ] > 0 ) { smallest += counter [ num ] ; } if ( smallest >= k ) { return num ; } } return - 1 ; } let arr = [ 7 , 1 , 4 , 4 , 20 , 15 , 8 ] ; let N = arr . length ; let K = 5 ; document . write ( findKthSmallest ( arr , N , K ) ) ;
Generate all numbers up to N in Lexicographical Order | Function to print all the numbers up to n in lexicographical order ; Driver Program
function lexNumbers ( n ) { let s = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) { s . push ( i . toString ( ) ) ; } s . sort ( ) ; let ans = [ ] ; for ( let i = 0 ; i < n ; i ++ ) ans . push ( parseInt ( s [ i ] ) ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( ans [ i ] + " " ) ; } let n = 15 ; lexNumbers ( n ) ;
Sort Matrix in alternating ascending and descending order rowwise | Javascript implementation to print row of matrix in ascending or descending order alternatively ; Iterate matrix rowwise ; Sort even rows in ascending order ; compare adjacent elements ; swap adjacent element ; Sort even rows in descending order ; compare adjacent elements ; swap adjacent element ; Printing the final Output ; Driver code
let N = 4 ; function func ( a ) { for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { for ( let j = 0 ; j < N ; j ++ ) { for ( let k = j + 1 ; k < N ; ++ k ) { if ( a [ i ] [ j ] > a [ i ] [ k ] ) { let temp = a [ i ] [ j ] ; a [ i ] [ j ] = a [ i ] [ k ] ; a [ i ] [ k ] = temp ; } } } } else { for ( let j = 0 ; j < N ; j ++ ) { for ( let k = j + 1 ; k < N ; ++ k ) { if ( a [ i ] [ j ] < a [ i ] [ k ] ) { let temp = a [ i ] [ j ] ; a [ i ] [ j ] = a [ i ] [ k ] ; a [ i ] [ k ] = temp ; } } } } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { document . write ( " " + a [ i ] [ j ] ) ; } document . write ( " " ) ; } } let a = [ [ 5 , 7 , 3 , 4 ] , [ 9 , 5 , 8 , 2 ] , [ 6 , 3 , 8 , 1 ] , [ 5 , 8 , 9 , 3 ] ] ; func ( a ) ;
Find weight of MST in a complete graph with edge | To store the edges of the given graph ; A utility function to perform DFS Traversal ; Check those vertices which are stored in the set ; Vertices are included if the weight of edge is 0 ; A utility function to find the weight of Minimum Spanning Tree ; To count the connected components ; Inserting the initial vertices in the set ; Traversing vertices stored in the set and Run DFS Traversal for each vertices ; Incrementing the zero weight connected components ; DFS Traversal for every vertex remove ; Driver 's Code ; Insert edges ; Function call find the weight of Minimum Spanning Tree
let g = new Array ( 200005 ) ; for ( let i = 0 ; i < 200005 ; i ++ ) g [ i ] = new Map ( ) ; let s = new Set ( ) ; let ns = new Set ( ) ; function dfs ( x ) { let v = [ ] ; for ( let it of s . values ( ) ) { if ( g [ x ] . get ( it ) != null ) { v . push ( it ) ; } else { ns . add ( it ) ; } } s = ns ; for ( let i of v . values ( ) ) { dfs ( i ) ; } } function weightOfMST ( N ) { let cnt = 0 ; for ( let i = 1 ; i <= N ; ++ i ) { s . add ( i ) ; } let qt = [ ] for ( let t of s . values ( ) ) qt . push ( t ) ; while ( qt . length != 0 ) { ++ cnt ; let t = qt [ 0 ] ; qt . shift ( ) ; dfs ( t ) ; } document . write ( cnt - 4 ) ; } let N = 6 , M = 11 ; let edges = [ [ 1 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 1 , 6 ] , [ 2 , 3 ] , [ 2 , 4 ] , [ 2 , 5 ] , [ 2 , 6 ] , [ 3 , 4 ] , [ 3 , 5 ] , [ 3 , 6 ] ] ; for ( let i = 0 ; i < M ; ++ i ) { let u = edges [ i ] [ 0 ] ; let v = edges [ i ] [ 1 ] ; g [ u ] . set ( v , 1 ) ; g [ v ] . set ( u , 1 ) ; } weightOfMST ( N ) ;
Count of distinct possible pairs such that the element from A is greater than the element from B | Function to return the count of pairs ; Driver Code
function countPairs ( A , B ) { let n = A . length ; let ans = 0 ; A . sort ( ) ; B . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( A [ i ] > B [ ans ] ) { ans ++ ; } } return ans ; } let A = [ 30 , 28 , 45 , 22 ] ; let B = [ 35 , 25 , 22 , 48 ] ; document . write ( countPairs ( A , B ) ) ;
Maximum possible remainder when an element is divided by other element in the array | Function to return the maximum mod value for any pair from the array ; Find the second maximum element from the array ; Driver code
function maxMod ( arr , n ) { let maxVal = arr . sort ( ( a , b ) => b - a ) [ 0 ] let secondMax = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < maxVal && arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return secondMax ; } let arr = [ 2 , 4 , 1 , 5 , 3 , 6 ] ; let n = arr . length ; document . write ( maxMod ( arr , n ) ) ;
Choose X elements from A [ ] and Y elements from B [ ] which satisfy the given condition | Function to that returns true if it possible to choose the elements ; If elements can 't be chosen ; Sort both the arrays ; If xth smallest element of A is smaller than the yth greatest element of B ; Driver code
function isPossible ( A , B , n , m , x , y ) { if ( x > n y > m ) return false ; A . sort ( ) ; B . sort ( ) ; if ( A [ x - 1 ] < B [ m - y ] ) return true ; else return false ; } var A = [ 1 , 1 , 1 , 1 , 1 ] ; var B = [ 2 , 2 ] ; var n = A . length ; var m = B . length ; ; var x = 3 , y = 1 ; if ( isPossible ( A , B , n , m , x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find minimum changes required in an array for it to contain k distinct elements | Javascript program to minimum changes required in an array for k distinct elements . ; Function to minimum changes required in an array for k distinct elements . ; Store the frequency of each element ; Store the frequency of elements ; Sort frequencies in descending order ; To store the required answer ; Return the required answer ; Driver code
var MAX = 100005 ; function Min_Replace ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) var freq = Array ( MAX ) . fill ( 0 ) ; var p = 0 ; freq [ p ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) ++ freq [ p ] ; else ++ freq [ ++ p ] ; } freq . sort ( ( a , b ) => b - a ) ; var ans = 0 ; for ( var i = k ; i <= p ; i ++ ) ans += freq [ i ] ; return ans ; } var arr = [ 1 , 2 , 7 , 8 , 2 , 3 , 2 , 3 ] ; var n = arr . length ; var k = 2 ; document . write ( Min_Replace ( arr , n , k ) ) ;
Maximum number of elements without overlapping in a Line | Function to find maximum number of elements without overlapping in a line ; If n = 1 , then answer is one ; We can always make 1 st element to cover left segment and nth the right segment ; If left segment for ith element doesnt overlap with i - 1 th element then do left ; else try towards right if possible ; update x [ i ] to right endpolet of segment covered by it ; Return the required answer ; Driver code ; Function call
function Segment ( x , l , n ) { if ( n == 1 ) return 1 ; let ans = 2 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( x [ i ] - l [ i ] > x [ i - 1 ] ) ans ++ ; else if ( x [ i ] + l [ i ] < x [ i + 1 ] ) { x [ i ] = x [ i ] + l [ i ] ; ans ++ ; } } return ans ; } let x = [ 1 , 3 , 4 , 5 , 8 ] , l = [ 10 , 1 , 2 , 2 , 5 ] ; let n = x . length ; document . write ( Segment ( x , l , n ) ) ;
Delete odd and even numbers at alternate step such that sum of remaining elements is minimized | Function to find the minimized sum ; If more odd elements ; Sort the elements ; Left - over elements ; Find the sum of leftover elements ; Return the sum ; If more even elements ; Sort the elements ; Left - over elements ; Find the sum of leftover elements ; Return the sum ; If same elements ; Driver code
function MinimizeleftOverSum ( a , n ) { var v1 = [ ] , v2 = [ ] ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) v1 . push ( a [ i ] ) ; else v2 . push ( a [ i ] ) ; } if ( v1 . length > v2 . length ) { v1 . sort ( ) ; v2 . sort ( ) ; var x = v1 . length - v2 . length - 1 ; var sum = 0 ; var i = 0 ; while ( i < x ) { sum += v1 [ i ++ ] ; } return sum ; } else if ( v2 . length > v1 . length ) { v1 . sort ( ) ; v2 . sort ( ) ; var x = v2 . length - v1 . length - 1 ; var sum = 0 ; var i = 0 ; while ( i < x ) { sum += v2 [ i ++ ] ; } return sum ; } else return 0 ; } var a = [ 2 , 2 , 2 , 2 ] ; var n = a . length ; document . write ( MinimizeleftOverSum ( a , n ) ) ;
Minimum operations to make frequency of all characters equal K | Function to find the minimum number of operations to convert the given string ; Check if N is divisible by K ; Array to store frequency of characters in given string ; Two arrays with number of operations required ; Checking for all possibility ; Driver Code
function minOperation ( S , N , K ) { if ( N % K ) { document . write ( " " ) ; return ; } var count = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { count [ S [ i ] . charCodeAt ( 0 ) - 97 ] ++ ; } var E = N / K ; var greaterE = [ ] ; var lessE = [ ] ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( count [ i ] < E ) lessE . push ( E - count [ i ] ) ; else greaterE . push ( count [ i ] - E ) ; } greaterE . sort ( ) ; lessE . sort ( ) ; var mi = 1000000000 ; for ( var i = 0 ; i <= K ; i ++ ) { var set1 = i ; var set2 = K - i ; if ( greaterE . length >= set1 && lessE . length >= set2 ) { var step1 = 0 ; var step2 = 0 ; for ( var j = 0 ; j < set1 ; j ++ ) step1 += greaterE [ j ] ; for ( var j = 0 ; j < set2 ; j ++ ) step2 += lessE [ j ] ; mi = Math . min ( mi , Math . max ( step1 , step2 ) ) ; } } document . write ( mi ) ; } var S = " " ; var N = S . length ; var K = 2 ; minOperation ( S , N , K ) ;
Minimum range increment operations to Sort an array | Function to find minimum range increments to sort an array ; If current element is found greater than last element Increment all terms in range i + 1 to n - 1 ; Minimum in range i to n - 1 ; Driver Code
function minMovesToSort ( arr , n ) { var moves = 0 ; var i , mn = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > mn ) moves += arr [ i ] - mn ; mn = arr [ i ] ; } return moves ; } var arr = [ 3 , 5 , 2 , 8 , 4 ] ; var n = arr . length ; document . write ( minMovesToSort ( arr , n ) ) ;