Generate an alternate increasing and decreasing Array | C ++ program to Generate an increasing and decreasing array ; Function that returns generated array ; Dynamically allocate array ; START = 0 , END = N ; iterate over array ; if Str [ i ] == ' I ' assign arr [ i ] as START and increment START ; if str [ i ] == ' D ' assign arr [ i ] as END and decrement END ; assign A [ N ] as START ; return starting address of array A ; Driver Program
#include <iostream> NEW_LINE using namespace std ; int * DiStirngMatch ( string Str ) { int N = Str . length ( ) ; int * arr = new int [ N ] ; int START = 0 , END = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( Str [ i ] == ' I ' ) arr [ i ] = START ++ ; if ( Str [ i ] == ' D ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } int main ( ) { string Str = " IDID " ; int N = Str . length ( ) ; int * ptr = DiStirngMatch ( Str ) ; for ( int i = 0 ; i <= N ; i ++ ) cout << ptr [ i ] << " ▁ " ; return 0 ; }