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
function countMinSteps ( arr , target , n ) { arr = arr . sort ( function ( a , b ) { return a - b ; } ) ; var minimumSteps = 0 ; var i = 0 , j = n - 1 ; while ( i <= j ) { if ( arr [ i ] + arr [ j ] <= target ) { i ++ ; j -- ; } else { j -- ; } minimumSteps ++ ; } return minimumSteps ; } var arr = [ 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 ] ; var target = 11 ; var size = arr . length ; document . write ( countMinSteps ( arr , target , size ) ) ;