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
def make_cuts ( arr , n , K ) : NEW_LINE INDENT ans = 0 NEW_LINE poss = [ ] NEW_LINE ce = 0 NEW_LINE co = 0 NEW_LINE for x in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ x ] % 2 == 0 ) : NEW_LINE INDENT ce += 1 NEW_LINE DEDENT else : NEW_LINE INDENT co += 1 NEW_LINE DEDENT if ( ce == co and co > 0 and ce > 0 ) : NEW_LINE INDENT poss . append ( abs ( arr [ x ] - arr [ x + 1 ] ) ) NEW_LINE DEDENT DEDENT poss . sort ( ) NEW_LINE for x in poss : NEW_LINE INDENT if ( K >= x ) : NEW_LINE INDENT ans += 1 NEW_LINE K -= x NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT N = 6 NEW_LINE K = 4 NEW_LINE arr = [ 1 , 2 , 5 , 10 , 15 , 20 ] NEW_LINE print ( make_cuts ( arr , N , K ) ) NEW_LINE