Given 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . . . k ' s print them in zig zag way . | function that prints given number of 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; two - dimensional array to store numbers . ; for even row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index ; for odd row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index . ; printing the stored elements . ; Driver Code
< ? php function ZigZag ( $ rows , $ columns , $ numbers ) { $ k = 0 ; $ arr = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ rows ; $ i ++ ) { if ( $ i % 2 == 0 ) { for ( $ j = 0 ; $ j < $ columns and $ numbers [ $ k ] > 0 ; $ j ++ ) { $ arr [ $ i ] [ $ j ] = $ k + 1 ; $ numbers [ $ k ] -- ; if ( $ numbers [ $ k ] == 0 ) $ k ++ ; } } else { for ( $ j = $ columns - 1 ; $ j >= 0 and $ numbers [ $ k ] > 0 ; $ j -- ) { $ arr [ $ i ] [ $ j ] = $ k + 1 ; $ numbers [ $ k ] -- ; if ( $ numbers [ $ k ] == 0 ) $ k ++ ; } } } for ( $ i = 0 ; $ i < $ rows ; $ i ++ ) { for ( $ j = 0 ; $ j < $ columns ; $ j ++ ) echo $ arr [ $ i ] [ $ j ] , " ▁ " ; echo " STRNEWLINE " ; } } $ rows = 4 ; $ columns = 5 ; $ Numbers = array ( 3 , 4 , 2 , 2 , 3 , 1 , 5 ) ; ZigZag ( $ rows , $ columns , $ Numbers ) ; ? >