Longest alternating ( positive and negative ) subarray starting at every index | PHP program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Print result ; Driver code
< ? php function longestAlternating ( $ arr , $ n ) { $ count = array ( ) ; $ count [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] * $ arr [ $ i + 1 ] < 0 ) $ count [ $ i ] = $ count [ $ i + 1 ] + 1 ; else $ count [ $ i ] = 1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ count [ $ i ] , " ▁ " ; } $ a = array ( -5 , -1 , -1 , 2 , -2 , -3 ) ; $ n = count ( $ a ) ; longestAlternating ( $ a , $ n ) ; ? >