Maximum number of times Array can be reduced in half when its all elements are even | Java code implementation for the above approach ; Function to return the number of operations possible ; counter to store the number of times the current element is divisible by 2 ; variable to store the final answer ; Initialize the counter to zero for each element ; update the counter till the number is divisible by 2 ; update the answer as the minimum of all the counts ; Driver code
import java . util . * ; class GFG { static int arrayDivisionByTwo ( int arr [ ] , int n ) { int cnt = 0 ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; cnt ++ ; } ans = Math . min ( ans , cnt ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 12 , 40 } ; int n = arr . length ; System . out . print ( arrayDivisionByTwo ( arr , n ) ) ; } }