Counts 1 s that can be obtained in an Array by performing given operations | Java program to implement the above approach ; Function to count total number of 1 s in array by performing given operations ; Stores count of 1 s in the array by performing the operations ; Iterate over the range [ 1 , N ] ; Flip all array elements whose index is multiple of i ; Update arr [ i ] ; Traverse the array ; If current element is 1 ; Update cntOnes ; Driver Code
class GFG { static int cntOnesArrWithGivenOp ( int arr [ ] , int N ) { int cntOnes = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }