Minimize operations of removing 2 i | Java program to implement the above approach ; Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver code
import java . util . * ; class GFG { static int minimumStepReqArr ( int [ ] arr , int N ) { int cntStep = 0 ; N += 1 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( N & ( 1 << i ) ) != 0 ) { cntStep += 1 ; } } return cntStep ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . length ; System . out . println ( minimumStepReqArr ( arr , N ) ) ; } }