Minimize the non | Java implementation to minimize the non - zero elements in the array ; Function to minimize the non - zero elements in the given array ; To store the min pos needed ; Loop to iterate over the elements of the given array ; If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . ; Driver Code ; Function Call
class GFG { static int minOccupiedPosition ( int A [ ] , int n ) { int minPos = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] > 0 ) { ++ minPos ; i += 2 ; } } return minPos ; } public static void main ( String [ ] args ) { int A [ ] = { 8 , 0 , 7 , 0 , 0 , 6 } ; int n = A . length ; System . out . print ( minOccupiedPosition ( A , n ) ) ; } }