Count number of 1 s in the array after N moves | Function to count number of 1 's in the array after performing N moves ; If index is multiple of move number ; Convert 0 to 1 ; Convert 1 to 0 ; Count number of 1 's ; count number of 1 's ; Initialize array size ; Initialize all elements to 0
function countOnes ( arr , N ) { for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = i ; j <= N ; j ++ ) { if ( j % i == 0 ) { if ( arr [ j - 1 ] == 0 ) arr [ j - 1 ] = 1 ; else arr [ j - 1 ] = 0 ; } } } let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( arr [ i ] == 1 ) count ++ ; return count ; } let N = 10 ; let arr = new Uint8Array ( 10 ) ; let ans = countOnes ( arr , N ) ; document . write ( ans ) ;