Min steps to empty an Array by removing a pair each time with sum at most K | Function to count minimum steps ; Function to sort the array ; Run while loop ; Condition to check whether sum exceed the target or not ; Increment the step by 1 ; Return minimum steps ; Given array arr [ ] ; Given target value ; Function call
def countMinSteps ( arr , target , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE minimumSteps = 0 NEW_LINE i , j = 0 , n - 1 NEW_LINE while i <= j : NEW_LINE INDENT if arr [ i ] + arr [ j ] <= target : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT minimumSteps += 1 NEW_LINE DEDENT return minimumSteps NEW_LINE DEDENT arr = [ 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 ] NEW_LINE target = 11 NEW_LINE size = len ( arr ) NEW_LINE print ( countMinSteps ( arr , target , size ) ) NEW_LINE