Longest alternating ( positive and negative ) subarray starting at every index | CPP program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Print result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestAlternating ( int arr [ ] , int n ) { int count [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) cout << count [ i ] << " ▁ " ; } int main ( ) { int a [ ] = { -5 , -1 , -1 , 2 , -2 , -3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; longestAlternating ( a , n ) ; return 0 ; }