Min steps to empty an Array by removing a pair each time with sum at most K | Java program implementation of the above approach ; 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 ; Driver code ; Given target value ; Print the minimum flip
import java . util . * ; import java . io . * ; class GFG { static int countMinSteps ( int arr [ ] , int target , int n ) { Arrays . sort ( arr ) ; int minimumSteps = 0 ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( arr [ i ] + arr [ j ] <= target ) { i += 1 ; j -= 1 ; } else { j -= 1 ; } minimumSteps += 1 ; } return minimumSteps ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 } ; int target = 11 ; int size = arr . length ; System . out . print ( countMinSteps ( arr , target , size ) ) ; } }