Minimum Subarray flips required to convert all elements of a Binary Array to K | Java program to implement the above approach ; Function to count the minimum number of subarray flips required ; Iterate the array ; If arr [ i ] and flag are equal ; Return the answer ; Driver code
import java . util . * ; class GFG { static int minSteps ( int arr [ ] , int n , int k ) { int i , cnt = 0 ; int flag ; if ( k == 1 ) flag = 0 ; else flag = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == flag ) { cnt ++ ; flag = ( flag + 1 ) % 2 ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . length ; int k = 1 ; System . out . print ( minSteps ( arr , n , k ) ) ; } }