Count number of 1 s in the array after N moves | C ++ implementation of the above approach ; Function to count number of 1 's in the array after performing N moves ; If index is multiple of move number ; arr [ j - 1 ] = 1 ; Convert 0 to 1 ; arr [ j - 1 ] = 0 ; Convert 1 to 0 ; Count number of 1 's ; count ++ ; count number of 1 's ; Driver Code ; Initialize all elements to 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOnes ( int arr [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { if ( j % i == 0 ) { if ( arr [ j - 1 ] == 0 ) else } } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] == 1 ) return count ; } int main ( ) { int arr [ 10 ] = { 0 } ; int ans = countOnes ( arr , N ) ; cout << ans ; return 0 ; }