Generate an Array in which count of even and odd sum sub | 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
function CreateArray ( N , even , odd ) { let EvenPreSums = 1 ; let temp = - 1 ; let OddPreSums = 0 ; for ( let i = 0 ; i <= N + 1 ; i ++ ) { if ( i * ( ( N + 1 ) - i ) == odd ) { temp = 0 ; OddPreSums = i ; break ; } } if ( temp == - 1 ) { document . write ( temp ) ; } else { EvenPreSums = ( ( N + 1 ) - OddPreSums ) ; let e = 1 ; let o = 0 ; let CurrSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( CurrSum % 2 == 0 ) { if ( e < EvenPreSums ) { e ++ ; document . write ( " " ) ; } else { o ++ ; document . write ( " " ) ; CurrSum ++ ; } } else { if ( e < EvenPreSums ) { e ++ ; document . write ( " " ) ; CurrSum ++ ; } else { o ++ ; document . write ( " " ) ; } } } document . write ( ) ; } } let N = 15 ; let even = 60 , odd = 60 ; CreateArray ( N , even , odd ) ;