Counts 1 s that can be obtained in an Array by performing given operations | 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
def cntOnesArrWithGivenOp ( arr , N ) : NEW_LINE INDENT cntOnes = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( i - 1 , N , i ) : NEW_LINE INDENT arr [ j ] = 1 if arr [ j ] == 0 else 0 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT cntOnes += 1 NEW_LINE DEDENT DEDENT return cntOnes NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntOnesArrWithGivenOp ( arr , N ) ) NEW_LINE DEDENT