Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Given array and K ; Function Call
function make_cuts ( arr , n , K ) { var ans = 0 ; var poss = [ ] ; var ce = 0 ; var co = 0 ; var x ; for ( x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . push ( Math . abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } poss . sort ( ) ; var i ; for ( i = 0 ; i < poss . length ; i ++ ) { if ( K >= poss [ i ] ) { ans ++ ; K -= poss [ i ] ; } else break ; } return ans ; } var N = 6 ; var K = 4 ; var arr = [ 1 , 2 , 5 , 10 , 15 , 20 ] ; document . write ( make_cuts ( arr , N , K ) ) ;