Generate an alternate increasing and decreasing Array | Java 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 code
class GFG { static int [ ] DiStirngMatch ( String Str ) { int N = Str . length ( ) ; int [ ] arr = new int [ N + 1 ] ; int START = 0 , END = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( Str . charAt ( i ) == ' I ' ) arr [ i ] = START ++ ; if ( Str . charAt ( i ) == ' D ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } public static void main ( String [ ] args ) { String Str = " IDID " ; int N = Str . length ( ) ; int [ ] ptr = DiStirngMatch ( Str ) ; for ( int i = 0 ; i <= N ; i ++ ) System . out . print ( ptr [ i ] + " ▁ " ) ; } }