Generate an array B [ ] from the given array A [ ] which satisfies the given conditions | C ++ implementation of the approach ; 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 print 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void generateArr ( int arr [ ] , int n ) { bool flip = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) { if ( flip ^= true ) cout << ceil ( ( float ) ( arr [ i ] ) / 2.0 ) << " ▁ " ; else cout << floor ( ( float ) ( arr [ i ] ) / 2.0 ) << " ▁ " ; } else { cout << arr [ i ] / 2 << " ▁ " ; } } } int main ( ) { int arr [ ] = { 3 , -5 , -7 , 9 , 2 , -2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; generateArr ( arr , n ) ; return 0 ; }