Generate an Array in which count of even and odd sum sub | C ++ implementation of the approach ; Function to generate and print the required array ; Find the number of odd prefix sums ; If no odd prefix sum found ; Calculating the number of even prefix sums ; Stores the current prefix sum ; If current prefix sum is even ; Print 0 until e = EvenPreSums - 1 ; Print 1 when e = EvenPreSums ; Print 0 for rest of the values ; Driver code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void CreateArray ( int N , int even , int odd ) { int temp = -1 ; int OddPreSums ; for ( int i = 0 ; i <= N + 1 ; i ++ ) { if ( i * ( ( N + 1 ) - i ) == odd ) { temp = 0 ; OddPreSums = i ; break ; } } if ( temp == -1 ) { cout << temp << endl ; } else { int EvenPreSums = ( N + 1 ) - OddPreSums ; int e = 1 ; int o = 0 ; int CurrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( CurrSum % 2 == 0 ) { if ( e < EvenPreSums ) { e ++ ; cout << "0 ▁ " ; } else { o ++ ; cout << "1 ▁ " ; CurrSum ++ ; } } else { if ( e < EvenPreSums ) { e ++ ; cout << "1 ▁ " ; CurrSum ++ ; } else { o ++ ; cout << "0 ▁ " ; } } } cout << endl ; } } int main ( ) { int N = 15 ; int even = 60 , odd = 60 ; CreateArray ( N , even , odd ) ; return 0 ; }