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
function cntOnesArrWithGivenOp ( arr , N ) { let cntOnes = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } let arr = [ 0 , 0 , 0 , 0 , 0 ] ; let N = arr . length ; document . write ( cntOnesArrWithGivenOp ( arr , N ) ) ;