Generate an array B [ ] from the given array A [ ] which satisfies the given conditions | Utility function to print the array elements ; Function to generate and print the required array ; To switch the ceil and floor function alternatively ; For every element of the array ; If the number is odd then prvar the ceil or floor value after division by 2 ; Use the ceil and floor alternatively ; If arr [ i ] is even then it will be completely divisible by 2 ; Driver code
function printArr ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function generateArr ( arr , n ) { var flip = true ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) { if ( flip ^= true ) document . write ( parseInt ( ( Math . ceil ( arr [ i ] / 2.0 ) ) ) + " " ) ; else document . write ( parseInt ( ( Math . floor ( arr [ i ] / 2.0 ) ) ) + " " ) ; } else { document . write ( arr [ i ] / 2 + " " ) ; } } } var arr = [ 3 , - 5 , - 7 , 9 , 2 , - 2 ] ; var n = arr . length ; generateArr ( arr , n ) ;